1308:【例1.5】高精除

1308:【例1.5】高精除_第1张图片

C++代码示例如下

#include
#include
#include
#include
using namespace std;
string sub(string s, string st2) {
	vector A, B, C;
	for (int i = s.size() - 1; ~i; i--) A.push_back(s[i] - '0');
	for (int i = st2.size() - 1; ~i; i--) B.push_back(st2[i] - '0');
	int t, la = A.size(), lb = B.size();
	for (int i = 0; i < la; i++) {
		t = A[i];
		if (i < lb) t -= B[i];
		if (t < 0) t += 10, A[i + 1]--;
		C.push_back(t % 10);
	}
	string str;
	while (C.size() > 1 && !C.back()) C.pop_back();
	for (int i = C.size() - 1; ~i; i--) str += C[i] + '0';
	return str;
}
bool cmp(string s, string st2) {
	if (s.size() != st2.size())
		return s.size() > st2.size();
	for (int i = 0; i < s.size(); i++) {
		if (s[i] != st2[i])
			return s[i] > st2[i];
	}
	return true;
}
string div(string st1, string st2, vector& C) {
	string s;
	for (int i = 0; i < st1.size(); i++) {
		s += st1[i];//s>st2 -> true
		int cnt = 0;
		while (cmp(s, st2) && cnt < 10) {
			s = sub(s, st2);
			cnt++;
		}
		C.push_back(cnt);
	}
	reverse(C.begin(), C.end());
	while (C.size() > 1 && !C.back()) C.pop_back();
	return s;
}
int main() {
	string st1, st2, st;
	vector C;
	cin >> st1 >> st2;
	st = div(st1, st2, C);
	for (int i = C.size() - 1; ~i; i--) cout << C[i];
	cout << '\n' << st;
	return 0;
}

你可能感兴趣的:(奥赛一本通,c++,算法,开发语言)