1、通过if else区分位数
#include<stdio.h>
int main()
{
int page,pagelenlist[1000]={0},resultlist[3]={729,900,1000},longth=0;
for(page=1;page<=999;page++)
{
if(page<10)
longth+=1;
else if(10<=page&page<100)
longth+=2;
else
longth+=3;
pagelenlist[page]=longth;
//printf("when the book has %d pages,the length of the number is %d\n",page,pagelenlist[page]);
}
for(page=1;page<=999;page++)
{
for(int j=0;j<3;j++)
{
if(resultlist[j]==pagelenlist[page])
printf("when the book has %d pages,the length of the number is %d\n",page,pagelenlist[page]);
}
}
return 0;
}
when the book has 279 pages,the length of the number is 729
when the book has 336 pages,the length of the number is 900
2、将整型转换为字符串,然后strlen
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int page,pagelenlist[1000]={0},resultlist[3]={729,900,1000};
for(page=1;page<=999;page++)
{
char string[16]={0};
itoa(page,string,10);
pagelenlist[page]=pagelenlist[page-1]+strlen(string);
//printf("when the book has %d pages,the length of the number is %d\n",page,pagelenlist[page]);
for(int j=0;j<3;j++)
{
if(resultlist[j]==pagelenlist[page])
printf("when the book has %d pages,the length of the number is %d\n",page,pagelenlist[page]);
}
}
return 0;
}