前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【ZOJ】1001A + B Problem

【ZOJ】1001A + B Problem

作者头像
韩旭051
发布2019-11-08 01:03:02
5880
发布2019-11-08 01:03:02
举报
文章被收录于专栏:刷题笔记

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://cloud.tencent.com/developer/article/1535043

今天做PAT看见一个特别炫酷的一栏,就做了一道题。。。。

A + B Problem

Time Limit: 2000 msMemory Limit: 65536 KB

Calculate a + b

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.

Sample Input

代码语言:javascript
复制
1 5

Sample Output

代码语言:javascript
复制
6

Hint

Use + operator

Sample Program Here

答案先放出来

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
  int  a,b;
    while(cin>>a>>b)
    {printf("%d\n",a + b);
    }
    return 0;
}

我没看到 一系列输入所以以为是个大数计算,所以错了

特地整了个string类型的加法,当然瞎写的,暴力书写很烂还是没有过

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main(){
  string a,b;
  
  
  int c=a.length()>b.length()? b.length():a.length();
  int d=a.length()<b.length()? b.length():a.length();
  int count=0;
  string s;
  for(int i=0;i<d;i++){
  	if(a.length()>i){
  		count+=a[a.length()-i-1]-'0';
	  }if(b.length()>i){
	  	count+=b[b.length()-i-1]-'0';
	  }
  	char o=(count%10+'0');
  	s=o+s;
  	count=count/10;
  
  if(count>0){
  	s+=(count%10+'0');
  }
  cout<<s<<endl;
}
  return 0;
}

后来我看 别人答案 才意识到是有 a series of 就改成了,卡在while循环超时了

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
  int  a,b;
    while(scanf("%d %d",&a,&b))
    {printf("%d\n",a + b);
    }
    return 0;
}

换成cin就可以了

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
  int  a,b;
    while(cin>>a>>b)
    {printf("%d\n",a + b);
    }
    return 0;
}

后来又查了资料,scanf的返回值比较EOF就行了

scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。 因为浙大的oj是文件读入,那就判断EOF呗, 开始想的是读入错误应该是返回0,就跳出while了,但是仍然会卡在while (其实自己不用文件读写的时候都没有跳出while循环....)

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
  int  a,b;
    while(scanf("%d %d",&a,&b)!=EOF)
    {printf("%d\n",a + b);
	}
    return 0;
}

反正不管啥吧,随便看看还学了一个知识点+++

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 今天做PAT看见一个特别炫酷的一栏,就做了一道题。。。。
    • Input
      • Output
        • Sample Input
          • Sample Output
            • Hint
            • 答案先放出来
            • 我没看到 一系列输入所以以为是个大数计算,所以错了
              • 特地整了个string类型的加法,当然瞎写的,暴力书写很烂还是没有过
                • 后来我看 别人答案 才意识到是有 a series of 就改成了,卡在while循环超时了
                • 反正不管啥吧,随便看看还学了一个知识点+++
            • 换成cin就可以了
            • 后来又查了资料,scanf的返回值比较EOF就行了
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档