1174: Long Integer Sorting (Pointer Topic)

1174: Long Integer Sorting (Pointer Topic)

Topic description
Sorting of long integers. Enter n and then enter n large integers with no more than 100 digits, possibly with leading zeros. Sort the n long integers and output without leading 0s.
int greater(char *s1, char *s2)
{ If the integer pointed to by s1 is greater than the integer pointed to by s2, return a positive integer; if the integer pointed to by s1 is less than the integer pointed to by s2, return a negative integer; if the integer pointed to by s1 is equal to s2 The integer pointed to, returns 0; } input The first line of input is a positive integer n (n<=10), and the next n lines are n large integers, all non-negative. Output Output n lines of n large integers sorted in ascending order. Sample input the Copy . 3 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 54,213,456,565 113456789456231545674632132156465132123156456423132156465461321654878976464654132132156455 sample output the Copy 54,213,456,565 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
















Source
/Classification

#include<stdio.h>
#include<string.h>
#define N 202

int greater(char *s1,char *s2){
    
    
    int len1,len2;
    len1=strlen(s1);
    len2=strlen(s2);

    if(len1>len2) return 1;
    else if(len1<len2) return -1;
    else {
    
    
        for(int i=0;i<len1;i++){
    
    
            if(s1[i]>s2[i]) return 1;
            else if(s1[i]<s2[i]) return -1;
        }
        return 0;
    }
}

int main(){
    
    
    int n,j;
    char str[12][102],*p;
    scanf("%d ",&n);
    p=(char *)malloc(sizeof(char)*102);
    for(int i=0;i<n;i++){
    
    
        gets(str[i]);
        strcpy(p,str[i]);
        for(j=0;p[j]!='\0';j++){
    
    
            if(p[j]!='0') break;
            //全是0,只返回一个0即可
            if(p[j+1]=='\0') break;
        }
        p=&p[j];
        strcpy(str[i],p);
    }

    for(int i=0;i<n;i++){
    
    
        for(int j=i+1;j<n;j++){
    
    
            if(greater(str[i],str[j])==1){
    
    
                strcpy(p,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],p);
            }
        }
    }
    for(int i=0;i<n;i++){
    
    
        puts(str[i]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44500344/article/details/108026637