u312233
  • 板块灌水区
  • 楼主封禁用户
  • 当前回复7
  • 已保存回复7
  • 发布时间2023/7/16 21:14
  • 上次更新2023/11/3 09:27:13
查看原帖
u312233
1028278
封禁用户楼主2023/7/16 21:14

这是朋友给的题,怎么RE了。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

string ans(string src, string another) {

	reverse(src.begin(), src.end());
	reverse(another.begin(), another.end());

	int n = src.length() + another.length();

	vector<int> temp;
	temp.resize(n);
	for (int i = 0; i < temp.size(); i++) {
		temp.at(i) = 0;
	}

	for (int i = 0; i < src.length(); i++) {
		for (int j = 0; j < another.length(); j++) {
			int tem = (src.at(i) - '0') * (another.at(j) - '0');
			temp.at(i + j) += tem;
			if (temp.at(i + j) > 9 && (i + j) < n - 1) {
				temp.at(i + j + 1) += temp.at(i + j) / 10;
				temp.at(i + j) = temp.at(i + j) % 10;
			}
			else if (temp.at(i + j) > 9 && (i + j) == n - 1) {
				temp.push_back(temp.at(i + j) / 10);
				temp.at(i + j) = temp.at(i + j) % 10;
			}
			temp.at(0) = temp.at(0);
		}
	}
	string returnString;
	returnString.resize(n);
	for (int i = 0; i < n; i++) {
		returnString[i] = temp[i] + '0';
	}
	reverse(returnString.begin(), returnString.end());
	int noZeroPos = returnString.find_first_not_of('0');

	if (noZeroPos != string::npos) {
		returnString.erase(0, noZeroPos);
	}
	else {
		returnString = "0";
	}
	return returnString;
}

void outPut(string a, int n) {
	string answerString;

	int dotPos = -1;
	int zeroNumble = 0;
	int integerNumble;
	int decimalNumble;

	dotPos = a.find_first_of('.');
	if (dotPos != string::npos) {
		a.erase(dotPos, 1);
	}

	integerNumble = dotPos;
	decimalNumble = a.size() - integerNumble;
	zeroNumble = a.find_first_not_of('0');

	answerString.push_back('1');
	for (int i = 0; i < n; i++) {
		answerString = ans(answerString, a);
	}

	int ansSize = answerString.size();
	int temp = decimalNumble * n;
	temp = temp;

	if (ansSize > decimalNumble * n) {
		int t = answerString.size() - n * decimalNumble;
		answerString.insert(ansSize - decimalNumble * n, 1, '.');

		int lastZeroPos = answerString.find_last_not_of('0');
		if (lastZeroPos < answerString.size() - 1) {
			answerString.erase(lastZeroPos + 1, answerString.size() - 1 - lastZeroPos);
		}

		if (lastZeroPos == t) answerString.erase(lastZeroPos, 1);
		cout << answerString;
	}
	else {
		cout << '.';
		for (int i = 0; i < decimalNumble * n - ansSize; i++) {
			cout << '0';
		}
		int lastZeroPos = answerString.find_last_not_of('0');
		if (lastZeroPos < answerString.size() - 1) {
			answerString.erase(lastZeroPos + 1, answerString.size() - 1 - lastZeroPos);
		}
		int t = answerString.size() - n * decimalNumble - 1;
		if (lastZeroPos == t) answerString.erase(lastZeroPos, 1);
		cout << answerString;
	}
	cout << endl;
}

int main() {
	string a;
	string in;
	int n;
	while (getline(cin, in)) {
		if (in.size() == 0) continue;
		string a = in.substr(0, 6);
		int n = atoi(in.substr(7, 2).c_str());
		outPut(a, n);
	}

	system("pause");
	return 0;}
2023/7/16 21:14
加载中...