HDU_1000
题目:
A+B Problem
Calculate A + B.
Input
Each line will contain two integers A and B. Process to end of file.
Output
For each case, output A + B in one line.
Sample Input
1 1
Sample Output
2
这道题不算特别难,但有两个坑:
(1)A 和 B的范围;
(2)有几组A和B;(这个真的是特别坑人!)
要使用while循环,注意条件是cin>>A>>B,而不能是true,否则程序会陷入死循环,造成Output Limit Exceeded(超出输出限制)
#include <iostream>
using namespace std;
int main(){
long long A;
long long B;
while(cin>>A>>B)//可能有多行输入
cout<<A+B<<endl;
getchar();
return 0;
}
HDU_1001
题目:
Sum Problem
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + … + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
1
100
Sample Output
1
5050
这道题和之前做的蓝桥杯的序列求和差不多,只是输出格式不一样。
(1) 要注意n的范围,还有要考虑内存和运行时间。求和可以利用for或者while循环一个个相加(这个只适合n比较小时)也可以使用等差数列求和公式S=(n*(1+n))/2;(这个不论n是小还是大都可以);
(2)还有就是输出格式“follow a blank line”结果后面要有一个空白行,所以要换行两次,否则会出现“Presentation Error”(格式错误)。
(3)还有和上一道题一样的,没说明要输入的n的个数,要使用while循环,注意条件是cin>>A>>B,而不能是true,否则程序会陷入死循环,造成Output Limit Exceeded(超出输出限制)。
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
int main(){
ll n;
while(cin>>n){
cout<<(n*(1+n))/2<<endl<<endl;
}
getchar();
return 0;
}
菜鸟进阶,略略略。