725 - Division (UVA)

题目链接如下:

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=9&page=show_problem&problem=666

先贴一个我觉得很厉害的写法:UVA-725_uva725-CSDN博客

我的解法只能说又臭又长,但敝帚自珍吧哈哈:

#include 
#include 
#include 
#include 
#include 
// #define debug

int N, kase = 0;
std::vector> ans[80];

bool uniq(int p){
    int cnt[10];
    std::fill(cnt, cnt + 10, 0);
    while (p){
        if (cnt[p % 10]++){
            return false;
        }
        p /= 10;
    }
    return true;
}

bool uniqPair(int p, int q){
    std::string ss = std::to_string(p) + std::to_string(q);
    sort(ss.begin(), ss.end());
    if (ss == "123456789" || ss == "0123456789"){
        return true;
    }
    return false;
}

void judge(int p, int k){
    int q = p * k;
    if (q > 98765 || !uniq(q)){
        return;
    }
    if (uniqPair(p, q)){
        ans[k].push_back({q, p});
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    for (int i = 1234; i <= 98765 / 2; ++i){
        if (!uniq(i)){
            continue;
        }
        for (int k = 2; k <= std::min(79, 98765 / i); ++k){
            judge(i, k);
        }
    }
    while (scanf("%d", &N) == 1 && N){
        printf("%s", kase ? "\n" : "");
        ++kase;
        if (ans[N].empty()){
            printf("There are no solutions for %d.\n", N);
        } else {
            for (int i = 0; i < ans[N].size(); ++i){
                printf("%05d / %05d = %d\n", ans[N][i].first, ans[N][i].second, N);
            }
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,算法)