| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 608 人关注过本帖
标题:[求助]下面的帖子怎么才能改成按使用频率从大到小进行排序
只看楼主 加入收藏
kid1412
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-6-26
收藏
 问题点数:0 回复次数:2 
[求助]下面的帖子怎么才能改成按使用频率从大到小进行排序

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
//select the letters of input the store them in a string
char word;
string str;
while ((word = cin.get()) != '\n'){
if (isalpha(word)){
word = tolower(word);
str += word;
}
}
//insertion sort for the string
for (int i = 1;i < str.size();++i){
if (str[i] < str[i-1]){
char temp = str[i];
int j = i;
do{
str[j] = str[j-1];
--j;
}
while (j > 0 && temp < str[j-1]);
str[j] = temp;
}
}
if (str.empty()){
cerr << "no letter in your input" << endl;
system("pause");
return EXIT_FAILURE;
}
cout << str << endl;
//count the letter times
int pos = 0;
while (true){
char key = str[pos];
int mark = str.find_first_not_of(key,pos);
if (mark == string::npos){
cout << key << "\t\t" << str.size() - pos << endl;
break;
}
int length = mark - pos;
cout << key << "\t\t" << length << endl;
pos = mark;
}
system("PAUSE");
return EXIT_SUCCESS;
}

谢谢拉!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

搜索更多相关主题的帖子: 频率 帖子 int include 
2007-06-27 19:54
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
//insertion sort for the string
for (int i = 1;i < str.size();++i){
if (str[i] > str[i-1]){ //change < to >
char temp = str[i];
int j = i;
do{
str[j] = str[j-1];
--j;
}
while (j > 0 && temp < str[j-1]);
str[j] = temp;
}
}

无语,都学到哪里去了?

Fight  to win  or  die...
2007-06-27 20:05
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
遍历.. 先比较再交换...
你用自己的方法试试? 用一个数组保存使用频率, 参考那个统计频率的帖子..
然后根据频率来重排..

女侠,约吗?
2007-06-27 20:19
快速回复:[求助]下面的帖子怎么才能改成按使用频率从大到小进行排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017907 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved