原题连接:https://www.patest.cn/contests/pat-a-practise/1001

题目如下:

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<stdio.h>
  2. #define Max 8
  3.  
  4. int main()
  5. {
  6. long a,b,sum;
  7. int i,c[Max];
  8. scanf("%d %d",&a, &b);
  9. sum=a+b;
  10.  
  11. if (sum<){printf("-");sum=-sum;}
  12. i=;
  13. c[i]=sum%;
  14. sum/=;
  15. while (sum>)
  16. {
  17. i++;
  18. c[i]=sum%;
  19. sum/=;
  20. }
  21. //printf("%d\n",i);
  22. for (;i>=;i--)
  23. {
  24. printf("%d",c[i]);
  25. if (i%==&&i!=)printf(",");
  26. }
  27. return ;
  28. }

_________________________________________________希望大神能帮我看看我的错误在哪了……

在网上参考了其他人的解法,是直接对sum这个数选取,利用了技巧:取一个数sum(假设该数共有a位)的前n位的时候,用sum/10的(a-n)次方;取它的后n位的时候,用sum%10的n次方,然后就是分情况讨论,因为题中所给a,b是在区间【-1000000,1000000】,将|sum|分为大于1000000,小于1000000而大于1000,大于等于0小于1000的。

代码如下:

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int a,b,sum;
  6. scanf("%d %d",&a,&b);
  7. int i;
  8. sum=a+b;
  9. if (sum<){printf("-");sum=-sum;}
  10. if (sum>=)printf("%d,%03d,%03d",sum/,(sum/)%,(sum%));
  11. else if (sum>=)printf("%d,%03d",sum/,sum%);
  12. else printf("%d",sum);
  13. return ;
  14. }

1001. A+B Format (20)的更多相关文章

  1. 1001.A+B Format (20)代码自查(补足版)

    1001.A+B Format (20)代码自查(补足版) 谢谢畅畅酱的提醒,发现了代码中的不足,把变量名更改成更合理的名字,并且把注释也换成英文啦! 栋哥提供的代码自查的方式也帮助了我发现很多代码中 ...

  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) 2016-09-09 22:47 25人阅读 评论(0) 收藏

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

  5. 关于‘1001.A+B Format (20)’的解题报告

    1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...

  6. "1001. A+B Format (20)" 解题报告

    Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...

  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-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】

    题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...

  9. 1001. A+B Format (20) (%0nd)

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

  10. PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

随机推荐

  1. docker容器与容器云读书笔记1

    搭建docker应用栈 操作系统: ubuntu 16.04.1 LTS 桌面版 1. 准备工作 换网易源, gedit 会报一个metadata的告警, 不用理会, sudo apt-get upd ...

  2. Android selecter背景选择器使用

    android:drawable这个属性是必须的,默认时的背景图片. android:state_pressed布尔值.true指当用户点击或者触摸该控件的状态.默认为false android:st ...

  3. sh6.脚本磁盘分区格式化

    练习1. 写一个脚本,通过ping 命令测试192.168.0.100到192.168.0.254之间的所有主机是否在线, 如果在线,就显示"ip is up."IP为真实IP地址 ...

  4. webservices接口 file &quot;/axis2-web/listsingleservice.jsp&quot; not found 问题解决

    搞了半天 ,原来是services.xml  配置的某个或者某些service 在代码中不存才.扫描的时候找不到对应的service代码所以就会报错

  5. for_each(c++11)

    http://www.cplusplus.com/reference/algorithm/for_each/ template<class InputIterator, class Functi ...

  6. HP-SOCKET TCP/UDP通信框架库解析

    项目概述: HP-SOCKET是一套通用TCP/UDP通信框架,包括服务器.客户端.Agent组件:其目标是提供高性能.通用性.简易性.可扩展.可定制: 鉴于此,其仅实现基本的通用框架通信.数据收发功 ...

  7. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  8. Linux下设置Mysql数据库编码

    重装系统后,要装个mysql服务器,发现添加数据时,一直错误,查其原因,原来时编码问题.Google了一下,可都不太一样,可能时数据库版本问题吧. 查看数据库版本: select version(); ...

  9. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  10. 2016 Google中国开发者大会游记

    本文地址:http://www.cnblogs.com/likeli/p/6146117.html 写在前面 平时一直埋头写代码,这次既然Google给了门票,也就来看看,看看这种世界顶尖的科技公司, ...