3.23算法学习(A+B练习一)

A+B input&output练习一

practice1

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

simple input

1 5
10 20

simple output

6
30

分析:

第一次看到这个题目,心想很简单嘛,就是定义两个数a,b 然后求它们的和a+b,于是有了以下代码:

#include<stdio.h>
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d",a+b);
}

但是不对!!!

题目中说的是要输入一些列的整型数(a series of pairs of integers a and b)

如果按照以上代码,仅仅只能算一个a和一个b的和!!!

于是我修改成了以下的代码:

#include<stdio.h>
int main()
{
    int a,b;
    while(scanf("%d",&a)&&scanf("%d",&b))
    {
        printf("%d",a+b);
        printf("\n");
    }
  
  return 0;
}

经过编译器运行之后没有问题,哈哈哈冲冲冲,提交到vj上!

但是结果:

傻眼了!!

Time Limit Exceeded 运行时间超出了

网上查找原因后,发现我这个代码存在一个问题:没有终止循环的条件

百度说,下面这个方法可以有效的终止循环

while(scanf()!=EOF)

再次修改后的代码:

#include<stdio.h>
int main()
{
    int a,b;
    while(scanf("%d",&a)!=EOF&&scanf("%d",&b)!=EOF)
    {
        printf("%d",a+b);
        printf("\n");
    }
  
  return 0;
}

再次提交vj后通过啦!

总结:

  • 首先要审题,这道题说的是要输入一系列的a和b,所以要用到循环

  • EOF是一个计算机术语,为End Of File的缩写,在操作系统中表示资料源无更多的资料可读取。资料源通常称为档案或串流。通常在文本的最后存在此字符表示资料结束。

    用EOF可以有效的终止循环

其它:

我觉得下面这个代码很不错哦~

#include<iostream>

using namespace std;

int main()
{
	int x,y;
	
	while(cin>>x && cin>>y){
	cout<<x+y;
	printf("\n");
	}

	
	return 0;
}

practice2

Your task is to Calculate a + b.

input

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.

output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

sample input

2
1 5
10 20

sample output

6
30

分析:

这次还是输入一系列a和b,但是有了一个特别重要的改动,就是最前面要输入一个数,表明要进行几次a+b运算。

(相当于我们在玩儿一个游戏,首先要输入一个数字来表示你要玩几局,玩好那几局就结束游戏去学习奥~)

我一开始的代码:

#include<stdio.h>
int main()
{
    int n,a,b;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        while(scanf("%d",&a)!=EOF&&scanf("%d",&b)!=EOF)
        {
            printf("%d",a+b);
            printf("\n");
        }
    }
    return 0;
    
}

存在一个问题,就是比如说n为2时,应该是输入两组a和b后就结束,但是居然还能再输,更神奇的是,vj上居然通过了,但是这样一定是有问题的!

再次修改后,加入了break,得到了想要的结果

#include<stdio.h>
int main()
{
    int n,a,b;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        while(scanf("%d",&a)!=EOF&&scanf("%d",&b)!=EOF)
        {
            printf("%d",a+b);
            printf("\n");
            break;
        }
    }
    return 0;
    
}

总结:

  • 套用两个循环就可以达到“设定游戏局数”的效果

  • 借这个机会,我也复习了以下continue语句和break语句的区别

    continue:在C语言中的作用是跳过此语句下面的语句,然后继续循环,并不是跳出循环。

    break:跳出当前循环(像上面的代码,跳出当前循环的效果就是返回了外面一层的循环)

practice3

Your task is to Calculate a + b.

input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

sample input

1 5
10 20
0 0

sample output

6
30

分析:

这道题还是要输入一系列的a和b,但是结束的方式变成了输入两个0(意思时当你不想玩这个游戏了,就输入a和b都为0,就结束游戏)

这次我非常顺利,一遍就过啦!

#include <stdio.h>
int main()
{
    int a, b;
    while (scanf("%d", &a) != EOF && scanf("%d", &b) != EOF)
    {
        if (a != 0 || b != 0)
        {
            printf("%d", a + b);
            printf("\n");
        }else{
            return 0;
        }
    }
    return 0;
}

总结:

  • 加一个if判断语句即可 (a != 0 || b != 0) 只要有一个不为0就可以向下执行a+b的语句

practice4

Your task is to Calculate the sum of some integers.

input

Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

sample input

4 1 2 3 4
5 1 2 3 4 5
0 

sample output

10
15

分析:

这道题乍一看类似practice2,然后我一头闷地去敲代码,然后后面发现不对!它与第二题不类似!!!

第一个数是要累加的数有几个的意思,

当输入0时结束。

因为第一个数为后面累加的数的总数,所以感觉不算a+b的问题了,应该算后面所有数的累加的问题。

所以我决定使用数组,代码如下:

#include <iostream>
using namespace std;
int main()
{
    int n, i, a[100], sum = 0;
    while (cin >> n)
    {
        if (n != 0)
        {
            while (n--)
            {
                cin >> a[n];
                sum += a[n];
            }
            cout << sum;
            sum = 0;
            cout << endl;
        }else{
            return 0;
        }
    }

    return 0;
}

又是一遍过啦!

分析:

  • 审题审题还是审题!
  • sum = 0;这条语句不能落了,因为一次结束sum要重新初始化为0,不然的话sum的值会一直加下去,就不对了。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值