您的位置:首页 > 其它

HDU 1.1.2解题报告

2015-03-08 11:20 357 查看
第二个A+B问题,稍微进行了改动,以下是大致题意。

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

表达的意思大致是,输入的第一行是一个整数N,代表你要做加法的次数,然后后面的N行就是N个A+B

这样的话就要思考,怎么样控制计算A+B的次数和你的输入的N有关了,想到用循环来处理。

#include <iostream>
using namespaces std;
int main()
{
int n,a,b;
cin>>n;
while(n&&cin>>a>>b)
{cout<<a+b<<endl;
--n;}
return 0;
}
结果是显而易见的,输入N之后,利用while来控制,条件是n&&cin>>a>>b,cin>>a>>b保证a和b一直在输入,而这个n,实际上应该是n!=0的简写,我在每一次输出a+b之后,进行--n操作,将n减1,这样当n减至0时,循环结束,而刚好进行a+b的次数就等于n。这样,就实现了题目的要求



如果用c语言呢?

#include <stdio.h>
int main()
{
int a,b,n;
scanf("%d",&n);
while(n&&scanf("%d%d",&a,&b)!=EOF)
{printf("%d\n",a+b);
n--;
}
return 0;
}
道理是一样的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息