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;
}