这题分情况有点麻烦,按照y总讲的思路写的,代码不简洁,可读性差,没思路的就别看了
import java.io.*;
class Main {
static int[] a = new int[10];
static void reverse(int len) {
int i = 1, j = len;
while (j > i) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
j--;
i++;
}
}
static int generateA(int n) {
int len = 0;
while(n > 0) {
a[++len] = n%10;
n /= 10;
}
reverse(len);
return len;
}
static int power10(int time) {
int ans = 1;
while (time > 0) {
ans *= 10;
time --;
}
return ans;
}
static int gerPre(int i) {
int ans = 0;
for (int k = 1; k < i; k++) ans = ans * 10 + a[k];
return ans;
}
static int getSuf(int i, int len) {
int ans = 0;
for (int k = i + 1; k <= len; k++) ans = ans * 10 + a[k];
return ans;
}
static int getCnt(int n, int x) {
int len = generateA(n);
int cnt = 0;
for (int i = 1; i <= len; i++) {
//前缀不等时
int pre = gerPre(i);
int power = power10(len - i);
if (x == 0 && pre != 0) cnt += (pre - 1) * power;
else cnt += (pre) * power;
if (pre == 0 && x == 0) continue;
//前缀相等时
int suf = getSuf(i,len);
if (a[i] < x) continue;
else if (a[i] == x) cnt += suf + 1;
else cnt += power;
}
return cnt;
}
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StreamTokenizer in = new StreamTokenizer(br);
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int a = (int) in.nval;
in.nextToken();
int b = (int) in.nval;
if (a == 0 && b == 0) break;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
for (int x = 0; x < 10; x++) {
int cnt = getCnt(b,x) - getCnt(a - 1,x);
out.write(cnt+" ");
}
out.write("\n");
}
br.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}