练习5.14
#include <iostream>
#include <string>
using namespace std;
#include<typeinfo>
#include<cctype>
#include<vector>
int main()
{
vector<string> word;
string ch;
int cnt = 1;
int maxcnt = 0;
while (cin >> ch)
word.push_back(ch);
string prech;
for (auto i = word.begin();i!=word.end();i++) {
if (i == word.begin()) {
prech = *i;
continue;
}
if (prech==*i) {
cnt++;
}
else {
if(cnt > maxcnt)
maxcnt = cnt;
cnt = 1;
}
prech = *i;
}
cout << "最多重复出现:" << maxcnt << endl;
system("pause");
return 0;
}
在vs里面使用ctrl+z停止输入。
练习5.17,其中对于cin输入的清除参考了下其他博客,链接:https://blog.csdn.net/qq_40130835/article/details/78636271。
#include <iostream>
#include <string>
using namespace std;
#include<typeinfo>
#include<cctype>
#include<vector>
int main()
{
vector<int> num;
vector<int> num1;
int number;
while (cin >> number){
num.push_back(number);
}
cin.clear(); // 清除ctrl z 状态 让 cin能进行下去
while (cin >> number){
num1.push_back(number);
}
auto a = num.begin(), b = num1.begin();
for (; a!=num.end() && b!=num1.end(); a++,b++){
if ((*a)!=(*b)){
break;
}
}
if (a==num.end() || b==num1.end()){
cout << "其中有他的前缀" << endl;
}
else {
cout << "无前缀" << endl;
}
system("pause");
return 0;
}
练习5.19
#include <iostream>
#include <string>
using namespace std;
#include<typeinfo>
#include<cctype>
#include<vector>
int main()
{
string ch1, ch2;
char flag;
do {
cout << "请依次输入两个字符串序列。\n" << endl;
cout << "输入第一个字符串\n";
cin >> ch1;
cin.clear();
cout << "输入第二个字符串\n";
cin >> ch2;
cin.clear();
if (ch1.size() > ch2.size()) {
cout << ch2 << endl;
}
else {
cout << ch1 << endl;
}
cout << "输入是否退出";
cin >> flag;
} while (flag!='q');
system("pause");
return 0;
}
练习5.20
#include <iostream>
#include <string>
using namespace std;
#include<typeinfo>
#include<cctype>
#include<vector>
int main()
{
vector<string> ch;
string ch1;
int flag = 0;
while (cin >> ch1){
for (auto &i : ch) {
if ((i)==ch1){
flag = 1;
cout << "重复的字符为:" << ch1 << endl;
break;
}
}
ch.push_back(ch1);
if (flag==1){
break;
}
}
if (flag == 0){
cout << "无重复字符,为手动停止输入" << endl;
}
system("pause");
return 0;
}
练习5.21,使用string库里的函数进行多一个判断,并且使用continue
#include <iostream>
#include <string>
using namespace std;
#include<typeinfo>
#include<cctype>
#include<vector>
int main()
{
vector<string> ch;
string ch1;
int flag = 0;
while (cin >> ch1){
for (auto &i : ch) {
if ((i)==ch1){
if (islower(i[0])){
continue;
}
else {
flag = 1;
cout << "重复的大写字符为:" << ch1 << endl;
break;
}
}
}
ch.push_back(ch1);
if (flag==1){
break;
}
}
if (flag == 0){
cout << "无重复字符和第一个字母为大写的,为手动停止输入" << endl;
}
system("pause");
return 0;
}