第2个回答 2014-03-09
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int n = sc.nextInt();
// 创建数组,存放0-9每个数字有多少个
int[] num = new int[10];
// 循环,对每个整数求其中包含的0-9个数,然后加和
for (int i = 0; i <= n; i++) {
int[] nn = getNum(i);
for (int j = 0; j < num.length; j++) {
num[j] += nn[j];
}
}
// 输出结果
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
// 对一个整数,求出其中包含0-9个数,返回数组
private static int[] getNum(Integer n) {
int[] nn = new int[10];
char[] chars = n.toString().toCharArray();
for (char c : chars) {
nn[Integer.parseInt(c + "")]++;
}
return nn;
}
}