https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

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

  1. -1000000 9

Sample Output

  1. -999,991
  2.  
  3. 代码:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1e5 + 10;
  5. char s[maxn], out[maxn];
  6. int a[maxn];
  7.  
  8. void zlr_itoa(int x) {
  9. if(x == 0) {
  10. s[0] = '0';
  11. s[1] = 0;
  12. return ;
  13. }
  14. int top = 0;
  15. while(x) {
  16. a[top ++] = x % 10;
  17. x = x / 10;
  18. }
  19. int sz = 0;
  20. while(top != 0) {
  21. s[sz++] = (char)(a[-- top] + '0');
  22. s[sz] = 0;
  23. }
  24. }
  25.  
  26. int main() {
  27. int a, b;
  28. scanf("%d%d", &a, &b);
  29. int sum = a + b;
  30. if(sum == 0) {
  31. printf("0\n");
  32. return 0;
  33. }
  34. if(sum < 0) {
  35. printf("-");
  36. sum = sum * (-1);
  37. }
  38.  
  39. zlr_itoa(sum);
  40. int len = strlen(s);
  41.  
  42. if(len < 3) {
  43. printf("%s\n", s);
  44. return 0;
  45. }
  46.  
  47. if(len % 3 == 0) {
  48. for(int i = 0; i < len - 3; i += 3) {
  49. for(int j = i; j < i + 3; j ++)
  50. printf("%c", s[j]);
  51. printf(",");
  52. }
  53. printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
  54. }
  55.  
  56. else if(len % 3 == 1) {
  57. printf("%c,", s[0]);
  58. for(int i = 1; i < len - 4; i += 3) {
  59. for(int j = i; j < i + 3; j ++)
  60. printf("%c", s[j]);
  61. printf(",");
  62. }
  63. printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
  64. }
  65.  
  66. else if(len % 3 == 2) {
  67. printf("%c%c,", s[0], s[1]);
  68. for(int i = 2; i < len - 5; i += 3) {
  69. for(int j = i; j < i + 3; j ++)
  70. printf("%c", s[j]);
  71. printf(",");
  72. }
  73. printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
  74. }
  75.  
  76. printf("\n");
  77. return 0;
  78. }

  

PAT 甲级 1001 A+B Format的更多相关文章

  1. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

  2. PAT 甲级 1001 A+B Format (20)(20 分)

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

  3. PAT 甲级1001 A+B Format (20)(C++ -思路)

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

  4. PAT甲级 1001. A+B Format (20)

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

  5. PAT甲级——1001 A+B Format (20分)

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

  6. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

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

  7. 【PAT】1001. A+B Format (20)

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

  8. PAT Advanced 1001 A+B Format (20 分)

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

  9. PAT甲级1001水题飘过

    #include<iostream> using namespace std; int main(){ int a, b; while(scanf("%d%d", &a ...

随机推荐

  1. 20155211 Exp1 PC平台逆向破解(5)M

    20155211 Exp1 PC平台逆向破解(5)M 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入 ...

  2. vue-cli打包后,图片路径不对

    在config文件夹下的 index.js 里面,查找build,在builid方法里面,添加一行:assetsPublicPath: './' 例: 在build文件夹下的utils.js里面,查找 ...

  3. c++ 分配与释放内存

    教学内容: calloc分配内存 calloc与malloc的区别 memset函数初始化内存 free释放动态分配的内存 一.calloc函数分配内存 void *calloc( size_t nu ...

  4. Velocity学习2

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  5. 软考计算机网络原理之IP计算问题汇总

    转自 http://www.cnblogs.com/jyh317/archive/2013/04/14/3018650.html 1.IP地址 分类: ①A类IP地址 ②B类IP地址 ③C类IP地址 ...

  6. BZOJ 2395 [Balkan 2011]Time is money

    题面 题解 将\(\sum_i c_i\)和\(\sum_i t_i\)分别看做分别看做\(x\)和\(y\),投射到平面直角坐标系中,于是就是找\(xy\)最小的点 于是可以先找出\(x\)最小的点 ...

  7. P4427 [BJOI2018]求和

    P4427 [BJOI2018]求和 同[TJOI2018]教科书般的扭曲虚空 懒得写了(雾 #include<bits/stdc++.h> #define il inline #defi ...

  8. [JLOI2013]地形生成[组合计数]

    题意 \(n\) 元素各有一个高度 \(h\) 和关键数字 \(b\) .求有多少个下标序列和高度序列,满足对任意 \(i\),\(j< i\) 且 \(h_j < h_i\)的 \(j\ ...

  9. java并发编程——Excutor

    概述 Excutor这个接口用的不多,但是ThreadPoolExcutor这个就用的比较多了,ThreadPoolExcutor是Excutor的一个实现.Excutor体系难点没有,大部分的关键点 ...

  10. iOS逆向+越狱

    感觉本文涉及内容有点多的,但是自己不愿意写太多,就简单的谢谢关于ios上手的东西吧 初级入手不免要用到,pp助手,i4 tools等 iOS逆向-ipa包重签名及非越狱手机安装多个应用 1.常识 我们 ...