国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

2016級(jí)ACM寒假訓(xùn)練(二)

2019-11-10 19:04:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

第一題

單詞排序

PRoblem:A

Time Limit:1000ms

Memory Limit:65535K

Description

給定一組單詞,對(duì)單詞進(jìn)行自小到大的排序。

Input

輸入數(shù)據(jù)有多組,每組的第1行為n,表示該組有n個(gè)單詞(n最多為200,且每個(gè)單詞的長(zhǎng)度不超過(guò)20),接下來(lái)是具體的n個(gè)單詞;

Output

 輸出數(shù)據(jù)有多行,每組n行,輸出該組單詞中自小到大排好序的單詞。

Sample Input

5bananaappleorangepearpeach4greenblackwhitered

Sample Output

applebananaorangepeachpearblackgreenredwhite

Hint

難度系數(shù) 2分;簡(jiǎn)單 本題目考核知識(shí)點(diǎn):   考點(diǎn)1:二維數(shù)組的定義。   考點(diǎn)2: 字符串?dāng)?shù)組的使用。   考點(diǎn)3:排序算法的應(yīng)用。

利用C語(yǔ)言上課學(xué)的冒泡排序可以寫(xiě)出以下代碼

#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函數(shù),代碼量大大減少

#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

阿里巴巴每次來(lái)到強(qiáng)盜的藏寶藏的洞穴門(mén)口,都要破譯動(dòng)門(mén)口的密碼;密碼由一些行的字符串構(gòu)成,只要知道這些行字符串的順序,就可以順利的進(jìn)入洞穴了,得到寶藏了!阿里巴巴冥思苦想,最后發(fā)現(xiàn)密碼就是對(duì)這些字符串的從小到大排序就行了。

Input

輸入數(shù)據(jù)有多組,每組第1行為n(1&lt;=n&lt;=20);然后為n行的字符串(包含空格,字符串的個(gè)數(shù)小于100)

Output

把這些串按照從小到大的順序輸出(輸出可以使用puts());

Sample Input

3good kood bugi amdog dx

Sample Output

dog dxgood kood bugi am

前一個(gè)題的升級(jí)版,用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要短很多

新出現(xiàn)的c++函數(shù) 可以自己查閱相關(guān)資料 鍛煉一下自學(xué)能力~~

#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

話說(shuō)在上一場(chǎng)訓(xùn)練中,zhouyukun學(xué)長(zhǎng)已經(jīng)給大家講解了字符串的有關(guān)知識(shí),下面我們來(lái)復(fù)習(xí)一下。給你一個(gè)字符串讓你對(duì)字符串中所有的字符按照從ASXII碼大到小的順序排序并輸出。簡(jiǎn)單吧,那就來(lái)吧!

Input

輸入一個(gè)字符串s(只含有英文大小寫(xiě)),長(zhǎng)度(1&lt;=len&lt;=100000)

Output

輸出排序好的字符串

Sample Input

ababac

Sample 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

輸入一行數(shù)字,如果我們把這行數(shù)字中的‘5’都看成空格,那么就得到一行用空格分割的若干非負(fù)整數(shù)(可能有些整數(shù)以‘0’開(kāi)頭,這些頭部的‘0’應(yīng)該被忽略掉,除非這個(gè)整數(shù)就是由若干個(gè)‘0’組成的,這時(shí)這個(gè)整數(shù)就是0)。	你的任務(wù)是:對(duì)這些分割得到的數(shù)字,依從小到大的順序排序輸出

Input

輸入包含多組測(cè)試用例,每組輸入數(shù)據(jù)只有一行數(shù)字(數(shù)字之間沒(méi)有空格),這行數(shù)字的長(zhǎng)度不大于1000。  	輸入數(shù)據(jù)保證:分割得到的非負(fù)整數(shù)長(zhǎng)度不大于100;輸入數(shù)據(jù)不可能全由‘5’組成。

Output

對(duì)于每個(gè)測(cè)試用例,輸出分割得到的整數(shù)排序的結(jié)果,相鄰的兩個(gè)整數(shù)之間用一個(gè)空格分開(kāi),每組輸出占一行。

Sample Input

0051231232050775

Sample Output

0 77 12312320

涉及到部分結(jié)構(gòu)體的知識(shí) 就當(dāng)作預(yù)習(xí)吧····我出這題的本意是讓大家用二維數(shù)組儲(chǔ)存的……你們?cè)囍母陌?/p>

#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和讀到整體的最后一個(gè)字符都需結(jié)束并輸出        {              if(tlen==0) continue;  //提前結(jié)束本次存儲(chǔ)數(shù)組的循環(huán),因?yàn)殚_(kāi)頭就是5            int d=0;              for(d=0;d<tlen-1;d++)              if(st[d]!='0')  //st[]是小段的數(shù)組            {                  break;  //哪位開(kāi)始不等于0,則從那位開(kāi)始復(fù)制            }              strcpy(nt[k++].s,st+d); //把小段的數(shù)組st復(fù)制給結(jié)構(gòu)體數(shù)組nt[k++]            memset(st,0,sizeof(st));  //數(shù)組st隨時(shí)清零更新            tlen=0;          }          else          {              st[tlen++]=s[i];  //在沒(méi)遇到5的情況下,逐個(gè)的字符復(fù)制給小段的數(shù)組的對(duì)應(yīng)位數(shù)        }          sort(nt,nt+k,cmp);          printf("%s",nt[0].s);  //為了保證開(kāi)頭沒(méi)有空格,其余中間分割有空格的寫(xiě)法        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和讀到整體的最后一個(gè)字符都需結(jié)束并輸出                {                    if(tlen==0) continue;  //提前結(jié)束本次存儲(chǔ)數(shù)組的循環(huán),因?yàn)殚_(kāi)頭就是5                    int d=0;                    for(d=0;d<tlen-1;d++)                        if(st[d]!='0')  //st[]是小段的數(shù)組                            break;  //哪位開(kāi)始不等于0,則從那位開(kāi)始復(fù)制                    strcpy(str[k],st+d);                    k++;                    for(int j=0;j<1009;j++)st[j]='/0';                    tlen=0;                }                else                {                    st[tlen++]=s[i];  //在沒(méi)遇到5的情況下,逐個(gè)的字符復(fù)制給小段的數(shù)組的對(duì)應(yīng)位數(shù)                }        }        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);  //為了保證開(kāi)頭沒(méi)有空格,其余中間分割有空格的寫(xiě)法        }        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;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 恩施市| 定西市| 江孜县| 阳朔县| 南宫市| 鄂温| 黔西县| 翁源县| 河北省| 泰安市| 永胜县| 麦盖提县| 连城县| 建始县| 九江市| 铅山县| 阳新县| 昌图县| 承德市| 鄂温| 怀集县| 顺昌县| 河北省| 兴化市| 彰化县| 怀化市| 岢岚县| 雷山县| 平和县| 东乡县| 玉环县| 环江| 抚宁县| 都江堰市| 罗平县| 河西区| 隆回县| 肇州县| 五峰| 东海县| 边坝县|