庞果网之字符串的完美度

题目详情

我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同,

而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度是一样的。


现在给定一个字符串,输出它的最大可能的完美度。

例如:dad,你可以将26分配给d,25分配给a,这样整个字符串最大可能的完美度为77。


/*********************************
*   日期:2013-11-03
*   作者:SJF0115
*   题号: 题目 字符串的完美度
*   来源:http://hero.pongo.cn/Question/Details?ID=71&ExamID=69
*   结果:AC
*   来源:庞果网
*   总结:
**********************************/
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

int cmp(const void*a,const void*b)  
{  
	return *(int*)b - *(int*)a;  
}  

int perfect(const string &s){
	int i;
	int len = s.size();
	int count[26] = {0};
	//统计字母个数
	for(i = 0;i < len;i++){
		if(s[i] >= 'A' && s[i] <= 'Z'){
			count[s[i] - 'A'] ++;
		}
		else if(s[i] >= 'a' && s[i] <= 'z'){
			count[s[i] - 'a'] ++;
		}
	}
	//从大到小排序
	qsort(count,26,sizeof(count[0]),cmp);
	int perfectDegree = 26;
	int sum = 0;
	//计算完美度
	for(i = 0;i < 26;i++){
		sum += count[i] * perfectDegree;
		perfectDegree --;
	}
	return sum;
}

int main()
{   
	int i,n;
	string str;
	while(cin>>str){
		printf("%d\n",perfect(str));
	}
    return 0;
}



天猫搞活动。。。。。。。。

 我在用来往,用它我们就能免费语聊!11月1日到11月9日用淘宝账号登陆我还能送你2元双11现金红包!点击  点击打开链接 ,赶紧拿红包吧! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值