题目描述:点击打开链接
值得一提的是,对这里的题目描述要理解准确。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************************************************************************/
/* */
/* DON'T MODIFY main() function anyway! */
/* */
/******************************************************************************/
double orderTable[26];
void solve(); /* write function solve() to process one case of the problem */
void init(){}
int main()
{ int i,t; init();
scanf("%d\n",&t);
for (i=0;i<t;i++)
{ printf("case #%d:\n",i);
solve();
}
return 0;
}
//1. 输入
//2. 编写比较函数
//3. 调用qsort函数对字符串重排
//4. 输出
int cmp(const void *a,const void *b)
{
char ch1,ch2; //ch1,ch2为待比较的字母
int p1,p2; //p1,p2为ch1,ch2在字母顺序表中的序号
ch1=*((char *)a);
ch2=*((char *)b);
if(ch1>='a'&&ch1<='z') p1=ch1-'a'; else p1=ch1-'A';
if(ch2>='a'&&ch2<='z') p2=ch2-'a'; else p2=ch2-'A';
if(orderTable[p2]>orderTable[p1]) return 1; //按照字母使用频率由大到小重排
else if(orderTable[p2]==orderTable[p1]) //字母使用频率相同的情况
if(p1==p2) return ch2-ch1; //相同字母,小写在大写前面
else return p1-p2;
//不同字母,但使用频率相同,则按照字母表顺序排列
else return -1;
}
void solve(){
double tmp;
char str[101];
int i;
for(i=0;i<25;i++){
scanf("%lf",&orderTable[i]);
}
scanf("%lf\n",&orderTable[i]);
gets(str);
qsort(str,strlen(str),sizeof(char),cmp);
printf("%s\n",str);
}