a104: 排序

2022.12.04 PM 10:00 by CBJ

來源 : https://zerojudge.tw/ShowProblem?problemid=a104
出題者 : Zerojudge
標籤 : 排序
難易度 : 1
解題想法 : 
C、C++、Python都有關於排序的函式,只要套用即可求解。

排序函式 :
C - qsort(陣列,陣列大小,每個元素的size,比較函式)
C++ - sort(陣列開頭,陣列結尾,比較函式(可選))
Python - 陣列.sort() or sorted(陣列)

注意 :
題目是多筆輸入,因此需讀到EOF為止。
//C language

#include<stdio.h>
#include<stdlib.h>  //qsort()
int cmp(const void *a,const void *b){
    return *(int*)a - *(int*)b;
}
int main(){
    int N;
    while(scanf("%d",&N)!=EOF){
        int a[1005];
        for(int i=0;i<N;i++){
            scanf("%d",&a[i]);
        }
        qsort(a,N,sizeof(a[0]),cmp);
        for(int i=0;i<N;i++){
            printf("%d ",a[i]);
        }
        printf("\n");
    }
    return 0;
}
//C++ language

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int N;
    while(cin>>N){
        vector<int>v;
        for(int i=0;i<N;i++){
            int x;cin>>x;
            v.push_back(x);
        }
        sort(v.begin(),v.end());
        for(int i:v)cout<<i<<" ";
        cout<<"\n";
    }
    return 0;
}
## Python language

from sys import stdin
for read in stdin:
    print(*sorted([int(x) for x in input().split()]))

相關文章