在线编程常见输入输出

在线编程常见输入输出

题目链接

1. 输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组,输出a+b的结果

#include <iostream>
using namespace std;
int main(){
int a,b;
while (cin >> a >> b){
cout << a+b << endl;
}
return 0;
}
while True:
try:
# a, b = map(int, input().strip().split(' ')) # 以空格为分割符
a, b = eval(input().strip()) # 以逗号为分割符
print(a+b)
except:
break

2. 输入第一行包括一个数据组数t(1 <= t <= 100) 、接下来每行包括两个正整数a,b(1 <= a, b <= 1000)

示例:

2
1 5
10 20
6
30
n = eval(input().strip())
data = []
for i in range(n):
in_data = list(map(int, input().strip().split()))
data.append(in_data)
for i in range(n):
print(data[i][0] + data[i][1])
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<vector <int>> datas;
vector<int> data(2, 0);
for (int i=0; i<n; i++){
cin >> data[0] >> data[1];
datas.push_back(data);
}
for (int i=0; i<datas.size(); i++){
cout << datas[i][0] + datas[i][1] << endl;
}
return 0;

3. 输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

1 5
10 20
0 0
6
30
while True:
try:
a,b = map(int, input().strip().split(' '))
if a==0 and b==0: break
print(a + b)
except:
break
#include <iostream>
using namespace std;
int main(){
int a,b;
while (cin >> a >> b){
if (a==0 && b==0)
break;
cout << a+b << endl;
}
return 0;
}

4. 输入数据包括多组、每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入、接下来n个正整数,即需要求和的每个正整数

4 1 2 3 4
5 1 2 3 4 5
0
10
15
#include <iostream>
using namespace std;
int main(){
int n;
while (cin >> n){
if (n==0) break;
int sum=0, num;
for (int i=0; i<n; i++){
cin >> num;
sum += num;
}
cout << sum << endl;
}
return 0;
}
while True:
res = 0
n = list(map(int, input().strip().split()))
if n[0]==0: break
res = sum(n) - n[0]
print(res)

5. 输入数据有多组, 每行表示一组输入数据,每行不定有n个整数,空格隔开。(1 <= n <= 100)

1 2 3
4 5
0 0 0 0 0
6
9
0
#include <iostream>
using namespace std;
int main(){
int n;
int sum = 0;
while (cin >> n){
sum += n;
if (cin.get() == '\n'){
cout << sum << endl;
sum = 0;
}
}
return 0;
}
posted @   rain-1227  阅读(227)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示