#include<iostream>
#include<vector>
using namespace std;
int compare(string, string);
int main() {
int num = 0;
while (cin >> num) {
vector<string>arr(num);
int ans = 0;
for (int i = 0; i < num; i++) {
cin >> arr[i];
}
for (int i = 0; i < num; i++) {
for (int j = i; j < num; j++) {
ans = compare(arr[i], arr[j]);
if (ans == 1) {
swap(arr[i], arr[j]);
}
}
}
for (int i = 0; i < num; i++) {
cout << arr[i] << endl;
}
}
}
int compare(string a, string b) {
//cout << a[0] << " " << b[0] << endl;
if (a[0] == '-' || b[0] == '-') {
if (a[0] != '-' && b[0] == '-') {
return 1;
}
else if (a[0] == '-' && b[0] != '-') {
return 0;
}
else if (a[0] == '-' && b[0] == '-' && b.size() > a.size()) {
return 1;
}
else if (a[0] == '-' && b[0] == '-' && b.size() == a.size()) {
for (int i = 1; i < a.size(); i++) {
if (a[i] < b[i]) {
return 1;
}
else if (a[i] > b[i]) {
return 0;
}
}
}
}
else {
if (a.size() > b.size() ) {
return 1;
}
else if (a.size() == b.size()) {
for (int i = 0; i < a.size(); i++) {
if (a[i] > b[i]) {
return 1;
}
else if (a[i] < b[i]) {
return 0;
}
}
}
}
}
#include
#include
using namespace std;
int compare(string, string);
int main() {
int num = 0;
while (cin >> num) {
vectorarr(num);
int ans = 0;
for (int i = 0; i < num; i++) {
cin >> arr[i];
}
for (int i = 0; i < num; i++) {
for (int j = i; j < num; j++) {
ans = compare(arr[i], arr[j]);
if (ans == 1) {
swap(arr[i], arr[j]);
}
}
}
for (int i = 0; i < num; i++) {
cout << arr[i] << endl;
}
}
}
int compare(string a, string b) {
//cout << a[0] << " " << b[0] << endl;
if (a[0] == '-' || b[0] == '-') {
if (a[0] != '-' && b[0] == '-') {
return 1;
}
else if (a[0] == '-' && b[0] != '-') {
return 0;
}
else if (a[0] == '-' && b[0] == '-' && b.size() > a.size()) {
return 1;
}
else if (a[0] == '-' && b[0] == '-' && b.size() == a.size()) {
for (int i = 1; i < a.size(); i++) {
if (a[i] < b[i]) {
return 1;
}
else if (a[i] > b[i]) {
return 0;
}
}
}
}
else {
if (a.size() > b.size() ) {
return 1;
}
else if (a.size() == b.size()) {
for (int i = 0; i < a.size(); i++) {
if (a[i] > b[i]) {
return 1;
}
else if (a[i] < b[i]) {
return 0;
}
}
}
}
}
你的compare()裡面,如果a.size() < b.size()沒有return。