-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifSentence.cpp
More file actions
33 lines (30 loc) · 883 Bytes
/
ifSentence.cpp
File metadata and controls
33 lines (30 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// ifSentence.cpp
// C++PrimerExercise
//
// Created by hp on 2017/6/9.
// Copyright © 2017年 hp. All rights reserved.
//
#include <iostream>
using namespace std;
int main(){
//currNum是当前正在统计的数,num是读入的新值
int currNum = 0, num = 0;
//读取第一个数,并且确保不为空
if (cin >> currNum) {
int count = 1;//保存正在处理的当前值的个数
while (cin >> num) {
if (num == currNum) {
count++ ;
}
else{
cout << currNum << " occurs "<< count << " times "<<endl;
currNum = num;//记住新值
count = 1;//重置计数器
}
}
//记住打印文件中最后一个值的个数
cout << currNum << " occurs "<< count << " times " << endl;
}
return 0;
}