problem

  1. 1001 A+B Format (20)(20 point(s))
  2. 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).
  3. Input
  4. 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.
  5. Output
  6. 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.
  7. Sample Input
  8. -1000000 9
  9. Sample Output
  10. -999,991

anwser

  1. #include <iostream>
  2. int main(){
  3. int a, b;
  4. int c;
  5. int end[10];
  6. std::cin>>a>>b;
  7. c = a+b;
  8. if (c==0) {
  9. std::cout<<"0";
  10. return 0;
  11. }
  12. if (c<0) {
  13. std::cout<<"-";
  14. c = -c;
  15. }
  16. int index = 0;
  17. while(c>0){
  18. end[index++] = c%1000;
  19. c/=1000;
  20. }
  21. for(int i = index - 1; i >= 0; i--){
  22. if(i != index-1){
  23. if(end[i] == 0)
  24. std::cout<<"000";
  25. if(end[i] > 99)
  26. std::cout<<end[i];
  27. else if(end[i] > 9){
  28. std::cout<<"0";
  29. std::cout<<end[i];
  30. }
  31. else if (end[i] > 0){
  32. std::cout<<"00";
  33. std::cout<<end[i];
  34. }
  35. }
  36. else
  37. std::cout<<end[i];
  38. if (i != 0)
  39. std::cout<<",";
  40. }
  41. return 0;
  42. }

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. HDU1693 Eat the Trees(zerojudge a228)

    传送门: https://zerojudge.tw/ShowProblem?problemid=a228 http://acm.hdu.edu.cn/showproblem.php?pid=1693 ...

  2. 【leetcode 简单】 第五十六题 快乐数

    编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如 ...

  3. backup服务器之rsync服务

    backup服务器之rsync服务   rsync是开源的.快速的.多功能的可实现全量及增量的本地或远程数据同步备份的优秀工具.它拥有scp.cp的全量复制功能,同时比scp.cp命令更优秀.更强大. ...

  4. 34、Collections工具类简介

    Collections工具类简介 就像数组中的Arrays工具类一样,在集合里面也有跟Arrays类似的工具类Collections package com.sutaoyu.Collections; ...

  5. 【CTF MISC】pyc文件反编译到Python源码-2017世安杯CTF writeup详解

    1.题目 Create-By-SimpleLab 适合作为桌面的图片 首先是一张图片,然后用StegSolve进行分析,发现二维码 扫码得到一串字符 03F30D0A79CB0558630000000 ...

  6. 解决 Windows 环境 Git Bash 无法识别 Composer 命令的问题

    思路 模拟 Linux,复制一个 composer 文件到 Git Bash 的 /usr 的子目录,并赋予执行权限. 解决 首先,请确定你的 composer.phar 文件路径.我的是: /d/w ...

  7. HDU 6199 2017沈阳网络赛 DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6199 题意:n堆石子,Alice和Bob来做游戏,一个人选择取K堆那么另外一个人就必须取k堆或者k+1 ...

  8. Unity3D Instantiate慢的问题

    1.NGUI直接打开界面卡 2.角色放技能的时候卡 3.载入模型的时候卡 http://www.xuanyusong.com/archives/2925

  9. [java笔记]父类设计法则

    1.父类通常情况下都设计为抽象类或接口,其中优先考虑接口,如接口不能满足才考虑抽象类. 2.一个具体的类尽可能不去继承另一个具体类,这样的好处是无需检查对象是否为父类的对象.

  10. 如何修改SQL Server 2000的数据库逻辑与物理名称

    在项目中使用SQL Server 2000创建了一个数据库,发现名称与另一个数据库太相似,于是决定更改名称,包括: 在企业管理器中看到的数据库名,也是实际应用程序中连接用的数据库名称: 在磁盘上看的物 ...