第一題
給定一組單詞,對單詞進行自小到大的排序。Input
輸入數據有多組,每組的第1行為n,表示該組有n個單詞(n最多為200,且每個單詞的長度不超過20),接下來是具體的n個單詞;Output
輸出數據有多行,每組n行,輸出該組單詞中自小到大排好序的單詞。Sample Input
5bananaappleorangepearpeach4greenblackwhiteredSample Output
applebananaorangepeachpearblackgreenredwhiteHint
難度系數 2分;簡單 本題目考核知識點: 考點1:二維數組的定義。 考點2: 字符串數組的使用。 考點3:排序算法的應用。利用C語言上課學的冒泡排序可以寫出以下代碼
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ int n,i,j; char t[20]; int a[200][20]; while(scanf("%d",&n)!=-1){ getchar(); for(i=0;i<n;i++){ gets(a[i]); } for(i=0;i<n-1;i++){ for(j=0;j<n-1-i;j++){ if(strcmp(a[j],a[j+1])>0){ strcpy(t,a[j]); strcpy(a[j],a[j+1]); strcpy(a[j+1],t); } } } for(i=0;i<n;i++){ puts(a[i]); } }}用這次的sort函數,代碼量大大減少#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;string s[201];int n,i;int main(){ while(~scanf("%d",&n)) { for(i=0;i<n;i++) cin>>s[i]; sort(s,s+n); for(i=0;i<n;i++) cout<<s[i]<<endl; } return 0;} 第二題阿里巴巴致富的秘密
Problem:B
Time Limit:1000ms
Memory Limit:65535K
Description
阿里巴巴每次來到強盜的藏寶藏的洞穴門口,都要破譯動門口的密碼;密碼由一些行的字符串構成,只要知道這些行字符串的順序,就可以順利的進入洞穴了,得到寶藏了!阿里巴巴冥思苦想,最后發現密碼就是對這些字符串的從小到大排序就行了。Input
輸入數據有多組,每組第1行為n(1<=n<=20);然后為n行的字符串(包含空格,字符串的個數小于100)Output
把這些串按照從小到大的順序輸出(輸出可以使用puts());Sample Input
3good kood bugi amdog dxSample Output
dog dxgood kood bugi am前一個題的升級版,用gets讀入即可
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ int n,i,j,k; char a[100][20]; char tmp[20]; while(scanf("%d",&n)!=-1) { getchar(); for(i=0;i<n;i++) gets(a[i]); for(i=0;i<n;i++) { strcpy(tmp,a[i]); for(j=i+1;j<n;j++) { if(strcmp(tmp,a[j])>0) { k=j; strcpy(tmp,a[j]); strcpy(a[k],a[i]); strcpy(a[i],tmp); } } } for(i=0;i<n;i++) { puts(a[i]); //printf("/n"); } } //printf("Hello world!/n"); return 0;}依舊是 sort要短很多新出現的c++函數 可以自己查閱相關資料 鍛煉一下自學能力~~
#include <iostream>#include <string>#include <algorithm>using namespace std;string a[105];int main(){ int n; while(cin>>n) { cin.get(); for(int i=0;i<n;i++) getline(cin,a[i]); sort(a,a+n); for(int i=0;i<n;i++) cout<<a[i]<<endl; } return 0;}第三題Yukun的字符串
Problem:C
Time Limit:1000ms
Memory Limit:65536K
Description
話說在上一場訓練中,zhouyukun學長已經給大家講解了字符串的有關知識,下面我們來復習一下。給你一個字符串讓你對字符串中所有的字符按照從ASXII碼大到小的順序排序并輸出。簡單吧,那就來吧!Input
輸入一個字符串s(只含有英文大小寫),長度(1<=len<=100000)Output
輸出排序好的字符串Sample Input
ababacSample Output
cbbaaa排序字符串 小case啊
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;char a[100005];int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); while(~scanf("%s",a)) { int n=strlen(a); sort(a,a+n); for(int i=n-1;i>=0;i--) printf("%c",a[i]); printf("/n"); } return 0;}第四題排序
Problem:D
Time Limit:1000ms
Memory Limit:65536K
Description
輸入一行數字,如果我們把這行數字中的‘5’都看成空格,那么就得到一行用空格分割的若干非負整數(可能有些整數以‘0’開頭,這些頭部的‘0’應該被忽略掉,除非這個整數就是由若干個‘0’組成的,這時這個整數就是0)。 你的任務是:對這些分割得到的數字,依從小到大的順序排序輸出Input
輸入包含多組測試用例,每組輸入數據只有一行數字(數字之間沒有空格),這行數字的長度不大于1000。 輸入數據保證:分割得到的非負整數長度不大于100;輸入數據不可能全由‘5’組成。Output
對于每個測試用例,輸出分割得到的整數排序的結果,相鄰的兩個整數之間用一個空格分開,每組輸出占一行。Sample Input
0051231232050775Sample Output
0 77 12312320涉及到部分結構體的知識 就當作預習吧····我出這題的本意是讓大家用二維數組儲存的……你們試著改改吧
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { char s[110]; }nt[1010]; bool cmp(node a,node b) { int alen=strlen(a.s),blen=strlen(b.s); if(alen<blen) { return true; } else if(alen>blen) { return false; } else { if(strcmp(a.s,b.s)<0) { return true; } else { return false; } } } int main() { //freopen("g://data.in", "r", stdin); //freopen("g://data.out", "w", stdout); char s[1010],st[110]; memset(st,0,sizeof(st)); while(scanf("%s",s)!=EOF) { int len=strlen(s); int k=0,tlen=0; for(int i=0;i<=len;i++) if(i==len||s[i]=='5') //遇到5和讀到整體的最后一個字符都需結束并輸出 { if(tlen==0) continue; //提前結束本次存儲數組的循環,因為開頭就是5 int d=0; for(d=0;d<tlen-1;d++) if(st[d]!='0') //st[]是小段的數組 { break; //哪位開始不等于0,則從那位開始復制 } strcpy(nt[k++].s,st+d); //把小段的數組st復制給結構體數組nt[k++] memset(st,0,sizeof(st)); //數組st隨時清零更新 tlen=0; } else { st[tlen++]=s[i]; //在沒遇到5的情況下,逐個的字符復制給小段的數組的對應位數 } sort(nt,nt+k,cmp); printf("%s",nt[0].s); //為了保證開頭沒有空格,其余中間分割有空格的寫法 for(int i=1;i<k;i++) { printf(" %s",nt[i].s); } puts(""); } return 0; }終于改好的代碼……
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{ char s[110];}nt[1010];char str[1010][110];bool cmp( char a[],char b[]){ int alen=strlen(a),blen=strlen(b); if(alen<blen) return true; else if(alen>blen) return false; else { if(strcmp(a,b)<0)return true; return false; }}int main(){// freopen("data.in", "r", stdin); //freopen("g://data.out", "w", stdout); char s[1010],st[1100]; memset(st,0,sizeof(st)); while(cin>>s) { if(strlen(s)==0)break; int len=strlen(s); int k=0,tlen=0; for(int i=0;i<=len;i++) { if(i==len||s[i]=='5') //遇到5和讀到整體的最后一個字符都需結束并輸出 { if(tlen==0) continue; //提前結束本次存儲數組的循環,因為開頭就是5 int d=0; for(d=0;d<tlen-1;d++) if(st[d]!='0') //st[]是小段的數組 break; //哪位開始不等于0,則從那位開始復制 strcpy(str[k],st+d); k++; for(int j=0;j<1009;j++)st[j]='/0'; tlen=0; } else { st[tlen++]=s[i]; //在沒遇到5的情況下,逐個的字符復制給小段的數組的對應位數 } } for(int i=0;i<k-1;i++) { for(int j=0;j<k-1-i;j++) if(!cmp(str[j],str[j+1])) { char tmp[1100]; strcpy(tmp,str[j]); strcpy(str[j],str[j+1]); strcpy(str[j+1],tmp); } // sort(nt,nt+k,cmp); // printf("%s",nt[0].s); //為了保證開頭沒有空格,其余中間分割有空格的寫法 } cout<<str[0]; for(int i=1;i<k;i++) { //printf(" %s",nt[i].s); cout<<" "<<str[i]; } puts(""); } return 0;}附上一份比賽里提交的代碼:
#include <stdio.h>#include <string.h>int bj(char a[],char b[]){ int i; if(strlen(a)==strlen(b)) { for(i=0;a[i]==b[i]&&a[i]!='/0';i++); return a[i]>b[i]; } return strlen(a)>strlen(b);}void px(char a[501][101],int n){ int i,j,k; char t[101]; for(i=0; i<n-1; i++) for(j=0; j<n-1-i; j++) if(bj(a[j],a[j+1])) { for(k=0; a[j][k]!='/0'; k++) t[k]=a[j][k]; t[k]='/0'; for(k=0; a[j+1][k]!='/0'; k++) a[j][k]=a[j+1][k]; a[j][k]='/0'; for(k=0; t[k]!='/0'; k++) a[j+1][k]=t[k]; a[j+1][k]='/0'; }}int main(){ char a[1001]; char b[501][101]; int i,k,j; while(gets(a)!=NULL) { j=0; for(i=0; a[i]!='/0';) { while(a[i]=='5') i++; if(i>=strlen(a)) break; while(a[i]=='0') i++; if(a[i]=='5'||a[i]=='/0') { b[j][0]='0'; b[j][1]='/0'; j++; } else { for(k=0; a[i]!='5'&&a[i]!='/0'; k++,i++) b[j][k]=a[i]; b[j][k]='/0'; j++; } } px(b,j); printf("%s",b[0]); for(i=1;i<j;i++) printf(" %s",b[i]); printf("/n"); } return 0;}
新聞熱點
疑難解答