题目地址:​​点击打开链接​

思路:本来挺简单的,结果犯了一个大错误,把错误代码也贴上来,要求按字典序最大,只要刚开始从大到小排序就行 ,还有就是明白了老师为啥告诉我们要好好学英语,这样就能用英语定义变量了,不只用a,b,c,d,cf,lol,这些破词来定义了,代码把cf敲成c,结果害的我调了2个小时,每个代码都是一部血泪史啊

错误代码1:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a,n,flag;
int visit[30];
char b[30];
int c[30],cf[6];
int cmp(const void *_x,const void *_y)
{
int *x = (int*)_x;
int *y = (int*)_y;
return *y - *x;
}
void dfs(int cur)
{
int i;
if(flag)
return;
if(cur == 5 && (cf[0] - cf[1]*cf[1] + cf[2]*cf[2]*cf[2] - cf[3]*cf[3]*cf[3]*cf[3] + cf[4]*cf[4]*cf[4]*cf[4]*cf[4] == a))
{
flag = 1;
for(i=0; i<5; i++)
{
printf("%c",cf[i] + 'A' -1);
}
printf("\n");
return;
}
for(i=0; i<n; i++)
{
if(visit[i] == 0)
{
visit[i] = 1;
cf[cur] = c[i];
dfs(cur + 1);
visit[i] = 0;
}
}
}
int main()
{
int i;
while(scanf("%d %s",&a,b))
{
flag = 0;
if(a == 0 && strcmp(b,"END") == 0)
break;
n = strlen(b);
memset(visit,0,sizeof(visit));
for(i=0; i<n; i++)
{
c[i] = b[i] - 'A' + 1;
}
qsort(c,n,sizeof(int),cmp);
dfs(0);
if(flag == 0)
printf("no solution\n");
}
return 0;
}

手贱把判断条件也加入if语句,害的cur=5还往下执行

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a,n,flag;
int visit[30];
char b[30];
int c[30],cf[6];
int cmp(const void *_x,const void *_y)
{
int *x = (int*)_x;
int *y = (int*)_y;
return *y - *x;
}
void dfs(int cur)
{
int i;
if(flag)
return;
if(cur == 5)
{
if(cf[0] - cf[1]*cf[1] + cf[2]*cf[2]*cf[2] - cf[3]*cf[3]*cf[3]*cf[3] + cf[4]*cf[4]*cf[4]*cf[4]*cf[4] == a)//就是这把cf写成c
{
flag = 1;
for(i=0; i<5; i++)
{
printf("%c",cf[i] + 'A' - 1);//注意减1
}
printf("\n");
}
return;
}
for(i=0; i<n; i++)
{
if(visit[i] == 0)
{
visit[i] = 1;
cf[cur] = c[i];
dfs(cur + 1);
visit[i] = 0;
}
}
}
int main()
{
int i;
while(scanf("%d%s",&a,b))//这里空不空都不影响结果
{
if(a == 0 && strcmp(b,"END") == 0)
break;
flag = 0;
n = strlen(b);
memset(visit,0,sizeof(visit));
for(i=0; i<n; i++)
{
c[i] = b[i] - 'A' + 1;//注意加1
}
qsort(c,n,sizeof(int),cmp);
dfs(0);
if(flag == 0)
printf("no solution\n");
}
return 0;
}

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char a[15];

int cmp(const void *_a,const void *_b)
{
return *(char*)_b - *(char*)_a;//记得前面还要再加一个*
}

int main()
{
int target,i,j,k,l,m,n,flag;
int x1,x2,x3,x4,x5;
while(scanf("%d %s",&target,a))
{
flag = 0;
if(target == 0 && strcmp(a,"END") == 0)
break;
n = strlen(a);
qsort(a,n,sizeof(a[0]),cmp);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(j == i)
continue;
for(k=0; k<n; k++)
{
if(k == i || k == j)
continue;
for(l=0; l<n; l++)
{
if(l == i || l == j || l == k)
continue;
for(m=0; m<n; m++)
{
if(m == i || m == j || m == k || m == l)
continue;
x1 = a[i] - 64;
x2 = a[j] - 64;
x3 = a[k] - 64;
x4 = a[l] - 64;
x5 = a[m] - 64;
if(x1 - x2*x2 + x3*x3*x3 - x4*x4*x4*x4 + x5*x5*x5*x5*x5 == target)
{
flag =1;
printf("%c%c%c%c%c\n",a[i],a[j],a[k],a[l],a[m]);
goto loop;
}
}
}
}
}
}
loop:if(!flag)
printf("no solution\n");
}
return 0;
}