poj2551

题意:n<=10000,并保证存在n的某个倍数的十进制表示形式各个数位都为1。问这个数是n的多少倍。

分析:我们枚举1,11,111……直到找到能被n整除的为止。为了防止这个1序列过大,我们不断将其对n取余,这样可以保证其一直在int范围内,并且不影响整除性。

#include <iostream>

#include <cstdlib>

#include <cstdio>

#include <cstring>

using namespace std;



int n;



int main()

{

    while (scanf("%d", &n) != EOF)

    {

        int ans = 1;

        int temp = 1;

        while (temp)

        {

            ans++;

            temp = temp * 10 + 1;

            temp %= n;

        }

        printf("%d\n", ans);

    }

    return 0;

}
View Code

 

你可能感兴趣的:(poj)