"Programming Thinking and Practice" 1033. Character Frequency

"Programming Thinking and Practice" 1033. Character Frequency

topic

insert image description here
insert image description here

train of thought

Lowercase letters and uppercase letters have the same frequency, and then judge whether the letter frequency is the same, and then judge whether it is the difference between uppercase and lowercase letters.

the code

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

double weigh[128]={
    
    0};          //每个位置的权重 

int cmp(const void *a,const void *b)
{
    
    
	char *m=(char*)a;
	char *n=(char*)b;
	
	if(weigh[*m]!=weigh[*n])
	{
    
    
        return weigh[*m]<weigh[*n]?1:-1;
	}
	else{
    
    
		if((*m>='a'&&*m<='z')&&(*n>='a'&&*n<='z'))  //同小写
		{
    
    
			return *m-*n;
		}
		else if((*m>='A'&&*m<='Z')&&(*n>='A'&&*n<='Z'))  //同大写
		{
    
    
			return *m-*n;
		}
		else if((*m>='a'&&*m<='z')&&(*n>='A'&&*n<='Z'))  //一个小写一个大写 将小写变成大写(-32)
		{
    
    
			return *m-32>*n?1:-1;
		}
		else if((*m>='A'&&*m<='Z')&&(*n>='a'&&*n<='z')) //一个小写一个大写 将小写变成大写(-32)
		{
    
    
            return *m>*n-32?-1:1;
		}
	}
}

int main()
{
    
    
	int T;
	scanf("%d",&T);
	for(int i=0;i<T;i++)
	{
    
    
		double temp;
		for(int j=0;j<26;j++)
		{
    
    
			scanf("%lf",&temp);
			weigh['a'+j]=temp;
			weigh['A'+j]=temp;
		}
		char s[101];  //不超过100个字符
		scanf("%s",s);
		qsort(s,strlen(s),sizeof(char),cmp);
		printf("case #%d:\n",i);
		printf("%s\n",s); 
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/boxueyuki/article/details/130037512