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,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这 ...
随机推荐
- javascript动画效果之多物体透明度
html和css 仅为布局,需要注意的是filter对应的是老版本的ie浏览器透明度,而opacity对应的其他浏览器的透明度 filter: alpha(opacity: 50); opacity: ...
- ural 1355. Bald Spot Revisited(数的素因子划分)
1355. Bald Spot Revisited Time limit: 1.0 secondMemory limit: 64 MB A student dreamt that he walked ...
- Foundations of Computer Science
1, Iteration, Induction and Recursion 2, the running time of program 3, combinatorics and probabilit ...
- 解决Eclipse无法添加Tomcat服务器的问题
eclipse配置好以后,如果Tomcat服务器在文件系统的位置发生了变化,则需要重新配置Tomcat服务器,这时会遇到无法设置服务器的问题 即图中框起来的部分无法进行操作,这时需要 关闭Eclips ...
- JQuery 多选按钮checkbox
JQuery 多选按钮checkbox 在需要全选和选择部分的时候我们就需要多选在这里主要介绍了具体的实现 JQuery $(function () { //全选或全不选 $("#allbo ...
- BFS and Queue
BFS (Bridth First Search) can be implemented by a queue. Procedure is like this: (Q is Queue) 1, Put ...
- utf8 文件 错误保存为gbk 中文乱码 解决方法
用zend studio 将utf-8 格式的文件 保存为 gbk 了,之后无论怎么装换 中文都是乱码 用 beyond compare(文件比较工具 对编码支持的比较强大) 打开,改下编码,中文就 ...
- codeforce div2 C 树状数组
http://codeforces.com/contest/362 题目大意:给你一个序列,用冒泡排序法让他变为非递减的序列最少需要几次.在冒泡交换之间,你有一个swap操作,该swap操作是交换任意 ...
- PS:抠图方法1(利用对比度ctrl+l)
PS:抠图方法1(利用对比度ctrl+l) 工具/原料 Photoshop.美女照片 方法/步骤 小编使用的是Photoshop cs5版本,大家使用其他版本都没有关系,界面略有不同,但操 ...
- go share library
http://blog.ralch.com/tutorial/golang-sharing-libraries/ Sharing Golang packages to C and Go Sun, Au ...