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,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这 ...
随机推荐
- MFC下MCI的使用播放音乐
最近研究了一下MFC下的音乐的播放,主要使用了MCI 1.需要包含的库文件 在链接资源里(link)添加库文件VFW32.lib winmm.lib 2.包含的头文件 #include <mms ...
- 如何用Navicat for MySQL 将mysql中的数据库导出,导入。
数据库导出 1.双击要导出的数据库,右键选转储SQL文件...,选择要保存的文件夹. 2.点击开始后,开始导出. 数据库导入 1.新建数据库,数据库的名字必须和导入的数据库文件一致. 2.在新建的数据 ...
- hdu 5524 二叉树找规律,二进制相关
input n 1<=n<=1e18 output 有n个结点的满二叉树有多少个不相同结点数的子树 做法:树有h=log2(n)层,最多有2h-2种(1除外),然后再n减去u重复的即可 # ...
- java泛型小问题
几年前当Java5还未正式发布的时候,看到过一些人写的介绍Tiger中的新特性,当时对我第一感觉冲击最大的就是泛型(generics)和注释(annotation),因为它们直接影响了我们编码的语法习 ...
- cmstop传递什么控制器和方法---就实例化该控制器
object顶级类class object 第一级抽象类controllerabstract class controller extends object 第二级抽象类controller_abst ...
- 【Machine Learning in Action --1】机器学习入门指南
摘自:http://www.jianshu.com/p/c3634a7f2320 机器学习算法 Coursera 上面 Stanford 的 机器学习 课程是优质的算法相关入门课程.Andrew Ng ...
- Javascript调用 ActiveXObject导出excel文档。
function makeDataBook(){ var xls = new ActiveXObject ("Excel.Application"); xls.visible = ...
- js获取url传递参数,js获取url?号后面的参数
方法一.正则表达式 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...
- WdatePicker默认日期为当天
不说废话,直接上代码: <input id="d4311" class="Wdate" onfocus="WdatePicker({maxDat ...
- remote staspack
输入文件:$HOME/utility/statspack11g/statspack.env, $HOME/utility/statspack11g/dblist,$HOME/utility/stats ...