C language - the numbers game

Numbers game
Problem Description:
Now, there are many children to play the numbers game, the game play is simple, but to create a not so easy a. In this, we will introduce an interesting game.
You will get a positive integer N, you can pick an integer integer after another to make a larger integer. For example, it has four digits 123, 124, 56, 90, they may be made of the following integers ─ 1231245690, 1241235690, 5612312490, 9012312456 , 9056124123 ... and the like, may be combined with a total of 24 (4!) Types of numbers. However, 9056124123 is the largest one.
You might think this is a simple thing, but just the concept of digital kids, this would be a simple task?
Inputs Note:
Input contain multiple sets of test data.
Two rows each test data, the behavior of a first positive integer N (N <= 50), the second line with N a positive integer.
When N = 0 represents the input end.
Description Output:
For each set of test data output line, this output with the maximum integer N may be combined into integers.
Here Insert Picture Description

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[51][1000];//输入的数用全局变量来操作,和他之后的数一个一个去比较,交换位置 
int cmp(int x,int y){
    char b[1000],c[1000],str[1000];
    int i,num1,num2;
    strcpy(b,a[x]);
    strcpy(c,a[y]);
    num1=strlen(a[x]);//数的长度 
    num2=strlen(a[y]);
     //字符串拼接
    for(i=num1;i<=num1+num2;i++) 
        b[i]=c[i-num1];
    for(i=num2;i<num1+num2;i++)
        c[i]=b[i-num2];
    c[i]='\0';
    return strcmp(b,c);//看a[i]a[j]大还是a[j]a[i]的数比较大 
 } 
 int main(){
    int n,i,j;
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++)
            scanf("%s",a[i]);
        char temp[1000];
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
            	if(cmp(i,j)<0){//交换位置 
	                strcpy(temp,a[i]);
	                strcpy(a[i],a[j]);
	                strcpy(a[j],temp);
            	}
			}	
        }
        for(i=0;i<n;i++)
            printf("%s",a[i]);
        printf("\n");
    }
    return 0;
 }
Released nine original articles · won praise 6 · views 1758

Guess you like

Origin blog.csdn.net/weixin_44252790/article/details/104093288