话不多说,上题。
Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 < N < 10000) . After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, with N = 13 , the sequence is:
12345678910111213
In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each digit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants to write a program to do this for him. Your task is to help him with writing this program.
Input
The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each test case, there is one single line containing the number N .
Output
For each test case, write sequentially in one line the number of digit 0, 1,...9 separated by a space.
题目的大意就是输入一个数字,然后写出1到这个数字的排列,最后输出0,1,2.。。。。这些数字各有多少个。
贴上代码。#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,k,l,t,m,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0;
char a[100000];
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
{
a1=a2=a3=a4=a5=a6=a7=a8=a9=a0=0;
l=0;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
if(j/1000!=0)
k=1;
else if(j/100!=0)
k=2;
else if(j/10!=0)
k=3;
else
k=4;
if(k==1)
{
a[l]=j/1000+'0';
a[l+1]=(j/100)%10+'0';
a[l+2]=(j/10)%10+'0';
a[l+3]=j%10+'0';
l=l+4;
}
if(k==2)
{
a[l]=j/100+'0';
a[l+1]=(j/10)%10+'0';
a[l+2]=j%10+'0';
l=l+3;
}
if(k==3)
{
a[l]=j/10+'0';
a[l+1]=j%10+'0';
l=l+2;
}
if(k==4)
{
a[l]=j+'0';
l=l+1;
}
}
for(m=0;m<strlen(a);m++)
{
if(a[m]=='0')
a0++;
if(a[m]=='1')
a1++;
if(a[m]=='2')
a2++;
if(a[m]=='3')
a3++;
if(a[m]=='4')
a4++;
if(a[m]=='5')
a5++;
if(a[m]=='6')
a6++;
if(a[m]=='7')
a7++;
if(a[m]=='8')
a8++;
if(a[m]=='9')
a9++;
}
printf("%d %d %d %d %d %d %d %d %d %d\n",a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,k,l,t,m,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0;
char a[100000];
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
{
a1=a2=a3=a4=a5=a6=a7=a8=a9=a0=0;
l=0;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
if(j/1000!=0)
k=1;
else if(j/100!=0)
k=2;
else if(j/10!=0)
k=3;
else
k=4;
if(k==1)
{
a[l]=j/1000+'0';
a[l+1]=(j/100)%10+'0';
a[l+2]=(j/10)%10+'0';
a[l+3]=j%10+'0';
l=l+4;
}
if(k==2)
{
a[l]=j/100+'0';
a[l+1]=(j/10)%10+'0';
a[l+2]=j%10+'0';
l=l+3;
}
if(k==3)
{
a[l]=j/10+'0';
a[l+1]=j%10+'0';
l=l+2;
}
if(k==4)
{
a[l]=j+'0';
l=l+1;
}
}
for(m=0;m<strlen(a);m++)
{
if(a[m]=='0')
a0++;
if(a[m]=='1')
a1++;
if(a[m]=='2')
a2++;
if(a[m]=='3')
a3++;
if(a[m]=='4')
a4++;
if(a[m]=='5')
a5++;
if(a[m]=='6')
a6++;
if(a[m]=='7')
a7++;
if(a[m]=='8')
a8++;
if(a[m]=='9')
a9++;
}
printf("%d %d %d %d %d %d %d %d %d %d\n",a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);
}
}
return 0;
}
这个程序的关键在于怎么把数字转化成字符串(嗷嗷,这是我的思路,把数字变成字符串,然后一个循环找一下就好)