4th IIUC Inter-University Programming Contest, 2005 | |
A |
Children’s Game |
Input: standard input | |
Problemsetter: Md. Kamruzzaman |
There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made – 1231245690, 1241235690, 5612312490, 9012312456, 9056124123 etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made.
You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?
Input
Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input is terminated by N = 0, which should not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending all the Nintegers.
Sample Input |
Output for Sample Input |
4 |
9056124123 99999 |
本题是关于字符串的比较!!!!
将输入的数两两进行比较,不断把满足条件的数提到前面!!
比如对于input:
123 124 56 90 9
第一次:
124 123 56 90 9
第二次:
56 124 123 90 9
第三次:
90 124 123 56 9
第四次:
9 124 123 56 90
结束!!
代码如下:
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int maxn = 10;
char num[60][200];
char temp1[200];
char temp2[200];
char temp[200];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
int i;
for(i = 0;i<n;++i)
scanf("%s",num[i]);
for(i = 0;i<n-1;++i)
{
for(int j = i+1;j<n;++j)
{
strcpy(temp1,num[i]);
strcat(temp1,num[j]);
strcpy(temp2,num[j]);
strcat(temp2,num[i]);
if(strcmp(temp1,temp2)<0)
{
strcpy(temp,num[i]);
strcpy(num[i],num[j]);
strcpy(num[j],temp);
}
}
}
for(i = 0;i<n;++i)
printf("%s",num[i]);
printf("\n");
}
return 0;
}
没发现错误但runtime error的代码:
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int maxn = 10;
int num[1000];
int num1[10000];
int num2[10000];
bool cmp(int n1,int n2)
{
int c1=0,c2=0;
int i ;
if(n1==0)
{
c1=1;
num1[1]=0;
}
else
while(n1)
{
c1++;
num1[c1]=n1%10;
n1/=10;
}
if(n2==0)
{
c2=1;
num2[1] = 0;
}
else
while(n2)
{
c2++;
num2[c2]=n2%10;
n2/=10;
}
int flag = 0;
if(c1==c2)
{
for(i = c1;i>=1;--i)
if(num1[i]>num2[i])
{
flag = 1;
break;
}
else
if(num1[i]<num2[i])
{
flag = 0;
break;
}
}
else
{
if(c1>c2)
{
for(i = c2;i>=1;--i)
{
if(num1[c1-c2+i]>num2[i])
{
flag = 1;
break;
}
else
if(num1[c1-c2+i]<num2[i])
{
flag = 0;
break;
}
}
if( num1[c1-c2]>=num2[c2])
flag = 1;
else
flag=0;
}
else
{
if(c2>c1)
{
for(i = c1;i>=1;--i)
{
if(num2[c2-c1+i]<num1[i])
{
flag = 1;
break;
}
else
if(num2[c2-c1+i]>num1[i])
{
flag = 0;
break;
}
}
if(num2[c2-c1]<=num1[c1])
flag=1;
else
flag=0;
}
}
}
return flag;
}
int main()
{
int n;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
int i ;
for(i = 0;i<n;++i)
scanf("%d",&num[i]);
sort(num,num+n,cmp);
for(i = 0;i<n;++i)
printf("%d",num[i]);
printf("\n");
}
return 0;
}