Untitled
unknown
plain_text
2 years ago
2.8 kB
8
Indexable
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100000
#define ABIG 1
#define BBIG 0
int strBigger(char* a, char* b, int a_len, int b_len){
// printf("comparing %s and %s\n", a, b);
// return 1 if a > b; 0 if a < b
if( a_len < b_len ){
for(int i=0; i<a_len; i++){
// printf("a[%d] = %c, b[%d] = %c\n", i, a[i], i, b[i]);
if( a[i] == b[i] ){
// printf("a[%d] == b[%d] = %c\n", i, i, b[i]);
continue;
}else if( a[i] > b[i] ){
// printf("a[%d] = %c > b[%d] = %c\n", i, a[i], i, b[i]);
return ABIG;
}else if( a[i] < b[i] ){
// printf("a[%d] = %c < b[%d] = %c\n", i, a[i], i, b[i]);
return BBIG;
}
}
// check if remaining of b is all smaller than last digit of a
return strBigger(a, b+a_len, a_len, b_len - a_len);
}else if( a_len > b_len ){
for(int i=0; i<b_len; i++){
// printf("a[%d] = %c, b[%d] = %c\n", i, a[i], i, b[i]);
if( a[i] == b[i] ){
// printf("a[%d] == b[%d] = %c\n", i, i, b[i]);
continue;
}else if( a[i] > b[i] ){
// printf("a[%d] = %c > b[%d] = %c\n", i, a[i], i, b[i]);
return ABIG;
}else if( a[i] < b[i] ){
// printf("a[%d] = %c < b[%d] = %c\n", i, a[i], i, b[i]);
return BBIG;
}
}
// check if remaining of b is all smaller than last digit of a
return strBigger(a+b_len, b, a_len - b_len, b_len);
}else if( a_len == b_len ){
for(int i=0; i<b_len; i++){
// printf("a[%d] = %c, b[%d] = %c\n", i, a[i], i, b[i]);
if( a[i] == b[i] ){
// printf("a[%d] == b[%d] = %c\n", i, i, b[i]);
continue;
}else if( a[i] > b[i] ){
// printf("a[%d] = %c > b[%d] = %c\n", i, a[i], i, b[i]);
return ABIG;
}else if( a[i] < b[i] ){
// printf("a[%d] = %c < b[%d] = %c\n", i, a[i], i, b[i]);
return BBIG;
}
}
return ABIG;
}
}
int main(){
int n; scanf("%d", &n);
char nums[n][MAX];
for(int i=0; i<n; i++){
char str[MAX];
scanf("%s", str);
strncpy(nums[i], str, strlen(str));
// printf("nums[%d] = %s\n", i, str);
}
// bubble sort nums small to big
for(int i=0; i<n; i++){
for(int j=0; j<n-i-1; j++){
if( strBigger( nums[j], nums[j+1], strlen(nums[j]), strlen(nums[j+1]) ) ){
// nums[j] > nums[j+1]
// printf("%s is larger than %s\n", nums[j], nums[j+1]);
char temp[MAX];
strcpy(temp, nums[j]);
strcpy(nums[j], nums[j+1]);
strcpy(nums[j+1], temp);
}
}
}
for(int i=n-1; i>-1; i--){
printf("%s", nums[i]);
}
return 0;
}
/*
100 10
10010
10100
-> 100 < 10
110 10
11010
10110
-> 110 > 10
100 11
10011
11100
-> 100 < 11
12 123
12123
12312
*/Editor is loading...
Leave a Comment