1075: Numerical Sorting by KACA [sorting]
Time Limit: 1 Sec Memory Limit: 128 MBCommits: 57 Resolved: 9 Stats
Topic description
PIPA wants KACA to sort a string of numbers.
KACA said it was a simple question, but when he saw the numbers, he was stunned. I saw all kinds of 1234567890987654321... They are all very huge numbers, but after thinking about it, he still chose to take over this task.
enter
The first line is an integer TT ( 1≤T≤1001≤T≤100 ), representing TT group test data.
The first row of each set of data is an integer nn ( 1≤n≤1001≤n≤100 ), representing nn numbers.
There are n lines below, each with an integer xx ( 0≤|x|≤101000≤|x|≤10100 )。
output
For each set of test data, output the results sorted from small to large.
Output one number per line.
sample input
1
3
123
345
234
Sample output
123
234
345
source
After seeing so many big guys playing "bitwise comparison of large numbers", I feel that I am a real dish. Here I use a lazy method to call the string class of C++.
code show as below:
#include<bits/stdc++.h> using namespace std; int cmp(string a,string b){ if(a[0]!='-'&&b[0]!='-'){ if(a.length()<b.length()) return 1; //If they are all positive numbers, the longer they are else if(a.length()==b.length()){ if(a<b) //The length is the same, compare in lexicographical order, the bigger is bigger return 1; } } if(a[0]=='-'&&b[0]!='-'){ //negative is less than positive return 1; } if(a[0]=='-'&&b[0]=='-'){ if(a.length()>b.length()) return 1; //If they are all negative numbers, the longer ones are smaller else if(a.length()==b.length()){ if(a>b) //The length is the same, compare in lexicographical order, the smaller is the bigger return 1; } } if(a[0]!='-'&&b[0]=='-'){ return 0; // positive is greater than negative } } int main(){ int t,n; string a[100]; cin>>t; while(t--){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n,cmp); for(int i=0;i<n;i++){ cout<<a[i]<<endl; } } return 0; }