C++ Primer-if语句

统计在输入中每个值连续出现了多少次:

 1 #include <iostream>
 2 int main()
 3 {
 4     int currval = 0,val = 0;
 5     if (std::cin >> currval){
 6         int cnt = 1;
 7         while (std::cin>>val){
 8             if (currval == val)
 9                 ++cnt;
10             else {
11                 std::cout<<currval<<" occurs "<<cnt<<" times."<<std::endl;
12                 currval = val;
13                 cnt = 1;
14             }
15         }
16             std::cout<<currval<<" occurs "<<cnt<<" times."<<std::endl;
17     }
18     system("pause");
19     return 0;
20 }
View Code

格式:

1 if (){
2     ...
3 }
4 else {
5     ...
6 }
View Code

 

posted @ 2018-08-24 10:46  archerzon  阅读(310)  评论(0编辑  收藏  举报