这题用C语言怎么排序啊,我用快排好像会超时。。

P1102 A-B 数对

https://www.luogu.com.cn/paste/q2bvmdoi 用的是个归并排序
by imsunxinhao @ 2024-02-22 00:29:16


@[imsunxinhao](/user/1188071) 解决了,感谢大佬
by Red_Cow @ 2024-03-02 19:26:08


#include <iostream> using namespace std; const int MAXSIZE = 2e5; class Stack { public: int list[MAXSIZE] = { 0 }; int top = -1; int is_empty();//判断栈是否为空 void push(int n);//往栈中推入元素 int pop();//弹出栈顶元素 int Top();//读取栈顶元素 int find(int n);//判断栈中的元素是否存在(存在则返回对应下标) void Delete(int n);//删除指定元素 void show();//遍历整个栈 }; int Stack::is_empty() { if (top == -1) { cout << "栈为空" << endl; return 1; } return 0; } void Stack::push(int n) { if (top == MAXSIZE-1) { cout << "栈已满" << endl; return; } top++; list[top] = n; } int Stack::pop() { if (is_empty()) return -1;/////////////////// else { top--; return list[top+1]; } } int Stack::Top() { if (is_empty()) return -1;/////////////////// return list[top]; } int Stack::find(int n) { if (is_empty()) return -1; for (int i = 0; i <= top; i++) { if (list[i] == n) return i; } cout << "不存在该元素" << endl; return -2; } void Stack::Delete(int n) { if (is_empty()) return; int idx = find(n); if (idx < 0) cout << "删除失败" << endl; for (int i = idx+1; i <= top; i++) { list[i - 1] = list[i]; } } void Stack::show() { if (is_empty()) return; for (int i = top; i >= 0; i--) { cout << list[i] << " "; } cout << endl; } int main() { Stack a; for (int i = 0; i < 10; i++) a.push(i); cout << a.pop() << endl; cout << a.Top() << endl; cout << a.find(1) << endl; return 0; }``` #include <iostream> using namespace std; const int MAXSIZE = 2e5; class Stack { public: int list[MAXSIZE] = { 0 }; int top = -1; int is_empty();//判断栈是否为空 void push(int n);//往栈中推入元素 int pop();//弹出栈顶元素 int Top();//读取栈顶元素 int find(int n);//判断栈中的元素是否存在(存在则返回对应下标) void Delete(int n);//删除指定元素 void show();//遍历整个栈 }; int Stack::is_empty() { if (top == -1) { cout << "栈为空" << endl; return 1; } return 0; } void Stack::push(int n) { if (top == MAXSIZE-1) { cout << "栈已满" << endl; return; } top++; list[top] = n; } int Stack::pop() { if (is_empty()) return -1;/////////////////// else { top--; return list[top+1]; } } int Stack::Top() { if (is_empty()) return -1;/////////////////// return list[top]; } int Stack::find(int n) { if (is_empty()) return -1; for (int i = 0; i <= top; i++) { if (list[i] == n) return i; } cout << "不存在该元素" << endl; return -2; } void Stack::Delete(int n) { if (is_empty()) return; int idx = find(n); if (idx < 0) cout << "删除失败" << endl; for (int i = idx+1; i <= top; i++) { list[i - 1] = list[i]; } } void Stack::show() { if (is_empty()) return; for (int i = top; i >= 0; i--) { cout << list[i] << " "; } cout << endl; } int main() { Stack a; for (int i = 0; i < 10; i++) a.push(i); cout << a.pop() << endl; cout << a.Top() << endl; cout << a.find(1) << endl; return 0; } ```
by Red_Cow @ 2024-03-28 17:29:20


```cpp #include <iostream> using namespace std; const int MAXSIZE = 2e5; class Stack { public: int list[MAXSIZE] = { 0 }; int top = -1; int is_empty();//判断栈是否为空 void push(int n);//往栈中推入元素 int pop();//弹出栈顶元素 int Top();//读取栈顶元素 int find(int n);//判断栈中的元素是否存在(存在则返回对应下标) void Delete(int n);//删除指定元素 void show();//遍历整个栈 }; int Stack::is_empty() { if (top == -1) { cout << "栈为空" << endl; return 1; } return 0; } void Stack::push(int n) { if (top == MAXSIZE-1) { cout << "栈已满" << endl; return; } top++; list[top] = n; } int Stack::pop() { if (is_empty()) return -1;/////////////////// else { top--; return list[top+1]; } } int Stack::Top() { if (is_empty()) return -1;/////////////////// return list[top]; } int Stack::find(int n) { if (is_empty()) return -1; for (int i = 0; i <= top; i++) { if (list[i] == n) return i; } cout << "不存在该元素" << endl; return -2; } void Stack::Delete(int n) { if (is_empty()) return; int idx = find(n); if (idx < 0) cout << "删除失败" << endl; for (int i = idx+1; i <= top; i++) { list[i - 1] = list[i]; } } void Stack::show() { if (is_empty()) return; for (int i = top; i >= 0; i--) { cout << list[i] << " "; } cout << endl; } int main() { Stack a; for (int i = 0; i < 10; i++) a.push(i); cout << a.pop() << endl; cout << a.Top() << endl; cout << a.find(1) << endl; return 0; } ```
by Red_Cow @ 2024-03-28 17:29:37


|