problem

1001 A+B Format (20)(20 point(s))
Calculate a + b and output the sum in standard format -- 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. 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. Sample Input -1000000 9
Sample Output -999,991

anwser

#include <iostream>
int main(){
int a, b;
int c;
int end[10];
std::cin>>a>>b;
c = a+b;
if (c==0) {
std::cout<<"0";
return 0;
}
if (c<0) {
std::cout<<"-";
c = -c;
}
int index = 0;
while(c>0){
end[index++] = c%1000;
c/=1000;
}
for(int i = index - 1; i >= 0; i--){
if(i != index-1){
if(end[i] == 0)
std::cout<<"000";
if(end[i] > 99)
std::cout<<end[i];
else if(end[i] > 9){
std::cout<<"0";
std::cout<<end[i];
}
else if (end[i] > 0){
std::cout<<"00";
std::cout<<end[i];
}
}
else
std::cout<<end[i];
if (i != 0)
std::cout<<",";
}
return 0;
}

experience

  • 边界条件要注意的

    • c ==0
  • 除首个数字外 end[i] == 0 的情况,并且要分类讨论
  • 首个数字不需要特殊处理

PAT 1001 Format的更多相关文章

  1. PAT 1001 A+B 解题报告

    PAT 1001 A+B 代码链接:传送门 题目链接:传送门 题目简述: 给定两个值a,b: 范围-1000000 <= a, b <= 1000000: 按指定格式输出a+b的结果,例: ...

  2. PAT 1001. A+B Format 解题

    GitHub PDF 1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, t ...

  3. pat 1001 A+B Format

    题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...

  4. PAT 1001 A+B Format (20分) to_string()

    题目 Calculate a+b and output the sum in standard format -- that is, the digits must be separated into ...

  5. PAT 1001. A+B Format(水题)

    #include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...

  6. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  7. PAT 1001 A+B Fotmat

    源码 1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calcula ...

  8. PAT 1001

    Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...

  9. PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)

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

随机推荐

  1. 【BZOJ】2655: calc 动态规划+拉格朗日插值

    [题意]一个序列$a_1,...,a_n$合法当且仅当它们都是[1,A]中的数字且互不相同,一个序列的价值定义为数字的乘积,求所有序列的价值和.n<=500,A<=10^9,n+1< ...

  2. 2016-2017-20155329 《Java程序设计》第7周学习总结

    学号 2016-2017-20155329 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治标准时间(GMT时间) 世界时(UT) 国际原子时(TAI) 世界协调 ...

  3. 在EF6.0中打印数据库操作日志

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. python正则表达式-re模块的爱恨情仇

    利用python的re模块,使用正则表达式对字符串进行处理 # 编辑者:闫龙 import re restr = "abccgccc123def456ghi789jgkl186000&quo ...

  5. 手动刷入Android 4.4.1 KOT49E OTA更新包

    一.Android 4.4 KitKat Google前段时间发布了Android新版本Android 4.4 KitKat,由于我的Nexus 4也是托朋友从US带回来的,所以很快就收到了Googl ...

  6. 大规模实时流处理平台架构-zz

    随着不同网络质量下接入终端设备种类的增多,服务端转码已经成为视频点播和直播产品中必备的能力之一.直播产品讲究时效性,希望在一定的时间内让所有终端看到不同尺寸甚至是不同质量的视频,因此对转码的实时性要求 ...

  7. O_NONBLOCK与O_NDELAY有何不同?

    O_NONBLOCK和O_NDELAY所产生的结果都是使I/O变成非搁置模式(non-blocking),在读取不到数据或是写入缓冲区已满会马上return,而不会搁置程序动作,直到有数据或写入完成. ...

  8. Jenkins忘记用户名密码

    一.进入C盘.jenkins配置文件中找到config.xml 需要删除一下内容: <useSecurity>true</useSecurity> <authorizat ...

  9. 数据库中INFORMATION_SCHEMA的说明及使用

    第一个查询看看库里有多少个表,表名等select * from INFORMATION_SCHEMA.TABLES information_schema这张数据表保存了MySQL服务器所有数据库的信息 ...

  10. django 建立一个简单的应用

    本人的用的版本是python 2.7.3和django 1.10.5,Windows10系统 1.首先通过命令建立项目和app 找到django的安装路径,我的路径是:C:\Python27\Lib\ ...