首页 > 试题广场 >

字符个数统计

[编程题]字符个数统计
  • 热度指数:561597 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的字符串,统计其中的 ASCII 码在 0127 范围内的不同字符的个数。

\hspace{15pt}备注:受限于输入,本题实际输入字符集为 ASCII 码在 33126 范围内的可见字符。您可以参阅下表获得其详细信息(您可能关注的内容是,这其中不包含空格、换行)。




输入描述:
\hspace{15pt}输入一个长度 1 \leqq {\rm length}(s) \leqq 500,仅由图片中的可见字符构成的字符串 s


输出描述:
\hspace{15pt}在一行上输出一个整数,代表给定字符串中 ASCII 码在 0127 范围内的不同字符的个数。
示例1

输入

[@A8aA].0

输出

8

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String[] str = in.nextLine().split("");
        Set num = new HashSet<>(Arrays.asList(str));

        System.out.println(num.size());
    }
}
发表于 2025-11-23 04:57:14 回复(0)
java版本,导入HashSet包,保存输入进来的字符串,从头遍历字符串中的每一个字符,如果不在HashSet里,记录为一个不重复字符,如果在HashSet里,直接跳过此字符,进入下一个字符,遍历完后,count即记录为整个子串的非重复字符数
import java.util.Scanner;
import java.util.HashSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            int count = 0;
            HashSet<Character> set = new HashSet<>();
            String str = in.nextLine();
            for (int i = 0; i < str.length(); i++){
                if (!set.contains(str.charAt(i))){
                    set.add(str.charAt(i));
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
发表于 2025-07-16 15:20:47 回复(1)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int len = str.length();
        
        Set<Character> set = new HashSet<>();
        for(int i= 0;i< len; i++){
            int c = str.charAt(i);
            char c1 = str.charAt(i);
            if(c>= 33 && c<= 126 && !set.contains(c1)){
                
                set.add(c1);
            }
        }
          System.out.println(set.size());
       
    }
}

发表于 2025-07-11 22:06:16 回复(1)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        Set set = new HashSet();
        for(int i=0;i<str.length();i++){
            int asc = (int)str.charAt(i);
            if(asc >127 || asc <0){
                continue;
            }
            set.add(str.charAt(i));
        }
        System.out.println(set.size());
    }

发表于 2025-07-06 11:17:22 回复(0)
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        Set<Integer> num = new HashSet<>();

        for (int i = 0; i < str.length(); i++) {
            num.add((int)str.charAt(i));
        }

        System.out.println(num.size());
    }
}

发表于 2025-06-16 15:40:11 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.next();
        Set<Character> set = new LinkedHashSet<>();
       
        int count = 0;
        for(int i = 0;i<str.length();i++){
            set.add(str.charAt(i));
        }
        for(Character cha : set){
            if(Integer.valueOf(cha)>0 & Integer.valueOf(cha)<127){
                count++;
            }
        }
        System.out.print(count);
    }
}
发表于 2025-06-13 15:19:47 回复(0)

import java.util.Scanner;

import java.util.LinkedHashSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息

public class Main {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    // 注意 hasNext 和 hasNextLine 的区别

// 注意 while 处理多个 case

    String a = in.nextLine();

    LinkedHashSetInteger> set = new LinkedHashSet();

    for(int i=0;ia.length();i++){

        int b = (int)a.charAt(i);

        if(b>=0&&b127){

            set.add(b);

        }    

    }

    System.out.print(set.size());

}

}

发表于 2025-05-21 00:01:08 回复(0)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        int count =0;zz
        Set s = new HashSet<Character>();
        for(int i =0; i< str.length();i++){
            Character c = str.charAt(i);
            try{
              int a = c;
              if(a >=0 && a<=127){
                s.add(a);
              }
            }catch(Exception e){
                continue;
            }
            
        }
        System.out.println(s.size());

    }
}

发表于 2025-05-18 16:52:27 回复(0)
import java.util.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        String str=in.nextLine();

        //set容器不允许有重复的元素,所以用set容器存储字符
        HashSet<Character> set= new HashSet<Character>();

        for(int i=0;i<str.length();i++){
            if(str.charAt(i)>=0 && str.charAt(i)<=127){
                set.add(str.charAt(i));
            }
        }

        System.out.println(set.size());
    }
}



发表于 2025-05-15 15:22:45 回复(0)
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner( System.in );
        String input = sc.nextLine().trim();

        Map< Character, Integer > map = new HashMap<>();
        for ( int i = 0; i < input.length(); i++ ) {
            map.put(input.charAt(i), map.getOrDefault( input.charAt(i), 0) + 1 );
        }
        System.out.println(map.size());
    }
}
发表于 2025-03-19 18:53:10 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        byte[] b=a.getBytes();
        int[] c=new int[128];
        int item=0;
        for(int i=0;i<a.length();i++){
            if(b[i]<127) {
              if(c[b[i]]==0){
                item++;
                c[b[i]]=1;
              }  
            }
       }
        System.out.println(item);
        
    }
}
发表于 2025-02-22 14:52:30 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        //使用HashSet存储不同的字符
        Set<Character> chars = new HashSet<>();
        for (int i = 0; i < str.length(); i++) {
            // 将字符添加到HashSet中,自动去重
            chars.add(str.charAt(i));
        }
        System.out.println(chars.size());
    }
}

发表于 2024-10-17 21:29:57 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Character> list = new ArrayList<>();
        String str = in.nextLine();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!list.contains(c)) {
                list.add(c);
            }
        }
        System.out.println(list.size());
    }
}

发表于 2024-10-09 14:01:21 回复(0)
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeSet<Integer> mySet = new TreeSet<>();
        while (in.hasNext()) {
            String str = in.nextLine();
            for (int i = 0; i < str.length(); i++) {
                mySet.add((int) str.charAt(i));
            }
        }
        System.out.print(mySet.size());
    }
}
发表于 2024-09-30 20:42:40 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        System.out.println(str.chars().distinct().count());
    }
}

发表于 2024-09-25 12:06:13 回复(0)