forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving formatting and style of 5.14.
- Loading branch information
Showing
1 changed file
with
19 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,33 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
using std::cout; | ||
using std::string; | ||
using std::cin; | ||
using std::cout; | ||
using std::endl; | ||
using std::string; | ||
|
||
int main() | ||
{ | ||
string str1; | ||
string tempstr, laststr; | ||
int count = 0, tempCount = 0; // 计数 count | ||
string pre_word, word, max_repeat_word; | ||
int repeat_times = 0, max_repeat_times = 0; | ||
|
||
while (cin >> tempstr) | ||
{ | ||
if (tempCount == 0) | ||
laststr = tempstr; | ||
if (tempstr == laststr) | ||
++tempCount; | ||
else | ||
{ | ||
laststr = tempstr; | ||
tempCount = 1; // 次数置为1 | ||
while (cin >> word) { | ||
if (word == pre_word) { | ||
++repeat_times; | ||
} else { | ||
repeat_times = 1; | ||
pre_word = word; | ||
} | ||
if (tempCount > count) | ||
{ | ||
count = tempCount; | ||
str1 = tempstr; | ||
|
||
if (max_repeat_times < repeat_times) { | ||
max_repeat_times = repeat_times; | ||
max_repeat_word = pre_word; | ||
} | ||
} | ||
|
||
if (count == 1) | ||
cout << "All words are single!" << endl; | ||
else | ||
cout << "\"" << str1 << "\" appears " << count << " times" << endl; | ||
|
||
return 0; | ||
if (max_repeat_times <= 1){ | ||
cout << "no word was repeated" << endl; | ||
} else { | ||
cout << "the word '" << max_repeat_word << "' occurred " << max_repeat_times << " times" << endl; | ||
} | ||
} |