c++笔记
Using namespace std;
1 cout << "enter two numbers:" << endl; 2 int v1 = 0, v2 = 0; 3 cin >> v1 >> v2; 4 cout << "the sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl;
# the sum of 4 and 6 is 10
#输出变量v1,v2两边加空格,这样控制台输出结果才好看
#空格和回车 输入多个数
1 while (cin>>value) 2 { 3 sum += value; 4 } 输入任意字母,退出循环,控制台得出结果
这里只统计最后连续输出的值
心得:对于Cin输入流的用法不太熟悉
int v = 0,a,cnt; while (cin>>a) { if (a == v) { ++cnt; } else { cnt = 1; v = a; } } cout << v << " 出现了 " << cnt << " 次 " << endl;
1 int currVal = 0, newval = 0; 2 if (cin>>currVal) 3 { 4 int cnt = 1; 5 while (cin>>newval) 6 { 7 if (newval ==currVal) 8 { 9 ++cnt; 10 } 11 else 12 { 13 cout << currVal << " occurs " << cnt << " times " << endl; 14 currVal = newval; 15 cnt = 1; 16 } 17 } 18 cout << currVal << " occurs " << cnt << " times" << endl; 19 } 20 return 0;
正确统计输入中得每个值连续出现的次数