PAT甲级 1073(C++)

一次就AC,太好了!!!

#include<iostream>
#include<string>
using namespace std;
int main() {
	string sc; cin >> sc;
	int pos = 1;
	string result = "";
	string number = "";
	int index = 0;
	if (sc[0] == '-')
		result += '-';
	while (sc[index] != 'E') {
		if(isdigit(sc[index]))
			number += sc[index];
			index++;
	}
	int positive = sc[++index] == '+';
	string e = ""; index++;
	while (index < sc.size()) {
		e += sc[index];
		index++;
	}
	int ex = stoi(e);
	if (positive == 0) {
		result += "0.";
		for (int i = 1; i <= ex - 1; i++) result += '0';
		result += number;
	}
	else {
		pos += ex;
		if (pos < number.size()) {
			for (int i = 0; i < number.size(); i++) {
				if (i == pos) result += '.';
				result += number[i];
			}
		}
		else {
			int add = pos - number.size();
			result += number;
			for (int i = 1; i <= add; i++) result += '0';
		}
	}
	cout << result;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45681165/article/details/121065250