toj 2807 Number Sort

 

2807.   Number Sort
Time Limit: 10.0 Seconds    Memory Limit: 65536K
Total Runs: 469    Accepted Runs: 160



Given you N positive integers, your job is to output these N numbers in the ascending order in every digit (if two numbers in some digit are the same, you should output the smaller one first). For example, there are five numbers 1 11 111 1111 11111. You should output five lines as sample output. If a number doesn't have the ith digit, you can consider its ith digit zero.

Input

The input contains several test cases. Each case contains two lines. The first line contains a positive integer N ( N ≤ 1000), indicating the length of the sequence. The second line gives N numbers (each number will not exceed 2147483647). If N = 0 it signals the end of the input and isn't considered to be processed.

Output

Each case contains several lines. The first line is indicated as sample output. The i + 1 line contains these N numbers in ascending order in the i th digit.

Sample Input

5
1 11 111 1111 11111
7
19 28 37 46 55 55 45
0

Sample Output

Case 1:
1 11 111 1111 11111
1 11 111 1111 11111
1 11 111 1111 11111
1 11 111 1111 11111
1 11 111 1111 11111
Case 2:
45 55 55 46 37 28 19
19 28 37 45 46 55 55

Problem Setter: mmatch@TJU



Source: TJU Programming Contest 2007 Preliminary
Submit   List    Runs   Forum   Statistics

//
#include  < iostream >
#include 
< algorithm >
#include 
< cmath >
#define  MAX 1002
using   namespace  std;
int  data[MAX],ddd[MAX];
struct  node
{
    
int  num;
    
int  dd;
    
int  da;
}s[MAX];
bool  comp(node a,node b)
{
    
if (a.dd != b.dd)
        
return  a.dd < b.dd;
    
else
        
return  a.da < b.da;
}
int  n;
int  main()
{
    
int  i,MM,j,zzz = 1 ,k;
    
while (scanf( " %d " , & n) != EOF  &&  n)
    {
        MM
=- 1 ;
        
for (i = 0 ;i < n;i ++ )
        {
            scanf(
" %d " , & data[i]);
            ddd[i]
= data[i];
            
if (data[i] > MM)
                MM
= data[i];
        }
        
int  nn = 0 ;
        
while (MM != 0 )
        {
            MM
= MM / 10 ;
            nn
++ ;
        }
        
int  cs = 10 ;
        printf(
" Case %d:\n " ,zzz ++ );
        
for (i = 0 ;i < nn;i ++ )
        {
            
for (j = 0 ;j < n;j ++ )
            {
                s[j].num
= j;
                s[j].da
= data[j];
                s[j].dd
= ddd[j] % cs;
                ddd[j]
= ddd[j] / 10 ;
            }
            sort(s,s
+ n,comp);
            printf(
" %d " ,data[s[ 0 ].num]);
            
for (k = 1 ;k < n;k ++ )
            {
                printf(
"  %d " ,data[s[k].num]);
            }
            printf(
" \n " );
        }
    }
    
return   0 ;
}

转载于:https://www.cnblogs.com/forever4444/archive/2009/05/14/1456960.html

你可能感兴趣的:(toj 2807 Number Sort)