第一个测试点,visual studio2022可以过,但上机是WA,大佬救命
查看原帖
第一个测试点,visual studio2022可以过,但上机是WA,大佬救命
393850
liaosa楼主2022/1/21 15:13

为什么第一个和第三个测试点过不了……其他都能过

下载了第一个测试集的数据,visual studio2022可以过,但上机是WA,是我的输出格式有问题吗。。。

用的是C++

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

int f(string a, int i,bool* flag);

int main() {
	string a;
	cin >> a;
	int length = a.length();
	int location = 1;
	int coefficient1 = 0, coefficient2 = 0;
	double x;
	bool* flag = new bool;
	char ch;
	for (int i = 0; i < length; i++) {
		if (a[i] == '=')location = -1;
		if (a[i] >= 'a' && a[i] <= 'z') {
			if (i == 0 || a[i - 1] == '+' || a[i - 1] == '=') {
				coefficient2 -= location;
			}
			if (a[i - 1] == '-') coefficient2 += location;
		}
		if (a[i] <= '9' && a[i] >= '0') {
			if (i == length - 1 || a[i + 1] == '-' || a[i + 1] == '+' || a[i + 1] == '=') {
				int ans = f(a, i, flag);
				if (*flag) {
					coefficient1 += ans * location;
				}
				else coefficient1 -= ans * location;
			}
			if (a[i + 1] <= 'z' && a[i + 1] >= 'a') {
				int ans = f(a, i, flag);
				if (*flag) {
					coefficient2 -= ans * location;
				}
				else coefficient2 += ans * location;
				ch = a[i + 1];
			}
		}
	}
	x = double(coefficient1) / coefficient2;
	cout << ch << '=';
	cout << setiosflags(ios::fixed) << setprecision(3) << fixed << x << endl;
	return 0;
}

int f(string a, int i, bool* flag) {
	if ((i == 0 && (a[i] > '9' || a[i] < '0')) && a[i] != '-' || a[i] == '+' || a[i] == '=') {
		*flag = true;
		return 0;
	}
	if (a[i] == '-') {
		*flag = false;
		return 0;
	}
	if (i == 0 && a[i] <= '9' && a[i] >= '0') {
		return int(a[i] - '0');
	}
	int ans = f(a, i - 1, flag);
	ans = ans * 10 + int(a[i] - '0');
	return ans;
}
2022/1/21 15:13
加载中...