//错误答案
#include<stdio.h>
#define M 10009
int have[7]={};
char str[M];
char g[6]={'P','A','T','e','s','t'};
int main()
{
int num;
scanf("%s",str);
int i;
for(i=0;str[i];i++)
{
if(str[i]=='P') have[0]++;
else if(str[i]=='A') have[1]++;
else if(str[i]=='T')have[2]++;
else if(str[i]=='e')have[3]++;
else if(str[i]=='s')have[4]++;
else if(str[i]=='t')have[5]++;
num++;
}
while(num>0)
{
for(i=0;i<6;i++)
if(have[i])
{
printf("%c",g[i]);
have[i]--;
num--;
}
}
return 0;
}
这个答案超时了,更改为如下
//正确答案
#include<stdio.h>
#define M 10009
int have[7]={};
char str[M];
char g[6]={'P','A','T','e','s','t'};
int main()
{
int num;
scanf("%s",str);
int i;
for(i=0;str[i];i++)
{
if(str[i]=='P') have[0]++;
else if(str[i]=='A') have[1]++;
else if(str[i]=='T')have[2]++;
else if(str[i]=='e')have[3]++;
else if(str[i]=='s')have[4]++;
else if(str[i]=='t')have[5]++;
}
while(1)
{
num=1;
for(i=0;i<6;i++)
if(have[i])
{
printf("%c",g[i]);
have[i]--;
num=0;
}
if(num)
break;
}
return 0;
}
对,就在于num上面,多计算两步就完蛋