Github仓库的链接

https://github.com/deepYY/object-oriented/blob/master/A-B-Format/A.B.Format.c

解题思路:

  • 输入a,b,并求a与b 之和c

  • 将c分为没有逗号和有1个逗号和有两个逗号

  • 用除和求余的方法求c的各个三位数,在各个三位数之间加上逗号并输出

bug的发现和修改过程:

  • 问题1:调试的过程中,逗号后面还存在负数

对1001. A+B Format (20)的描述

#include<stdio.h>
int main()
{	
	int a, b, c;
	scanf("%d %d",&a,&b);
	c = a + b;		
	if (c >= 1000000 || c <= -1000000)	{printf("%d,%d,%d", c / 1000000, (c / 1000) % 1000, c % 1000);}		
	else if (c >= 1000 || c <= -1000)     {printf("%d,%d", c / 1000, c % 1000);		}	
	else    {printf("%d", c);}	
	return 0;
}
  • 修改:c为负数取余时余数也为负数,我就修改先将c取正在输出数之前加个负号

  • 问题2:输出正数时,位数少了许多,有些零不见了

对1001. A+B Format (20)的描述

#include<stdio.h>
int main()
{	
	int a, b, c;
	scanf("%d %d",&a,&b);
	c = a + b;	
	if (c < 0){		
	printf("-");
	c = -c;	}	
	if (c >= 1000000)	{printf("%d,%d,%d", c / 1000000, (c / 1000) % 1000, c % 1000);}		
	else if (c >= 1000)     {printf("%d,%d", c / 1000, c % 1000);		}	
	else    {printf("%d", c);}	
	return 0;
}
  • 修改:输出时加上%03d 在不足三位数时补上零

PAT的截图

对1001. A+B Format (20)的描述

相关文章:

  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-12
  • 2021-11-30
  • 2021-09-30
  • 2022-01-12
相关资源
相似解决方案