分享一个网上找到的第一题的代码
#include<iostream>
#include<vector>
using namespace std;
bool check(char a, char b, char c) {
if (b == '+' && a == '+' && c != '*' && c != '/') return true;
if (b == '-' && a == '-' && c != '*' && c != '/') return true;
if (b == '*' && (a == '+' or a == '-')) return true;
if (b == '/' && a == '/') return true;
return false;
}
int main() {
int n; cin >> n;
vector<int> num(n); vector<char> op(n+1,'+');
for (int i = 0; i < n-1; i++) {
cin >> num[i];
cin >> op[i+1];
}
cin >> num[n - 1];
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (num[j]>num[j+1]&&check(op[j],op[j+1],op[j+2])) {
num[j] = num[j] ^ num[j + 1];
num[j + 1] = num[j] ^ num[j + 1];
num[j] = num[j] ^ num[j + 1];
}
}
}
for (int i = 0; i < n; i++) {
cout << num[i] << " ";
if (i < n - 1) cout << op[i + 1] << " ";
}
cout << endl;
system("pause");
return 0;
}