Sy_Faker 2019-12-29 22:59 采纳率: 25%
浏览 352

PTA:进阶练习1001 A+B Format

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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −10
​6
​​ ≤a,b≤10
​6
​​ . The numbers are separated by a space.

Output Specification:
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

大概意思应该是两个数相加的和后有逗号的输出吧

程序清单:

#include<stdio.h>
int main()
{
    int a,b,fu=0,num[1000];
    scanf("%d %d",&a,&b);
    int sum=a+b;
    if(sum==0)printf("0");
    if(sum<0)
    {
        fu=1;
        sum=-sum;
    }
    int i=0;
    while(sum!=0)
    {
        num[i++]=sum%10;
        sum/=10;
    }
    i--;
    if(fu)printf("-");
    int count=0;
    for(;i>=0;i--)
    {
        printf("%d",num[i]);
        count++;
        if(count%3==0&&i!=0)printf(",");
    }
}

有挺多错误的,我看网上的代码也不知道自己什么情况没考虑到,请教~
图片说明

  • 写回答

1条回答 默认 最新

  • 菜鸟小白! 2022-02-27 16:17
    关注

    我和你样,这几个测试点过不去,你现在知道原因了吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题