场景1.求a+b的和
#include <iostream>
using namespace std;
int main() {
int a,b;
while(cin >> a >> b)//注意while处理多个case
cout << a+b << endl;
}
输入3行,其中第一行为N,其他两行为数据
int main(){
int N;
cin >> N;
vector<int> A;
vector<int> B;
int x;
while(cin>>x)
{
A.push_back(x);
if(getchar()=='\n')
break;
}
while(cin>>x)
{
B.push_back(x);
if(getchar()=='\n')
break;
}
return 0;
}
场景2.给出N阶方阵所有的数,求方阵中所有数的和
Input:
3
1 2 3
2 1 3
3 2 1
Output:
18
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
//freopen("1.in","r",stdin);
int n,ans = 0;
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int x; scanf("%d",&x);
ans += x;
}
}
cout << ans << endl;
return 0;
}
3.输入字符串
#include<iostream>
using namespace std;
int main()
{
char buf[ 255 ];
while(cin.getline( buf, 255 ));
}
getline
用法
string s;
getline(cin,s);
getline会忽略行末的空格
4.输入不说明有多少组数据,但以某个特殊输入为结束标志
#include<iostream>
using namespace std;
int main()
{
int a ,b;
while(cin>>a>>b&&(a||b))
{cout<<a+b<<endl;}
return 0;
}