51nod1109(01组成的n的倍数)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1109

从小到大枚举只含有01的十进制数,因为只含有01,所以借助二进制,从1开始,把每一个数都转化成二进制,当做十进制来使用,看除以n的余数是不是0。

#include <iostream>

using namespace std;

int digit[20];
int n;

int main()
{
    cin >> n;
    for (long long i=1; i<=100000000; i++){
        int cnt = 0;
        int x = i;
        while (x > 0){
            digit[cnt++] = x & 1;
            x >>= 1;
        }
        int last = 0;
        for (int j=cnt-1; j>=0; j--){
            last = (last * 10 + digit[j]) % n;
        }
        if (last == 0){
            for (int j=cnt-1; j>=0; j--) {
                cout << digit[j];
            }
            cout << endl;
            break;
        }
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/qust1508060414/article/details/76732964