C語言作業9

來源:互聯網
上載者:User

標籤:百度   輸入   getch   goto   ++   pre   c語言   max   scanf   

1.編寫函數判斷使用者輸入的字元是否是對稱的字串(即順讀和倒讀都一樣的字串)。例如“XYZYX”和“xyzzyx”都是對稱的。

#include <stdio.h>#include <stdlib.h>int main(){    int f,i=0,a[100],n,j;    char x;    do    {        i++;        x=getchar();        a[i]=(int)(x);    }    while(a[i]!=10);    n=i;    if(n==3||n==4)    {        if(a[n-1]==a[1])            goto END1;        else            goto END2;    }    for(j=1; f&&j<=(n/2); j++)    {        f=0;        if(a[j]==a[n-j])            f=1;    }    if((float)j>=((float)(n)/2))        END1:printf("Yes\n");    else        END2:printf("No\n");    return 0;}

2. 編寫函數用冒泡排序法將使用者輸入的若干個數(或學生姓名)按升序排序。

#include <stdio.h>int main(){    int a[10];    int i,j,temp;    for(i=0;i<10;i++)        scanf("%d",&a[i]);    for(j=0;j<9;j++)        for(i=0;i<9-j;i++)        {            if(a[i]>a[i+1])            {                temp=a[i];                a[i]=a[i+1];                a[i+1]=temp;            }        }     for(i=0; i<10;i++)        printf("%d,",&a[i]);     printf("\n");     return 0;}

3. 編寫函數用選擇排序法將使用者輸入的若干個數(或學生姓名)按升序排序。

#include<stdio.h>int max(int a[]){    int i,j,t;    for(i=0;i<10;i++)        for(j=i+1;j<10;j++)        if (a[i]>a[j])       {        t=a[i];           a[i]=a[j];           a[j]=t;      }}int main(){     int a[10],i;     printf("輸入十個數字:");            for(i=0;i<10;i++)          scanf("%d",&a[i]);     max(a);     for(i=0;i<10;i++)           printf("%d  ",a[i]);     return 0; }

4. 編寫函數用插入排序法將使用者輸入的若干個數(或學生姓名)按升序排序。

#include<stdio.h>int main(){     int a[11],n,i,j;     printf("輸入10個數:");     for(i=1;i<=10;i++)         scanf("%d",&a[i]);     for(i=2;i<=10;i++)     {         a[0]=a[i];         for(j=i-1;a[j]>a[0];j--)         a[j+1]=a[j];         a[j+1]=a[0];     }     for(i=1;i<=10;i++)     printf("%d ",a[i]);     printf("\n");     return 0;}

到網上百度了好多...還問了好多同學..無論怎樣終於把作業寫出來了.感謝

C語言作業9

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.