标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12203 Accepted Submission(s): 6319
暴力搜素所有可能的结果
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> //#define res ((ans[0]-‘A‘+1)+pow(ans[1]-‘A‘+1,2)+pow(ans[2]-‘A‘+1,3)+pow(ans[3]-‘A‘+1,4)+pow(ans[4]-‘A‘+1,5)) using namespace std; int len; bool flag; bool vis[30]; char ans[30]; int n; char ch[30]; int pow(int a,int b) { int tmp=1; for(int i=0;i<b;i++) tmp*=a; return tmp; } void dfs(int num) { if(num>5) return; if(num==5) { int tmp=((ans[0]-‘A‘+1)-pow(ans[1]-‘A‘+1,2)+pow(ans[2]-‘A‘+1,3)-pow(ans[3]-‘A‘+1,4)+pow(ans[4]-‘A‘+1,5)); //cout<<tmp<<endl; //if(ans[0]==‘Y‘&&ans[1]==‘O‘)printf("%s %d\n",ans,tmp); if(tmp==n) flag=true; return; } for(int i=0; i<len; i++) { if(flag) return; if(!vis[i]) { ans[num]=ch[i]; vis[i]=true; dfs(num+1); vis[i]=false; } } } bool cmp(char a,char b) { return a>b; } int main() { while(scanf("%I64d %s",&n,ch)) { if(n==0&&!strcmp(ch,"END")) break; len=strlen(ch); sort(ch,ch+len,cmp); for(int i=0; i<30; i++) vis[i]=false; flag=false; dfs(0); if(flag) printf("%s\n",ans); else printf("no solution\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5537737.html