Division UVA - 725 (枚举)

虽然是简单枚举。。但其实题中需要考虑的东西还蛮多的。前几位为0的情况用%05d补齐就行,然后计算0-9各个数的输出次数即可

#include<bits/stdc++.h>
using namespace std;
int cnt[15]; //0-9每个数的出现次数
bool judge(int c, int d)
{
    if(d > 98765) return false; //超过5位数
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < 5; i++){
        cnt[c%10]++, cnt[d%10]++;
        c/=10, d/=10;
    }
    for(int i = 0;i < 10;i++) //判断是否重复
        if(cnt[i] != 1) return false;
    return true;
}
int main()
{
    int n, kase = 0;
    while(scanf("%d",&n) == 1 && n){
        if(kase++ > 0) printf("\n");
        bool flag = false;
        for(int i = 1234; i <= 99999; i++)//i为被除数
            if(judge(i, n*i)){
                printf("%05d / %05d = %d\n",n*i,i,n); //左边直接补0
                flag = true;
            }
        if(!flag) printf("There are no solutions for %d.\n",n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/82701577
今日推荐