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

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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助