查了十几遍不知道为什么不输出
查看原帖
查了十几遍不知道为什么不输出
432183
JoeBiden2020楼主2021/5/29 00:34

如题,提交显示answer too short,本地调试发现 main中的四句dfs直接略过了?

#include<iostream>
using namespace std;
int n,p[4]={1,3,7,9};
bool prime(int x){
    if(x==1) return 0;
    if(x==2) return 1;
    for(int i=2;i<=x;i++){
        if(x%i==0){
		  	return 0;
		}
    }
        return 1;
}
void dfs(int num,int len){
    int y; 
	    if(len==n){
	        cout<<num<<endl;
	        return;
	    }
	    else{
	    for(int i=0;i<4;i++){
	        y=num*10+p[i];
	    	if(prime(y))dfs(y,len+1);
		}
	}
}
int main(){
    cin>>n;
    dfs(2,1);
    dfs(3,1);
    dfs(5,1);
    dfs(7,1);
    return 0;
}
2021/5/29 00:34
加载中...