每个输入包含1个测试用例,即一个不超过1000位的正整数N。
对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。
100311
0:2<br/>1:3<br/>3:1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String n = in.nextLine(); String m = "0123456789"; int num[] = new int[10]; for (int j = 0; j < m.length() ; j++) { int count = 0; for (int i = 0; i < n.length(); i++) { if(n.charAt(i) == m.charAt(j)){ count++; } } num[j] = count; } for (int k = 0; k < 10;k++) { if(num[k] != 0){ System.out.println(k+":"+num[k]); } } } }
反正就是0-9遍历两次就好咯!
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.nextLine(); HashMap<Character,Integer> map = new HashMap<>(); for(char ch:str.toCharArray()){ map.put(ch,map.getOrDefault(ch,0)+1); } for(Map.Entry<Character,Integer> entry : map.entrySet()){ System.out.println(entry.getKey() +":"+entry.getValue()); } } } }
import java.util.*; public class Main { //思路:建个数组0~9用于统计次数M public static void main(String[] args) { Scanner in=new Scanner(System.in); String str=in.nextLine(); int[] times=new int[10]; for(char c:str.toCharArray()) { times[c-'0']++; } for(int i=0;i<10;i++) {if(times[i]!=0) System.out.println(i+":"+times[i]);} }}
只能说 java 1.8 新特性真好用
import java.util.*; public class Pat1011 { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] num = in.next().toCharArray(); Map<Character, Integer> count = new TreeMap<>((a,b)->a.compareTo(b)); for(char c : num) { count.merge(c, 1, (a,b)->a+b); } for(Map.Entry<Character, Integer> e : count.entrySet()) { System.out.println(e.getKey() + ":" + e.getValue()); } } }
import java.util.Scanner; public clas***ain{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.next(); StringBuffer buffer = new StringBuffer(s); int []array = {0,0,0,0,0,0,0,0,0,0}; for (int i = 0; i < buffer.length(); i++) { char c = buffer.charAt(i); Integer integer = new Integer(c); array[integer.intValue()-48]++; } for (int i = 0; i < array.length; i++) { if (array[i]!=0){ System.out.println(i+":"+array[i]); } } }}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String n = sc.next(); String[] nc = n.split(""); int[] result = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int temp = 0; for (int i = 1; i < nc.length; i++) { temp = Integer.parseInt(nc[i]); result[temp]++; }int i=0; while(i<10){ if(result[i]!=0)System.out.println(i+":"+result[i]); i++; } } }
我本地运行没问题,他老是说我答案错误,输入0说没输出,但是我本地运行时有输出的
import java.util.Scanner;
import java.util.Scanner; public class Main { @SuppressWarnings("resource") public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] numArray = new int[10];// 先建立一个大小为10的数组,因为只有10个数字 String string = in.next();// 得到输入的字符串 for (int i = 0; i < string.length(); i++) { numArray[string.charAt(i) - '0']++;// 这一步很关键,统计每个字符出现的次数,用作字母也可以, } for (int i = 0; i < numArray.length; i++) { if (numArray[i] != 0) { System.out.println(i + ":" + numArray[i]); } } } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
int[] num = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0;i < a.length();i++){
num[(int)(a.charAt(i) - '0')]++;
}
for (int i = 0;i < 10;i++){
if (num[i] != 0)
System.out.println(i + ":" + num[i]);
}
}
}
import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); char [] arr= sc.nextLine().toCharArray(); Map <Character,Integer> number = new TreeMap<>(); for (char c : arr) { if(number.containsKey(c)){ number.put(c, number.get(c)+1); }else{ number.put(c, 1); } } Iterator<Entry<Character, Integer>> it = number.entrySet().iterator(); Entry<Character, Integer> en; while(it.hasNext()){ en = it.next(); System.out.println(en.getKey()+":"+en.getValue()); } } }