PAT-B 1017. A除以B

1. 字符串模拟大数除法

2. 测试点1考察000000 / x的情况,此时应输出0 0


代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string a;
	int b, now=0;
	bool non_zero = false;
	cin >> a >> b;

	for (size_t i = 0; i < a.size(); ++ i)
	{
		now = 10*now + a[i] - 48;
		if (non_zero == true)
		{
			cout << now/b;
		} else if (now/b != 0)
		{
			cout << now/b;
			non_zero = true;
		}
		now %= b;
	}
	if (non_zero == false)
	{
		cout << 0;
	}
	cout << " " << now;

	return 0;
}




你可能感兴趣的:(C++,PAT-B)