请问自定义的排序为什么不行呢 就第一个过不了 谢谢了
查看原帖
请问自定义的排序为什么不行呢 就第一个过不了 谢谢了
786263
Qulingjun楼主2024/12/4 16:26
#include<iostream>
#include<cstring>

using namespace std;

// 根据第一个数字大小降序排序
void sort(char (*str)[10], int n) {
    while (--n) {
        for (int j = 0; j < n; j ++) {
            if (strcmp(str[j], str[j+1]) < 0) swap(str[j], str[j+1]);
        }
    }
}

int main() {
    int n;
    cin>>n;
    // 将输入当作字符串处理
    char (*str)[10] = (char(*)[10])malloc(sizeof(char)*10*n);
    for (int i = 0; i < n; i ++) {
        scanf("%s", str[i]);
    }
    sort(str,n);
    for (int i = 0; i < n; i ++) {
        cout<<str[i];
    }
    return 0;
}
2024/12/4 16:26
加载中...