原题连接: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

-1000000 9

Sample Output

-999,991

______________________________________________________________________________________________________________________________________________________
  
这道题难点在于如何三位输出,我是利用数组存放每一个数,然后倒序输出,当下标为3的倍数的时候,加入“,”,但有好几个测试点没通过……代码如下:
 #include<stdio.h>
#define Max 8 int main()
{
long a,b,sum;
int i,c[Max];
scanf("%d %d",&a, &b);
sum=a+b; if (sum<){printf("-");sum=-sum;}
i=;
c[i]=sum%;
sum/=;
while (sum>)
{
i++;
c[i]=sum%;
sum/=;
}
//printf("%d\n",i);
for (;i>=;i--)
{
printf("%d",c[i]);
if (i%==&&i!=)printf(",");
}
return ;
}

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

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

代码如下:

 #include<stdio.h>

 int main()
{
int a,b,sum;
scanf("%d %d",&a,&b);
int i;
sum=a+b;
if (sum<){printf("-");sum=-sum;}
if (sum>=)printf("%d,%03d,%03d",sum/,(sum/)%,(sum%));
else if (sum>=)printf("%d,%03d",sum/,sum%);
else printf("%d",sum);
return ;
}

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. STM32 IIC

    #include "Type.h" #include "IIC.h" #include "Delay.h" void I2C_Init(vo ...

  2. 解决eclipse端口占用问题

    在eclipse中开启tomcat服务器时报错:端口已被占用. 这是因为在tomcat开启的状态下,eclipse异常关闭,导致tomcat一直占用端口. 解决方法 在cmd窗口中输入命令-- net ...

  3. Centos挂载第二块硬盘

    作为一个初创小公司的架构师,工作内容纷繁复杂,涉及了系统管理员.数据库管理员.架构师.高级软件工程师.项目经理的部分.   今天的任务是安装公司的服务器,使用centos6.7.安装过程就不用细讲了. ...

  4. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  5. 剑指Offer-【面试题05:从头到尾打印链表】

    package com.cxz.question5; import java.util.Stack; /* * 输入个链表的头结点,从尾到头反过来打印出每个结点的值. * */ public clas ...

  6. Notepad++ 使用nppexec插件配置简易开发环境

    notepad++  采用nppexec插件来配置简易开发环境,而不需要笨重的IDE以及麻烦.重复的命令行.控制台输入: 以下为本人最近用到的脚本配置: //编程语言脚本中$(NAME_PART).x ...

  7. ppmoney 总结二

    1. return false   ES6函数的扩展:箭头函数  数组 arr.map()   arr.filter() <!DOCTYPE html> <html lang=&qu ...

  8. 比较完整的WebView的用法

    WebView, WebChromeClient和WebViewClient加载网页基本用法 webview是android中的浏览器控件,在一些手机应用中常会用到b/s模式去开发应用,这时webvi ...

  9. HDU 5754 Life Winner Bo 组合博弈

    Life Winner Bo Problem Description   Bo is a "Life Winner".He likes playing chessboard gam ...

  10. java中Class.forName("xxx")和ClassLoader().loadClass("xxx")的区别

    一.首先,查看Class类中的forName方法,可以发现有如下三个方法,但是我们通常用的是只有一个参数的方法. 简单介绍一下这三个方法: 第一个方法Class.forName("xxx&q ...