初学C++ RE求助
查看原帖
初学C++ RE求助
571249
Passthem楼主2021/10/14 12:29

本地测试通过,本地上样例是输出正确的,请问RE的原因是什么。

#include <iostream>
using namespace std;

struct node {
	node* left;
	node* right;
	int val;
	int getLength() {
		int ret = 0;
		if (this->left) {
			ret += this->left->getLength();
		}
		if (this->right) {
			ret += this->right->getLength();
		}
		return ret + 1;
	}
	int smaller(int num) {
		if (this->val >= num) {
			if (this->left) {
				return this->left->smaller(num);
			}
			return -2147483647;
		} else {
			if (this->right) {
				return max(this->right->smaller(num), this->val);
			}
			return this->val;
		}
	}
	int bigger(int num) {
		if (this->val > num) {
			if (this->left) {
				return max(this->left->bigger(num), this->val);
			}
			return this->val;
		} else {
			if (this->right) {
				return this->right->bigger(num);
			}
			return 2147483647;
		}
	}
	int index(int ind) {
		if (this->left) {
			int llength = this->left->getLength();
			
			if (llength < ind) {
				return this->right->index(ind - llength);
			} else if (llength == ind) {
				return this->val;
			} else {
				return this->left->index(ind);
			}
		} else if (ind == 0) {
			return this->val;
		}
		return this->right->index(ind - 1);
	}
	int findNumber(int number) {
		if (number == this->val) {
			if (this->left) {
				return this->left->getLength();
			}
			return 0;
		} else if (number < this->val) {
			if (this->left) {
				return this->left->findNumber(number);
			}
		} else if (number > this->val) {
			if (this->right) {
				if (this->left) {
					return this->right->findNumber(number) + this->left->getLength() + 1;
				}
				return this->right->findNumber(number) + 1;
			}
		}
	}
	int push(int val) {
		if (val < this->val) {
			if (this->left) {
				this->left->push(val);
			} else {
				this->left = new node();
				this->left->val = val;
				this->left->left = NULL;
				this->left->right = NULL;
			}
		} else {
			if (this->right) {
				this->right->push(val);
			} else {
				this->right = new node();
				this->right->val = val;
				this->right->left = NULL;
				this->right->right = NULL;
			}
		}
	}
};


int main() {
	int n;
	cin >> n;
	int code, inp;
	node* root = new node();
	bool initial = false;
	
	for (int i=0; i<n; i++) {
		cin >> code >> inp;
		if (code == 5) {
			if (initial) {
				root->push(inp);
			} else {
				root = new node();
				root->val = inp;
				root->left = NULL;
				root->right = NULL;
				initial = true;
			}
		} else if (code == 1) {
			cout << root->findNumber(inp) + 1 << endl;
		} else if (code == 2) {
			cout << root->index(inp - 1) << endl;
		} else if (code == 3) {
			cout << root->smaller(inp) << endl;
		} else if (code == 4) {
			cout << root->bigger(inp) << endl;
		}
	}
	
	return 0;
}

2021/10/14 12:29
加载中...