可以帮我看一下怎么改进这个程序吗
查看原帖
可以帮我看一下怎么改进这个程序吗
1489953
SelinaFlora楼主2024/10/13 19:20

本人刚学c语言,在制作这个程序的时候最后一个测试超时,大佬们可以帮我看一下该怎么改进吗

#include<stdio.h>
int test1(int);
int test2(int);
int test1(int n){
	int t=2;
	while(t*t<=n){
		if(n%t==0){
			return 0;
		}
		t++;
	}
	return 1;
}
int test2(int n){
	int pow[9],i=0,t=0,p;
	while(n%10!=0||n/10!=0){
		pow[i]=n%10;
		n/=10;
		i++;
	}
	p=i-1;
	while(t<=p){
		if(pow[t]!=pow[p])
			return 0;
		t++;
		p--;
	}
	return 1;
}
int main(){
	int n1,n2,t;
	scanf("%d %d",&n1,&n2);
	t=n1;
	while(t<=n2){
		if((test2(t)==1)&&(test1(t)==1)){
			printf("%d\n",t);
		}
		t++;
	}
	return 0;
}
2024/10/13 19:20
加载中...