《C++ Primer》第15页,我的第一个有点逻辑的C++代码:
#include <iostream>
int main()
{
// a是正在统计的数,读入的新值存入b
int a = 0, b = 0;
// 读入第一个数,确保有数据可处理
if (std::cin >> a)
{
int cnt = 1;
while (std::cin >> b)
{
if (b == a)
{
++cnt;
}
else
{
std::cout << a << " occurs " << cnt << " times." << std::endl;
a = b;
cnt = 1;
}
}
std::cout << a << " occurs " << cnt << " times." << std::endl;
}
return 0;
}
好好学习,加油!cpp是世界上最好的语言!!!