思路:
利用java 特性,将数字从1 一直加到n,全部放到String中,然后依次对strring扫描每一位,使其carr[str.charAt(i)-'0']++;
最后输出carr[i],即可。
13
string=12345678910111213
carr[1]++、carr[2]++、carr[3]++、、、、carr[1]++、carr[1]++、carr[1]++、carr[2]++、carr[1]++、carr[3]++
AC Code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); while (n-- != 0) { int a = sc.nextInt(); String str = ""; int[] carr = new int[10]; for (int i = 1; i <= a; i++) str = str + i; for (int i = 0; i < str.length(); i++) { carr[str.charAt(i) - '0']++; } for (int i = 0; i < 9; i++) { System.out.print(carr[i] + " "); } System.out.println(carr[9]); } } } }