NEU1681 The Singles(数学)

题目链接

题意:输入a,输出最小的n个1组成的数,使这个数能整除a(如果存在)。

思路:不断对余数乘以10+1,直到余数已经出现停止(不存在),或者能整除(存在)。

#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1000005], n;
int main(){
    //freopen("a.txt", "r", stdin);
    while(scanf("%d", &n) != EOF){
        memset(a, 0, sizeof(a));
        int ans = 1;
        int cnt = 1;
        int flag = 0;
        while(ans%n != 0){
            int t = ans%n;
            cnt++;
            ans = t*10+1;
            if(a[t]){
                flag = 1;
                break;
            }
            a[t] = 1;
        }
        if(flag) cout << "There is no Singles' Day!" << endl;
        else{
            cout << "Singles' Day is on ";
            for(int i = 0; i < cnt; i++) cout << "1";
            cout << "." <<endl;
        }
    }
    return 0;
}
Zhao Li /
Published under (CC) BY-NC-SA in categories 算法  tagged with 数学