时间限制

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. laravel安装 笔记

    http://laod.cn/hosts/2015-google-hosts.html 谷歌FQIP laravel安装和设置流程 1安装composer , VirtualBox和Vagrant 下 ...

  2. Webdriver实现下载功能,屏蔽掉windows弹出的对话框,FireFox下设置浏览器的属性,两种实现方式:

    一.使用一个全新的FireFox浏览器打开Web应用,浏览器不带任何插件,也未对浏览器做任何默认配置,但需要对浏览器属性进行配置 // 获取浏览器的所有配置文件 ProfilesIni allProf ...

  3. CentOS的KVM实践(虚拟机创建、网桥配置、Spice)

    最近公司准备上一套基于openstack的虚拟桌面系统,作为该项目的负责人,觉得有必要自己实践一下,该系统的搭建.最基础的就是需要了解基于linux的kvm的实践. 一.基础软件包准备 系统是采用px ...

  4. A. Brain's Photos ——Codeforces Round #368 (Div. 2)

    A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. OC-之AFNetworking与ASIHTTPRequest对比

    一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession 2.ASI的底层实现基于纯C语言的CFNetwork框架 3.因为NSURLConnection和 ...

  6. js去除字符串中所有html标签及&nbsp符号

    近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...

  7. ios用xib实现三等分以及多等分思路

    Auto Layout 的本质原理 Auto Layout 的本质是用一些约束条件对元素进行约束,从而让他们显示在我们想让他们显示的地方. 约束主要分为以下几种(欢迎补充): 相对于父 view 的约 ...

  8. Windows 8/7下还原系统默认扩展名打开方式类型

    在百度知道上如果你搜“改回选错的打开方式”,看到的大多数都是XP系统的方法,不管是批处理还是别的方法,但适用于Windows 8/7的只有修改注册表的方法. 因为Windows 7你也就根本找不到[工 ...

  9. how computer boot up?

    The power button activates the power supply in the PC, sending power to the motherboard and other co ...

  10. 硬盘安装win8系统方法汇总

    从硬盘安装 (推荐)硬盘安装方法一 使用Nt6 hdd installer进行安装,此方法适合XP,vista, Windows 7等系统. 下载Nt6 hdd installer(Win8硬盘安装工 ...