标签:count 20 Format int sum item printf -- 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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. 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, sum, count=0;
int item[100];
count = 0;
scanf("%d%d", &a, &b);
sum = a+b;
if (sum<0){
sum = -sum;
printf("-");
}
while(sum){
item[count++]= sum%1000;
sum/=1000;
}
printf("%d", item[--count]);
while(count>0){
printf(",%03d", item[--count]);
}
printf("\n");
return 0;
}
结果在第四个测试点死活过不去,甚至通过二分查找的方式撞出了第四个测试点的数据(2与-2)
结果为0,但是本地上和pta测试出现差异的地方来了,pta输出结果为-2,本地显示为0
发现当sum为0时,打印的是item[-1],也就是b的位置(机组)
所以结果为0的时候就会输出b的值
本地不会出现这个问题可能是经过优化(不确定)
附上修改过后的代码
#include<stdio.h>
main(){
int a, b, sum, count;
int item[100];
scanf("%d%d", &a, &b);
sum = a+b;
count = 0;
// 添加代码
if(!sum) printf("0");
//
else{
if (sum<0){
sum = -sum;
printf("-");
}
while(sum){
item[count++]= sum%1000;
sum/=1000;
}
printf("%d", item[--count]);
while(count>0){
printf(",%03d", item[--count]);
}
printf("\n");
}
return 0;
}
标签:count,20,Format,int,sum,item,printf,--,1001 来源: https://www.cnblogs.com/Uni-/p/15063908.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。