Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?
Sample Input
3
7
9901
Sample Output
3
6
12
题意:给出一个数N,N不是5或者2的倍数,求一个长度最小的由1组成的十进制数,满足这个数是N的倍数,输出1的个数。。。。0<N<100000
思路:如果我们写成m=m*10+1;if(m%N==0) break;count++;这样m的值会溢出。。
解释:像3这种,11进去余数为2,按原来想法我们应该让111进去,但是因为余数为2,我们把余数扩大10倍再加1,效果和111进去一样,如果这个数被除尽
那么111就被除尽
解释:像3这种,11进去余数为2,按原来想法我们应该让111进去,但是因为余数为2,我们把余数扩大10倍再加1,效果和111进去一样,如果这个数被除尽
那么111就被除尽
代码:
#include <stdio.h>
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
int count=1,m=0;
while(1){
m=(m*10+1)%N;
if(m==0)
break;
count++;
}
printf("%d\n",count);
}
return 0;
}
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
int count=1,m=0;
while(1){
m=(m*10+1)%N;
if(m==0)
break;
count++;
}
printf("%d\n",count);
}
return 0;
}