PAT1001
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
Calculate a + b and output the sum in standard format
计算a+b并且输出标准形式的和
-- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
形式就是,必须把数字用逗号分隔成三个一组(除非少于四位数)
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000.
对于每一个输入文件包含一个测试用例。每个测试用例包含一对数a和b,-1000000 <= a, b <= 1000000.
The numbers are separated by a space.
数字用空格分开
Output
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
对于每个测试用例,你需要在一行中输出ab的和。这个和必须以标准的形式被写出。
Sample Input
-1000000 9
Sample Output
-999,991
简单题目,主要问题在于格式。
然后有两个比较好的技巧,一个是提前处理0,防止除数为0。还有一个是提前处理负数的请求。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm> using namespace std; int main()
{
char ch[];//用于存放最终输出的数据
int a,b,c;
int i=,j=;
cin>>a>>b;
c=a+b;
//提前处理0的请求
if(c==)
{
cout<<;
}
else
{
//化负数为正数,提前处理请求
if(c < )
{
cout<<"-";
c=-c;
}
while (c>=)
{
ch[i] = c% + '';
c/=;
i++;
//用J来记录访问位数为3的逗号
if((i-j)% == )
{
ch[i] = ',';
j++;
i++;
}
}
ch[i] = c + ''; //循环输出最后的结果
for (;i>=;i--)
cout<<ch[i];
}
return ;
}
网上还看到一个比较取巧的方法。
#include<stdio.h>
int main()
{
int a,b;
int sum;
while(scanf("%d%d\n",&a,&b) != EOF){
sum = a+b;
if(sum < ){
printf("-");
sum = -sum;
}
if(sum>=){
printf("%d,%03d,%03d\n",sum/, (sum/)%, sum%);
}
else if(sum >= ){
printf("%d,%03d\n",sum/,sum%);
} else{
printf("%d\n", sum);
}
}
return ;
}
PAT1001的更多相关文章
- 浙大PAT-1001
1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...
- (甲)PAT-1001
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output the sum ...
- 随笔2 PAT1001.A+B Format (20)
1001.A+B Format(20) 题目链接 1001.A+B Format (20) C++ 代码 第一次使用markdown,还不是很习惯,现在努力的在适应它 首先这道题我们很容易就可以读懂题 ...
- PAT---1001. A+B Format (20)
#include<stdio.h> int main(){ //定义要读入的两个整数 int a,b; //定义好放拆项用的数组 ]; //读入两个整数 scanf("%d%d& ...
- PAT1001 A+B Format
思路:每三位分割,注意符号,首位不要出现逗号. AC代码 #include <stdio.h> #include <algorithm> using namespace std ...
- PAT-1001 采花生
题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”. 鲁宾逊先生和多多都很开心,因为花生正是他 ...
- PAT----1001. A+B Format (20)解题过程
1001. A+B Format (20) github链接 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output t ...
- pat1001. Battle Over Cities - Hard Version 解题报告
/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...
- PAT1001~1005AC代码
晚上了,睡不着觉,做CF把,太累了,那就来几道乙级的编程小题吧. 1001.卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这 ...
随机推荐
- <meta http-equiv="X-UA-Compatible" content="IE=Edge">
1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...
- jquery的隐藏
HTML 代码:<form> <input type="text" name="email" /> <input type=&qu ...
- POJ 3416 Crossing
树状数组+离线操作 #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...
- 辨别 ShopEX Ecshop
御剑可以识别ShopEX 或者 Ecshop 特征 ShopEX : 蓝色的icon js里有很多Cookie. <link rel="stylesheet" href=&q ...
- ASP.NET弹出提示点击确定之后再跳转页面的方法
//ASP.NET弹出提示点击确定之后再跳转页面的方法 //弹出了提示并且通过location.href转到了DeskTop.aspx页面 Response.Write("<scrip ...
- simHash 简介以及java实现
http://gemantic.iteye.com/blog/1701101 simHash 简介以及java实现 博客分类: 算法 simHash java 去重 传统的hash 算法只负责将原始 ...
- marzullo's algorithm
given several intervals, how to find a interval which is a intersect of the most number of the given ...
- AngularJs多重视图和路由的使用
使用AngularJs来做多重视图和路由是在方便不过了,在开发过程中,都有许多的页面,而这些页面都有相同的部分,比如页面的头部和尾部通常都是一样的,变化的都是主体部分,还有就是一些后端管理的一些项目, ...
- 上传下载文件, 同时部署在webapps下, 而不是项目下,防止重新部署tomcat, 上传文件消失
前端上传 <a href='javascript:upload("+data[i].id+")' title='Upload Report'> <img src= ...
- isr
Kafaka动态维护了一个同步状态的副本的集合(a set of in-sync replicas),简称ISR