标题:ZOJ的1003的不同解法,其中一个能AC,而另一个输出错误。因此,我用自己的计 ...
取消只看楼主
SUREAl
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-9-12
 问题点数:0 回复次数:0 
ZOJ的1003的不同解法,其中一个能AC,而另一个输出错误。因此,我用自己的计算机产生随机数对(100000次以上),结果就是两个程序输出一样,并没有错,我该
ZOJ的1003的不同解法,其中一个能AC,而另一个输出错误。因此,我用自己的计算机产生随机数对(100000次以上),结果就是两个程序输出一样,并没有错,我该咋办?
可以通过的:
#include <stdio.h>
int flag1, flag2;

void dfs(int n, int m, int fac){
    if (flag1)
        return;
    if (n == 1 && m == 1){
        flag1 = 1;
        flag2 = 1;
        return;
    }
    if (m == 1){
        flag2 = 1;
    }

    if (fac < 2)
        return;
    if (n % fac == 0)
        dfs(n / fac, m, fac - 1);
    if (m % fac == 0)
        dfs(n, m / fac, fac - 1);
    dfs(n, m, fac - 1);
}

int main(){
    int n, m;
    while(~scanf("%d%d", &n, &m)){
        if (m > n){
            n = m ^ n;
            m = m ^ n;
            n = m ^ n;
        }
        flag1 = 0;
        flag2 = 0;
        dfs(n, m, 100);
        if (flag1 || !flag2)
            printf("%d\n", n);
        else
            printf("%d\n", m);
    }
    return 0;
}

不能通过的:
#include <stdbool.h>
#include <stdio.h>

#define MAX 100

bool factorFlag[MAX];

bool isResolve(int n, int factor){
    if (n == 1)
        return true;
    if (factor < 2)
        return false;
    else{
        while (factor >= 2){
            if (factorFlag[factor-1] == false && n % factor == 0)
                break;
            else
                factor--;
        }
        if (factor < 2)
            return false;
        if(isResolve(n/factor, factor-1))
            return true;
        else
            return isResolve(n, factor-1);
    }
}

bool isFactor(int n, int m, int factor){
    if (n == 1)
        return isResolve(m, MAX);
    if (factor < 2)
        return false;
    while (factor >= 2){
        if (n % factor == 0)
            break;
        else
            factor--;
    }
    if (factor < 2)
        return false;
    factorFlag[factor-1] = true;
    if (isFactor(n / factor, m, factor-1))
        return true;
    factorFlag[factor-1] = false;
    return isFactor(n, m, factor-1);
}

int main(){
    int i = 0;
    while (i < MAX){
        factorFlag[i++] = false;
    }
    int a, b;
    while (~scanf("%d%d", &a, &b)){
        if (b > a){
            b = a ^ b;
            a = a ^ b;
            b = a ^ b;
        }
        if (!isResolve(b, MAX) && b > MAX){
            printf("%d\n", a);
        }
        else if (isFactor(a, b, MAX))
            printf ("%d\n", a);
        else
            printf("%d\n", b);
    }
    return 0;
}
搜索更多相关主题的帖子: int return printf MAX false 
2017-09-12 23:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480652-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.296255 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved