HDU1015

来源:互联网 发布:用中文域名的大公司 编辑:程序博客网 时间:2024/05/17 02:14


//给出一串字符串只含有A--Z
//A代表1,B代表2,C代表3......以此类推//输入m和字符串//判断字符串中是否有五个这样的字母 满足 v - w^2 + x^3 - y^4 + z^5 = m ,并且只输出最大的一组数 ; #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char str[30];int m,len,num[30],step[10],book[50],f;void dfs(int x){if(x == 6)  //如果已经搜到了五个数 {if(step[1] - step[2]*step[2] + step[3]*step[3]*step[3] - step[4]*step[4]*step[4]*step[4] + step[5]*step[5]*step[5]*step[5]*step[5] == m)//判断是否满足条件 {printf("%c%c%c%c%c\n",step[1]+'A'-1,step[2]+'A'-1,step[3]+'A'-1,step[4]+'A'-1,step[5]+'A'-1);//满足条件,输出这几个数 f  = 1;  //标记 }return;}for(int i = len-1; i >= 0; i--){if(f)break;  // 如果已经找到答案break if(book[i] == 0){book[i] = 1;step[x] = num[i];dfs(x+1);book[i] = 0;}}return ;}int main(){while(scanf("%d",&m)){memset(book,0,sizeof(book));scanf("%s",str);if(m == 0 && strcmp(str,"END") == 0)break;else{f = 0;  //标记 len = strlen(str);sort(str,str+len);//对字符串排序 for(int i = len-1; i >= 0; i--)num[i] = str[i] - 'A' + 1;//将字符串转化成数字 dfs(1);if(f == 0)printf("no solution\n");}}}
原创粉丝点击