LeetCode刷题计划
推荐 代码随想录:https://github.com/youngyangyang04/leetcode-master
卡码网 练习ACM模式 https://kamacoder.com/
01
#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().split())print(a+b)except EOFError:break
注意:
- try和except配套用
- python输入是使用a = int(input())
- 输入一行,使用map(int,input().split())
02
#include <bits/stdc++.h>
using namespace std;int main()
{int a, b, n;while(cin>>n){for (int i = 0; i < n; ++i) {cin >> a >> b;cout << a + b << endl;}}return 0;
}
try:while True:n = int(input()) # 输入行数for _ in range(n):a, b = map(int, input().split())print(a + b)
except EOFError:pass # 捕获EOFError异常,结束循环
注意:
- #include <bits/stdc++.h>是万能头文件
- python的单个循环可使用for _ in range(n):
03
#include <bits/stdc++.h>int main(){int a ,b ;while(true){std::cin>>a>>b;if(a==0&&b==0)break;else std::cout<<a+b<<std::endl;}return 0;
}
while True:try:a,b = map(int,input().split())if a==b==0:breakelse:print(a+b)except EOFError:pass
04
#include <iostream>
using namespace std;int main(){while(true){int n;cin>>n;if (n==0)break;else{int a[n];int sum=0;for(int i=0;i<n;++i){cin>>a[i];sum = a[i]+sum;}cout<<sum<<endl;}
}return 0;
}
while True:ls=input().split()if int(ls[0]) == 0:breakelse:nums = list(map(int,ls[1:]))ans = sum(nums)print(ans)
注意:
- python的列表 nums = list(map(int,ls[1:]))
- 可以使用sum函数
05
#include <iostream>
using namespace std;int main()
{int a,b;while(cin>>a>>b){cout<<a+b<<endl;cout<<endl;}return 0;
}
try:while True:a, b = map(int, input().split())print(a + b)print()
except EOFError:pass
注意:
- C++输出空行:cout<<endl;
- Python输出空行:print()或者print(’ ')
06
#include <iostream>
using namespace std;int main() {int N;cin >> N; // 读取N的值for (int i = 0; i < N; ++i) {int M;cin >> M; // 读取M的值int sum = 0;for (int j = 0; j < M; ++j) {int num;cin >> num; // 读取M个整数sum += num; // 累加每个整数}cout << sum << endl; // 输出M个整数的和if (i != N - 1) {cout << endl; // 每组输出之间输出一个空行,但最后一组不输出空行}}return 0;
}
while True:try:n = int(input())while n > 0:n -= 1 nums = list(map(int,input().split()))ans = sum(nums[1:])print(ans)if n != 0:print()except:break
注意:
- 使用数组记得要先定义n,m并且获得输入,再定义数组。