//p5_14.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string temp;
string strTemp = "\0"; //临时存储正在统计的单词
string strMax; //存储当前连续出现次数最多的单词
unsigned strCnt = 0, strTempCnt = 0; //strCnt当前连续出现最多次数,strTempCnt当前统计单词连续出现次数
while(cin >> temp)
{
if(strTemp == "\0" )
{
strTemp = temp;
++strTempCnt;
}
else if(strTemp == temp)
{
++strTempCnt;
}
else
{
if(strTempCnt >= strCnt )
{
strMax = strTemp;
strCnt = strTempCnt;
}
strTemp = temp;
strTempCnt = 1;
}
}
if(strCnt < 2)
cout << "have no word continuously appear!" << endl;
else
{
cout << strMax << " "<< strCnt << " times " << endl;
}
return 0;
}
5.17
//p5_17.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec1 = { 0, 1, 2 };
vector<int> ivec2 = { 0, 1, 1, 2, 3, 5, 8 };
//int temp;
//while(cin >> temp) //想通过交互式的读取数据,但是每次只有第一个while循环能够成功读取,第二个while循环就只接跳过了
// ivec1.push_back(temp); //有什么解决方案,请不吝赐教
//while(cin >> temp)
// ivec2.push_back(temp);
auto n = (ivec1.size() < ivec2.size()) ? ivec1.size() : ivec2.size();
decltype(ivec1.size()) i = 0;
for (; i < n; ++i)
{
if (ivec1[i] != ivec2[i])
break;
}
bool isPrefix = (i == n) ? true : false;
if (isPrefix)
cout << "true" << endl;
else
{
cout << "false" << endl;
}
return 0;
}