| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1109 人关注过本帖
标题:c++输入输出流求解
只看楼主 加入收藏
创世乔丹
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-11-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
c++输入输出流求解
在words.txt文件中包含了87314个单词,编写C++程序从words文件中读取单词,并输出重复字母对最多的单词,将第一个最多重复字母对的单词写入newwords.txt文件中。例如tooth这个单词有一个重复字母对,committee有三个重复字母对。
要求写注释。
搜索更多相关主题的帖子: 重复 单词 输出 字母 c++ 
2021-11-22 15:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
听不懂

什么叫“单词”,比如“I'm”算一个单词还是两个?
“例如tooth这个单词有一个重复字母对”------ 你的意思是只有相邻的相同字母才算是“重复字母对”?
例如“ooop”“www”算几个“重复字母对”?
2021-11-23 07:59
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int foo( const std::string& word )
{
    int count = 0;
    for( const char* p=word.c_str(); *p; ++p )
        count += *p == *(p+1);
    return count;
}

int main( void )
{
    int count_max = -1;
    string word_max;

    ifstream fin( "words.txt" );
    for( string word; fin>>word; )
    {
        int count = foo(word);
        if( count_max < count )
        {
            count_max = count;
            word_max = word;
        }
    }

    ofstream fout( "newwords.txt" );
    fout << word_max;
}
2021-11-23 08:16
快速回复:c++输入输出流求解
数据加载中...
 
   



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

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