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; }
總結
以上是生活随笔為你收集整理的Scramble Sort的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scramble-string
- 下一篇: Progressive Scramble