您的位置:首页 > 移动开发 > 微信开发

编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.

2016-09-27 22:17 701 查看
// test13.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
vector<string> vec;
vector<int> strCnt;
vector<string> strName;
string str;
string temp;
int num = 0;
int max=0,flag=0;

while (cin>>str)
{
vec.push_back(str);

str.clear();

if (getchar() == '\n')
{
break;
}
}
for (auto it = vec.begin(); it != vec.end();it++)
{
cout << *it <<  endl;
}

temp = *vec.begin();
for (auto it = vec.begin(); it != vec.end(); it++)
{
if (it + 1 == vec.end())
{
num++;
strName.push_back(temp);
strCnt.push_back(num);
}

else if (temp == *it)
num++;
else
{
strName.push_back(temp);
strCnt.push_back(num);
temp = *it;
num = 1;

}
}

for (int i = 0; i < strName.size();i++)
{
cout << strName[i] << ":" << strCnt[i] << endl;
}
max = *strCnt.begin();
for (int i=0; i<strCnt.size(); i++)
{
if (max < strCnt[i])
{
max = strCnt[i];
flag = i;
}
}
cout << "连续出现的字符串的名称是:" << strName[flag] << " " <<"出现次数是:"<< strCnt[flag] << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐