字符串的完美度
来源:互联网 发布:python编写gui程序 编辑:程序博客网 时间:2024/04/30 10:42
还是庞果网,又看到一个题目,这个比较简单。
题目详情
算法描述
算法没什么可说的,比较简单,先把字符串都变成大写或者小写,然后依次找出出现次数最多的字母,然后最多的分26,次多的分25,依次类推,最后求和,算法难度一颗星。。。。
#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <vector>#include <iostream>using namespace std;struct myclass { bool operator() (int i,int j) { return (i>j);}} myobject;int howMany(string &str){ if(str.size()==1) { str.erase(0,1); return 1; } if(str.size()==0) return 0; string::iterator it; char cmp=str[0]; int count=0; for(int i=0;i<str.size();i++) { if(cmp==str[i]) { count++; str.erase(i,1); i--; } } return count;}int perfect(const string &s) { string str=s; vector<int> charCounts; int count,i,j; int perfectNum=0; transform(str.begin(), str.end(), str.begin(), ::toupper); do { count=howMany(str); charCounts.push_back(count); }while(count>0); sort(charCounts.begin(),charCounts.end(),myobject); for(i=0,j=26;i<charCounts.size();i++,j--) { perfectNum=perfectNum+j*charCounts[i]; //cout << charCounts[i] << " --- "; } //cout << endl; //cout << "perfect Num is " << perfectNum << endl; return perfectNum;}//start 提示:自动阅卷起始唯一标识,请勿删除或增加。int main(){ cout << perfect("DAddfdafdfhjfaodnDFESSDF") << endl;} //end //提示:自动阅卷结束唯一标识,请勿删除或增加。
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度
- 字符串的完美度总结
- 字符串的完美度算法
- 挑战编程:字符串的完美度
- 在线编程:字符串的完美度
- 编程挑战:字符串的完美度
- 庞果网之字符串的完美度
- nm命令介绍
- LA 3027并查集
- (一)HTTPS原理
- Linux常用命令大全
- android webview
- 字符串的完美度
- C++基于过程的程序设计
- 最大流
- HandlerSocket系列(三):性能及其性能优化
- 入门-WPF数据绑定-实例
- awk涉及的for用法
- Android 4.0 Launcher源码引入与调试
- 快速去掉.svn文件夹
- MYSQL中统计查询结果总行数的便捷方法