High Precision Sort [Template]

High precision has always been a thing that makes this konjac bald, but today it is necessary to write high precision sorting. . .

Enter the number of numbers, and each number, find the largest number and its number.

general idea

Through the greedy method, record the number of the largest number and the number of digits of the largest number each time you input, store all the numbers in a two-dimensional array (if you don't need it later, you can not store it), and then you only need to compare the number of digits each time you input. Okay, if the number of digits is the same, compare the size of the highest digit, if it is still the same, compare the size of the next highest digit... and so on to find out who is bigger.

Core: Convert the comparison of multiple numbers into a comparison of two high-precision numbers through greedy (which can be understood as bubble sort)

code above

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,ma,a[30][1000],maxx;

MAXX stores the number of digits of the largest number, and ma stores the number of the largest number

char x[105];
int com(int a,int b)
{
    if(a<b) return 0;
    if(a>b) return 1;
    if(a==b) return 2;
}

Compare functions for convenience

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        for(int j=0;j<strlen(x);j++) a[i][j]=x[j]-'0';
        if(strlen(x)>maxx)
        {
            maxx=strlen(x);
            ma=i;
        }

above for storage

Here comes the focus

        else if(strlen(x)==maxx)
        {
            for(int j=0;j<strlen(x);j++)
            {
                int f=com(a[ma][j],a[i][j]);
                if(f==0)
                {
                    ma=i;
                    break;
                }
                if(f==1) break;
                if(f==2) continue;
            }
        }
    }

0, 1, and 2 represent three states (<, >, =) respectively. If this bit is equal, the cycle continues and the next bit is compared. Compare the difference and break it

cout<<ma+1<<endl;

Because it is an array that starts from 0, it needs +1

for(int i=0;i<maxx;i++)
    {
        cout<<a[ma][i];
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691360&siteId=291194637