POJ 2551 Ones(数论)

Description
找出一个数的由1组成的最小倍数(10进制),输出其倍数的位数
Input
多组输入,每组一个整数n,以文件尾结束
Output
对于每组用例,输出其由1组成的最小倍数的位数
Sample Input
3
7
9901
Sample Output
3
6
12
Solution
直接暴力会超时,稍微优化一下即可
其实根据(a+b)%d=(a%d+b%d)%d就可以做出来了。
以7为例
1%7=1 1%7=1
11%7=4 (10%7+1)%7=4
111%7=6 (4*10%7+1)%7=6
1111%7=5 (6*10%7+1)%7=5
11111%7=2 (5*10%7+1)%7=2
111111%7=0 (2*10%7+1)%7=0
输出6.
Code

#include<stdio.h>
int main()
{
    int n,m,j;
    while(scanf("%d",&n)!=EOF)
    {
        m=1;
        for(j=1;;j++)
        {
            m=m%n*10+1;
            if(m%n==0)
            {
                printf("%d\n",j+1);
                break;
            }
        }
    }
    return 0;
}

你可能感兴趣的:(POJ 2551 Ones(数论))