用陣列來實作堆疊,得有empty, top, push, pop,size這些功能;
輸入:第一行輸入指令數(一共要輸入多少條指令),第二行開始 輸入指令
左邊是樣例輸入,右邊是樣例輸出

本地vs運行是沒有問題的,然后學校的在線判題的系統上傳上去總是run-error,也沒有錯誤提示,
之前助教說是因為陣列設定的太小,所以從100改成了10001,但是還是報錯run-error

因為是第一次用這種網頁的在線判題系統,不知道是什么地方出問題了。
另外 還想問下:有沒有別的cin的方式來讀取輸入呢
這是學校在線判題系統的環境:
OS : Ubuntu 18.04 (64-bit)
gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0
代碼:
#include<iostream>
#include<string>
using namespace std;
class Array_Stack {
public:
int* Stack;
int capacity; //SIZE
int t; //top
Array_Stack(int capacity) {
this->capacity = capacity;
this->Stack = new int[capacity];
this->t = -1;
}
~Array_Stack() {
delete Stack;
}
int size() {
return t+1;
}
bool empty() {
if (t == -1) {
return true;
}
else {
return false;
}
}
int top() {
if (empty()) {
return -1;
}
else {
return Stack[t];
}
}
void push(int e) {
if (t == capacity - 1) {
}
else {
Stack[++t] = e;
}
}
int pop(){
if (empty()) {
return -1;
}
else {
return Stack[t--];
}
}
};
int main() {
Array_Stack A(10001);
string str;
string arr[2];
int index = 0;
int M;
cin >> M;
for (int n = 0; n < M; n++) {
while (cin >> str) {
arr[index++] = str;
char ch = getchar();
if (ch == '\n') break;
}
if (arr[0] == "empty") {
if (A.empty()) {
cout << "1"<<endl;
}
else {
cout << "0"<<endl;
}
}if (arr[0] == "push") {
A.push(atoi(arr[1].c_str()));
}if (arr[0] == "size") {
cout << A.size()<<endl;
}
if (arr[0] == "pop") {
cout << A.pop() << endl;
}
if (arr[0] == "top") {
cout << A.top() << endl;
}
cin.clear();
index = 0;
}
return 0;
}
uj5u.com熱心網友回復:
然后如果我把main函式里面就是那一堆輸入的代碼注釋以后,上傳上去就不是run-error了 ;變成Result: no-output這是不是意味著是我輸入這部分的代碼有問題呢?
因為第一次接觸這種在線評測系統...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100741.html
標籤:C++ 語言
上一篇:大佬救命二叉樹創建卡死了
