fjnu 1669 Scramble Sort

Description

In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.

Input

The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single
period.

Output

For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.

Sample Input

0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.

Sample Output

0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.

 

KEY:这是我们第一次预赛,我是把他用strtok拆成存数字和字符串,同时标记原来的顺序(数字0,字符1),分别排序再打印出来;

 

Source:

#include
< iostream >
#include
< string >
#include
< algorithm >
using   namespace  std;

int  a[ 1000 ];
char  b[ 1000 ][ 100 ];
int  la,lb;
int  lf;
int  flag[ 1000 ];
char  str[ 10000 ];

void  GetList()
{
    
char* t;
    
char* step=". ,"
    t
=strtok(str,step);
    
while(t!=NULL)
    
{
        
if(t[0]=='-'||(t[0]>='0'&&t[0]<='9'))
        
{
            a[
++la]=atoi(t);
            flag[
++lf]=0;
        }

        
else
        
{
            strcpy(b[
++lb],t);
            flag[
++lf]=1;
        }

        t
=strtok(NULL,step);
    }

}


int  compare( char  x[], char  y[])
{
    
int i;
    
for(i=0;i<strlen(x);i++)
        
if(x[i]>='A'&&x[i]<='Z') x[i]=x[i]+32;
    
for(i=0;i<strlen(y);i++)
        
if(y[i]>='A'&&y[i]<='Z') y[i]=y[i]+32;
    
if(strcmp(x,y)>0return 1;
    
else return 0;
}


void  SORT()
{
    
int i,j;
    sort(a
+1,a+la+1);
    
for(i=1;i<=lb;i++)
        
for(j=1;j<=lb;j++)
        
{
            
char b1[1000],b2[1000];
            strcpy(b1,b[i]);
            strcpy(b2,b[j]);
            
if(!compare(b1,b2))
            
{
                
char t[1000];
                strcpy(t,b[i]);
                strcpy(b[i],b[j]);
                strcpy(b[j],t);
            }

        }

}


void  output()
{
    
int i=1,j=1,k;
    
for(k=1;k<lf;k++)
    
{
        
if(flag[k]==0)
            cout
<<a[i++]<<"";
        
if(flag[k]==1)
            cout
<<b[j++]<<"";
    }

    
if(flag[k]==0)
        cout
<<a[i++]<<"."<<endl;
    
if(flag[k]==1)
        cout
<<b[j++]<<"."<<endl;
}


int  main()
{
    
while(gets(str))
    
{
        
if(!strcmp(str,".")) break;
        GetList();
        SORT();
        output();
        la
=lb=lf=0;
    }

    
return 0;
}













 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值