OJ地址鏈接:http://acm.cugb.edu.cn/JudgeOnline/showproblem?problem_id=1022
Description
若一個整數Y的個位數為X,將X移到最高位得到的恰好是Y的X倍,稱Y為一個X倍數。例如,0是一個0倍數,1是一個1倍數。
Input
無
Output
請依次輸出最小的0倍數、1倍數、2倍數、……9倍數。每個X倍數輸出一行。
Sample Input
無
Sample Output
0
1
……
……
……
Hint
即使是最小的X倍數也可能是一個非常大的整數,甚至遠遠超出int所能表達的范圍。
對于X倍數,設定一個數組(事實上應該是字符串,節省空間),第0位賦予初始值k,k=2,3,……9,依次與k相乘得到上一位,即可。注意循環跳出的條件。
代碼:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100]="\0";
int i,c,j,k;
printf("0\n1\n");
for(k=2;k<10;k++)
{
str[0]=k+'0';
i=0;c=0;
while(1)
{
str[i+1]=((str[i]-'0')*k+c)%10+'0';
c=((str[i]-'0')*k+c)/10;
i++;
if((str[i]-'0')*k==k && c==0)
break;
}
for(j=i;j>=0;j--)
{
printf("%c",str[j]);
}
printf("\n");
memset(str,0,sizeof(str));
}
return 0;
}
#include<string.h>
int main()
{
char str[100]="\0";
int i,c,j,k;
printf("0\n1\n");
for(k=2;k<10;k++)
{
str[0]=k+'0';
i=0;c=0;
while(1)
{
str[i+1]=((str[i]-'0')*k+c)%10+'0';
c=((str[i]-'0')*k+c)/10;
i++;
if((str[i]-'0')*k==k && c==0)
break;
}
for(j=i;j>=0;j--)
{
printf("%c",str[j]);
}
printf("\n");
memset(str,0,sizeof(str));
}
return 0;
}