可以AC,但为啥两个IDE运行的结果不一样
查看原帖
可以AC,但为啥两个IDE运行的结果不一样
468029
dfxgbm楼主2021/2/3 10:41
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
int t[11],a,b; 
bool isprime(int x) {
	for(int i=2; i*i<=x; i++) {
		if(x%i==0)
			return false;
	}
	return true;
}

int wei(int x) {
	int t=0;
	while(x) {
		x/=10;
		t++;
	}
	return t;
}

void huiwen(int x,int h) { //h代表最高有多少位
	int k=0;
	if(h%2!=0) { //除11外,所有回文质数都是奇数位
		if(x>h/2) {
			for(int i=0; i<h; i++) {
				k=k+(t[i]*pow(10,h-i-1));
			}
			if(isprime(k)&&k>=a&&k<=b)
				printf("%d\n",k);
			return;
		}
		for(int i=0; i<=9; i++) {
			t[x]=i;
			t[h-x-1]=i;
			if(t[0]!=0)
				huiwen(x+1,h);
		}
	}
}

int main() {
	scanf("%d%d",&a,&b);
	if(a<=5&&b>=5)
		printf("5\n");
	if(a<=7&&b>=7)
		printf("7\n");
	if(a<=11&&b>=11)
		printf("11\n"); //特判
	for(int i=3; i<=wei(b); i++) {
		huiwen(0,i);
	}
	return 0;
}

上面这段代码提交上去可以AC,但用codeblocks17.12编译后无法得到正确答案,用dev-c++5.11(我自己手动安装了GCC8.1.0)却可以,我感到很奇怪,是不是代码里有什么未定义行为?

2021/2/3 10:41
加载中...