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等数据,现在不清楚错在哪里,希望有大佬可以帮忙看一下。