时间限制

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的更多相关文章

  1. 浙大PAT-1001

    1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...

  2. (甲)PAT-1001

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B   Calculate a + b and output the sum ...

  3. 随笔2 PAT1001.A+B Format (20)

    1001.A+B Format(20) 题目链接 1001.A+B Format (20) C++ 代码 第一次使用markdown,还不是很习惯,现在努力的在适应它 首先这道题我们很容易就可以读懂题 ...

  4. PAT---1001. A+B Format (20)

    #include<stdio.h> int main(){ //定义要读入的两个整数 int a,b; //定义好放拆项用的数组 ]; //读入两个整数 scanf("%d%d& ...

  5. PAT1001 A+B Format

    思路:每三位分割,注意符号,首位不要出现逗号. AC代码 #include <stdio.h> #include <algorithm> using namespace std ...

  6. PAT-1001 采花生

    题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”. 鲁宾逊先生和多多都很开心,因为花生正是他 ...

  7. PAT----1001. A+B Format (20)解题过程

    1001. A+B Format (20) github链接 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output t ...

  8. pat1001. Battle Over Cities - Hard Version 解题报告

    /**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...

  9. PAT1001~1005AC代码

    晚上了,睡不着觉,做CF把,太累了,那就来几道乙级的编程小题吧. 1001.卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这 ...

随机推荐

  1. python [1:3]

    Python下标是以0开始的x[1:3]表示返回集合中下标1至3(不包括3)的元素集合x[:3] 表示返回从开始到下标3(不包括3)的元素集合x[3:]表示返回从下标3到结束的元素集合X[:]表示返回 ...

  2. 《JavaScript高级程序设计》读书笔记 ---执行环境及作用域

    执行环境及作用域 执行环境(execution context,为简单起见,有时也称为“环境”)是JavaScript 中最为重要的一个概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自 ...

  3. apt-get 总结

    转自: apt-get 总结 1.apt-get install <package_name> install a new package. 2.apt-get build-dep < ...

  4. hibernate对象的状态以及生命周期

    瞬时状态:session中没有,数据库中没有 持久状态:session中有,数据库中有 游离状态:session中没有,数据库中有 get和load都是用来提取数据的 get和load的区别: get ...

  5. hdu_5221_Occupation(树剖)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5221 题意:给你一棵树,每个节点有一定的值,有三种操作: 1 x y 表示占领树上x-y的所有节点,2 ...

  6. permutation求全排列

    include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...

  7. 公司用中会用到的iOS开源库和第三方组件(不断更新...)

    分享一些目前我个人接触到的一些第三方组件和开源的库, 感谢开源, 减少了我们的开发成本, 节约了我们大量的时间, 让我们有更多的时间和精力专注做我们自己的产品.总有没有接触过的 , 总有你会用到的 , ...

  8. Javascript调用 ActiveXObject导出excel文档。

    function makeDataBook(){ var xls = new ActiveXObject ("Excel.Application"); xls.visible = ...

  9. MSSQL 字符串XML 合成列

    declare @str varchar(2000) set @str='1,2,3,4,6,8,5,9,10,11,12,13,14,15,16,17,18,19,20,29,30,31,32,33 ...

  10. onPostCreate——Activity彻底运行起来之后的回调

    记得之前想要在Activity布局完成,彻底跑起来之后,再获取当前Activity的窗口中,某个View的宽高,之前用的办法很土,弄个Handler,发个Message出来,使用sendMessage ...