zoukankan      html  css  js  c++  java
  • 华为机试:字符个数统计

    题目描述

    编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。

    输入描述:

    输入N个字符,字符在ACSII码范围内。

    输出描述:

    输出范围在(0~127)字符的个数。

    示例1

    输入

    abc

    输出

    3

    Java:

     1 import java.util.Scanner;
     2 
     3 public class Main {
     4 
     5     public static void main(String[] args) {
     6         Scanner sc=new Scanner(System.in);
     7         while(sc.hasNext()){
     8             String s = sc.nextLine();
     9             int[] state = new int[128];
    10             for(int i = 0;i < s.length();i++){
    11                 int temp_num = s.charAt(i);
    12                 state[temp_num] = 1;
    13             }
    14             int count = 0;
    15             for(int i = 0;i<state.length;i++){
    16                 count+=state[i];
    17             }
    18             System.out.println(count);
    19         }
    20         sc.close();
    21     }
    22 }
  • 相关阅读:
    luogu4781
    luogu 4933
    luogu p1726
    bzoj2238
    luogu 1462 通往奥格瑞玛的道路
    noip.ac 3276 矩阵
    luogu1144
    noip.ac 3248
    奶牛比赛
    小P的Civilization V
  • 原文地址:https://www.cnblogs.com/zdtiio/p/7570655.html
Copyright © 2011-2022 走看看