HrbustOJ 1644 水水更健康

水水更健康

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 559(275 users) Total Accepted: 223(206 users) Rating: Special Judge: No

Description

水神为了越来越健康,于是决定每天一道水题~

Input

本题有多组测试数据,每组测试数据占n+1行。
第一行输入一个数字n (1 ≤ n ≤ 100),表示正整数的个数,接下来n行每行输入一个正整数,每个正整数不超过10^100。

Output

每组测试数据输出占n行,按照从小到大的顺序输出。

Sample Input

2
456
56
3
1
90
7

Sample Output

56
456
1
7
90

Hint

保证不会出现 001 等非法输入。

Author

sunshine@Hrbust

题解

由于数字非常大而数据量特别小,所以我们可以用 string 存这个数字,用 sort 对其排序。而 sort 默认以字典序给 string 排序,所以写一个 cmp 函数传给它即可,具体思路就是长度相等时返回较小字典序的那个,长度不等时返回长度较小的那个。代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std ;

bool cmp(string str1 , string str2){
    if ( str1.length() == str2.length() ){
        return str1 < str2 ;
    }else{
        return str1.length() < str2.length() ;
    }
}

int main(){
    int n ;
    while ( cin >> n ){
        string str[n] ;
        for ( int i = 0 ; i < n ; i ++ ){
            cin >> str[i] ;
        }
        sort(str , str + n , cmp) ;
        for ( int i = 0 ; i < n ; i ++ ){
            cout << str[i] << endl ;
        }
    }
    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/Cantredo/p/9899026.html
今日推荐