HDU_1000 AND HDU_1001

本文详细介绍了解决ACM编程竞赛中典型问题的方法,包括如何使用C++进行A+B问题的计算,以及如何通过等差数列求和公式解决序列求和问题。文章强调了正确处理输入数据的重要性,避免陷入死循环,同时提供了有效的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
} 

菜鸟进阶,略略略。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值