Numbers game (sort of application of string)

Title Description

Beef held a numbers game, there are n players participate in the game, the game starts each player selected a number, then this number will be written on paper (decimal number, no prefix zero), and then followed for each digit the bit according to the number of non-descending order, to obtain a new number, zero new prefix number will be ignored. To get the maximum number of players to win this game.

Enter a description:

Input comprises two rows, a first row comprising an integer n (1 ≤ n ≤ 50) , i.e., the number of players 
a second row of n integers x [i] (0 ≤ x [i] ≤ 100000), i.e. each player to write integer under.

Output Description:

Output An integer representing the maximum number of players get to win the game that is.
Example 1

Entry

copy
3
9638 8210 331

Export

copy
3689 

Outline of Solution 1: ordering function using the string (that is, non-descending ascending) and atoi (x.c_str ()) the application function can be transformed Solving
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    int n,i,Max=-1;
    for(cin>>n,i=0;i<n;i++){
        string x;
        cin>>x,sort(x.begin(),x.end());
        Max=max(Max,atoi(x.c_str()));
    }
   cout<<Max;

}

Problem-solving ideas 2: Do not use string properties themselves bit taken out and then assigned to another array, after a call to sort integer () function to sort // not recommended

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    int* x=new int[n];
    int* rev=new int[n];
    for(int i=0;i<n;i++){
        cin>>x[i];
        int a[6],count=0,temp1=x[i],temp2=0;
        while(temp1>0){
            a[count++]=temp1%10;
            temp1/=10;
        }
        sort(a,a+count);
        for(int j=0;j<count;j++)
            temp2=temp2*10+a[j];
        rev[i]=temp2;
    }
    sort(rev,rev+n);
    cout<<rev[n-1]<<endl;
    delete(x);delete(rev);
}

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/10993198.html