3-3
此题代码较为繁琐,前半部分初始化一整个数组为 "1234567891011....99989999" ,以便多组数据输入时直接查找即可,不必费时初始化;后半部分即为计数部分,代码如下:
#include <stdio.h>
char s[50000];
int main(void)
{
int T, N, count = 1, i = 0, find;
int n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;
n0=n1=n2=n3=n4=n5=n6=n7=n8=n9=0;
while (count / 10 == 0) {
s[i++] = count;
count++;
}
while (count / 100 == 0) {
s[i++] = count / 10;
s[i++] = count % 10;
count++;
}
while (count / 1000 == 0) {
s[i++] = count / 100;
s[i++] = (count/10) % 10;
s[i++] = count % 10;
count++;
}
while (count / 10000 == 0) {
s[i++] = count / 1000;
s[i++] = (count/100) % 10;
s[i++] = (count/10) % 10;
s[i++] = count % 10;
count++;
}
scanf("%d", &T);
getchar();
while (T--) {
scanf("%d", &N);
getchar();
if (N / 10 == 0)
find = N - 1;
else if (N / 100 == 0)
find = 2*N - 10;
else if (N / 1000 == 0)
find = 3*N - 109;
else
find = 4*N - 1108;
for (i = find; i >= 0; i--)
switch (s[i]) {
case 0:
n0++;
break;
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
break;
case 4:
n4++;
break;
case 5:
n5++;
break;
case 6:
n6++;
break;
case 7:
n7++;
break;
case 8:
n8++;
break;
case 9:
n9++;
break;
}
printf("%d %d %d %d %d %d %d %d %d %d\n",n0,n1,n2,n3,n4,n5,n6,n7,n8,n9);
n0=n1=n2=n3=n4=n5=n6=n7=n8=n9=0;
}
return 0;
}