PAT1001. A+B Format(20)

最近开始每天做一道PAT练手,下面是PAT1001的题目。

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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

根据这个题目,初步想到可以直接用数字做,也可以用字符串来做。那么试试直接用数字做。首先a+b的和在-2^31和2^31-1之间,所以可以用int表示两者之和。然后每三位用一个逗号,我就想到对和用1000取余,因为这样取余出来是最后的三位,那么我就借助栈后入先出的特点,将对1000取余的后的结果先放入栈中,这样最后输出时就是正确的顺序了。初步写成的代码如下:

#include <iostream>
#include <stack>
using namespace std;

int main()
{
	int a, b, sum, num;
	stack<int> s;
	while (cin >> a >> b)
	{
		sum = a + b;

		do
		{
			num = sum % 1000;
			s.push(num);
			sum /= 1000;
		} while (sum != 0);

		cout << s.top();
		s.pop();
		while (!s.empty())
		{
	               cout << "," << s.top();
                       s.pop();
                }		
	}
	return 0;
}

但经过测试,因为负数对1000取余后还是负数,这样对于负数进行上述操作后每一部分都会是负的,如下图所示:

所以要解决这个问题,在一开始可以先判断和是否为负,为负直接输出负号,然后变为正数即可。代码如下:

#include <iostream>
#include <stack>
using namespace std;

int main()
{
	int a, b, sum, num;
	stack<int> s;
	while (cin >> a >> b)
	{
		sum = a + b;

		if (sum < 0)
		{
			cout << "-";
			sum = -sum;
		}
			
		do
		{
			num = sum % 1000;
			s.push(num);
			sum /= 1000;
		} while (sum != 0);

		cout << s.top();
		s.pop();
		while (!s.empty())
		{
			cout << "," << s.top();
			s.pop();
		}		
	}
	return 0;
}
但经过测试发现,比如能整除1000的数,对1000取余后则为0,就会将0输出而不是我们需要的000,测试结果如下:


所以对栈顶元素进行了特判,如果为0,则输出000,更改后代码如下:

#include <iostream>
#include <stack>
using namespace std;

int main()
{
	int a, b, sum, num;
	stack<int> s;
	while (cin >> a >> b)
	{
		sum = a + b;

		if (sum < 0)
		{
			cout << "-";
			sum = -sum;
		}
			
		do
		{
			num = sum % 1000;
			s.push(num);
			sum /= 1000;
		} while (sum != 0);

		cout << s.top();
		s.pop();
		while (!s.empty())
		{
			if (s.top() != 0)
				cout << "," << s.top();
			else
				cout << ",000";
			s.pop();
		}		
	}
	return 0;
}
测试几组数据没问题后再PAT里进行测试,发现有两个测试点不对:


仔细思考了下,其实和上面0是一样的问题,比如三个数是一位数,那么按我的代码,只会输出1位而不会前面补0,两位数也类似,所以再次在输出的时候进行了判断,代码如下:

#include <iostream>
#include <stack>
using namespace std;

int main()
{
	int a, b, sum, num;
	stack<int> s;
	while (cin >> a >> b)
	{
		sum = a + b;

		if (sum < 0)
		{
			cout << "-";
			sum = -sum;
		}
			
		do
		{
			num = sum % 1000;
			s.push(num);
			sum /= 1000;
		} while (sum != 0);

		cout << s.top();
		s.pop();
		while (!s.empty())
		{
			if (s.top() > 100)
				cout << "," << s.top();
			else if (s.top() >= 10)
				cout << ",0" << s.top();
			else
				cout << ",00" << s.top();
			s.pop();
		}		
	}
	return 0;
}
这次测试后就所有用例都对了。


这里我想,如果直接用c语言的printf,那么用类似printf("%03d",sum);的语句就可以直接补前缀0了。c++也可以通过setw和setfill的方法来直接填充前缀0,代码如下:

#include <iostream>
#include <stack>
#include <iomanip>
using namespace std;

int main()
{
	int a, b, sum, num;
	stack<int> s;
	while (cin >> a >> b)
	{
		sum = a + b;

		if (sum < 0)
		{
			cout << "-";
			sum = -sum;
		}
			
		do
		{
			num = sum % 1000;
			s.push(num);
			sum /= 1000;
		} while (sum != 0);

		cout << s.top();
		s.pop();
		while (!s.empty())
		{
			
			cout << ",";
			cout << setw(3) << setfill('0') << s.top();  // 设置域宽为3,填充字符为0
			s.pop();
		}		
	}
	return 0;
}

这样测试也是正确的。

去网上查阅后倒是发现了一种很取巧的方法:

#include<stdio.h>
  
  int main()
  {
     int a,b,sum;
     scanf("%d %d",&a,&b);
     int i;
     sum=a+b;
     if (sum<0){printf("-");sum=-sum;}
     if (sum>=1000000)printf("%d,%03d,%03d",sum/1000000,(sum/1000)%1000,(sum%1000));
     else if (sum>=1000)printf("%d,%03d",sum/1000,sum%1000);
     else printf("%d",sum);
     return 0;
  }
因为给定的数字范围其实不是很大,通过这种方法分三类直接讨论,也能得到正确的结果。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值