#37871: 動態陣列解


qqappleop87@gmail.com (ku uk)

學校 : 不指定學校
編號 : 168221
來源 : [140.113.237.228]
最後登入時間 :
2024-01-04 10:22:16
a225. 明明愛排列 | From: [140.113.90.199] | 發表日期 : 2023-10-15 09:11

#include <iostream>

using namespace std;

 

int main() {

    int len;

    while(cin>>len){

        int* ary = new int[len];//創儲存完整數字的動態陣列

        for(int i = 0 ; i < len ; i++){//輸入進動態陣列裡

            cin>>ary[i];

        }

        //比個位數大小

        for(int i = 0 ; i < len ; i++){

            for(int j = 0 ; j < len-1 ; j++){

                if((ary[j]%10)>(ary[j+1]%10)){

                    int temp = ary[j];

                    ary[j] = ary[j+1];

                    ary[j+1] = temp;

                }

            }

        }

        //確認個位數一樣再比大小,小的排後面

        for(int i = 0 ; i < len ; i++){

            for(int j = 0 ; j < len-1 ; j++){

                if((ary[j]%10)==(ary[j+1]%10)&&ary[j]<ary[j+1]){

                    int temp = ary[j];

                    ary[j] = ary[j+1];

                    ary[j+1] = temp;

                }

            }

        }

        for(int i = 0 ; i < len ; i++){

            cout << ary[i] << " ";

        }

        cout << endl;

        delete[]ary;//回收

    }

}

 

 
ZeroJudge Forum