迴圈

輸入字串

判別字串大小做排序

(因為比較方式寫在外側,故bubble sort較為妥當)

(若用quick sort 則變成函式(quick sort)呼叫函式(cmp) 會 TLE)

輸出

 

 

#include <iostream>
#include <cstring>

#define N 1000

 

 

using namespace std;

bool cmp(string str1, string str2)
{
    //比較正負
    if(str1[0] != '-' && str2[0] == '-') return true;
    if(str1[0] == '-' && str2[0] != '-') return false;

    //比較位數
    if(str1.length() > str2.length())
        {if(str1[0] != '-') return true; else return false;}
    if(str1.length() < str2.length())
        {if(str1[0] == '-') return true; else return false;}

    //比較大小
    if(str1 > str2)
        {if(str1[0] != '-') return true; else return false;}
    if(str1 < str2)
        {if(str1[0] == '-') return true; else return false;}
        
    return false;
}

int main()
{
    string str[N];
    int strNum;

    while(cin >> strNum)
    {
        for(int i = 0; i<strNum; i++)
            cin >> str[i];

        for(int i = 0; i<strNum-1; i++)
            for(int j = 0; j<strNum-i-1; j++)
                if(cmp(str[j], str[j+1]))
                    swap(str[j], str[j+1]);

        for(int i = 0; i<strNum; i++)
            cout << str[i] << endl;
    }
    
    return 0;
}
 

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()