PAT甲级1001 (C语言描述)遇到问题

PAT甲级1001 (C语言描述)遇到问题
新手菜鸟希望大佬指点一下


问题描述

pat甲级1001

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.

代码如下

#include <stdio.h>
#include <math.h>

int main(){
    long long int a,b;
    scanf("%lld %lld",&a,&b);
    long long int c = a+b,d;
    d = abs(c);
    int cnt = 0;
    while(d > 0){
    	d = d/10;
    	cnt++;
	}
	int zi = 0;
	if(c < 0){
		c = -c;
		printf("-");
	}
	while(1){
		if(cnt - 3 >= 0){
			zi = pow(10,cnt - 3);
            if(c/zi > 99){
                printf("%lld",c/zi);    
            }else if(c/zi > 9){
                printf("0%lld",c/zi);
            }else {
                printf("00%lld",c/zi);
            }
			
			c = c - c/zi*pow(10,cnt - 3);
			cnt = cnt-3;
        }else{
        	if(cnt == 2){
        		if(c > 9){
	                printf("%lld",c);    
	            }else{
	                printf("0%lld",c);
	            }
			}else {
                printf("%lld",c);
            }
			cnt = cnt-3;
        }
        if(cnt > 0){
        	printf(",");
		}else{
			break;
		}
    }
    return 0;
}

提交结果
在这里插入图片描述
也尝试了1000 10000 100000等数据,现在不清楚错在哪里,希望有大佬可以帮忙看一下。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值