Scramble Sort

描述:

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.


输入:

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.


输出:

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.


样例输入:

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.

.


样例输出:

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.


题目大意:

输入数据有多行最后一行以.来结束,每行中会有单词和数字,将单词按字典序由小到大排序后按照单词原来所在的位置排序(不得排在数字的位置),同理数字也由小到大排序(不得排在单词的位置)。


代码如下:

#include<stdio.h>  
#include<string.h>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
char s[1005];  
int e1,e2;  
int flag[105];                                                  //该数组用来记录每行中数字和单词的位置1为数字0为单词   
struct word  
{  
    char zxc[105];  
}word1[105];  
struct number  
{  
    char asd[105];  
}number1[105];  
bool cmp1(number A,number B)  
{  
    if(A.asd[0]!='-'&&B.asd[0]!='-')                        //都是正数的情况   
    {  
        if(strlen(A.asd)==strlen(B.asd))  
        {  
            return strcmp(A.asd,B.asd)<0;  
        }     
        else  
        {  
            return strlen(A.asd)<strlen(B.asd);  
        }         
    }  
    else if(A.asd[0]=='-'&&B.asd[0]=='-')                   //都是负数绝对值大的数小   
    {  
        if(strlen(A.asd)==strlen(B.asd))  
        {  
            return strcmp(A.asd,B.asd)>0;  
        }     
        else  
        {  
            return strlen(A.asd)>strlen(B.asd);  
        }                 
    }  
    else                                                    //其他情况带负号的小   
    {  
        if(A.asd[0]=='-')  
        {  
            return true;  
        }  
        else  
        return false;  
    }     
}  
bool cmp2(word A,word B)  
{  
    return strcmp(strupr(A.zxc),strupr(B.zxc))<0;        //防止大小写干扰都化成一样形式后比较   
}  
void divide(int a)                          //拆分每行输入的字符串,将单词的存入结构体word1内同理将数字存入结构体number1内   
{  
    int e3=0,flag1=0,e4=0,flag2=0;  
    char s2[105];  
    for(int i=0;i<a;i++)  
    {  
        if(s[i]==' ')  
        {  
            continue;  
        }  
        else if(s[i]==','||s[i]=='.')                   //表示一个单词或数字输入完成   
        {  
            e4=0;  
            if(flag1==1)                                //利用flag1来判断是否为数字    
            {  
                flag1=0;  
                strcpy(number1[e1++].asd,s2);           //拷贝记录的字符串到存储数字的结构体中   
            }  
            else                                        //同理来记录单词   
            {  
                flag2=0;  
                strcpy(word1[e2++].zxc,s2);  
            }  
        }  
        else  
        {  
            if((s[i]>='0'&&s[i]<='9')||s[i]=='-')           
            {  
                if(flag1==0)  
                {  
                    flag[e3++]=1;  
                    flag1=1;  
                }  
                s2[e4++]=s[i];  
                s2[e4]='\0';  
            }  
            else  
            {  
                if(flag2==0)  
                {  
                    flag[e3++]=0;  
                    flag2=1;  
                }  
                s2[e4++]=s[i];  
                s2[e4]='\0';                  
            }  
        }  
    }  
}  
void sove()  
{  
    int e5=0,e6=0;  
    for(int i=0;i<e1+e2;i++)  
    {  
        if(flag[i]==1)  
        {  
            if(i!=e1+e2-1)  
            cout<<number1[e5++].asd<<','<<' ';  
            else  
            cout<<number1[e5++].asd<<'.'<<endl;  
        }  
        else  
        {  
            if(i!=e1+e2-1)  
            cout<<word1[e6++].zxc<<','<<' ';  
            else  
            cout<<word1[e6++].zxc<<'.'<<endl;           
        }  
    }  
}  
int main()  
{  
    while(gets(s))  
    {  
        if(strcmp(s,".")==0)  
        break;  
        memset(flag,0,sizeof(flag));  
        e1=0,e2=0;  
        int len=strlen(s);  
        divide(len);  
        sort(number1,number1+e1,cmp1);  
        sort(word1,word1+e2,cmp2);  
        sove();  
    }  
    return 0;  
}   





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值