<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    C語言經典算法27-37

    發布時間:2017-10-7 15:18    發布者:ludi
    關鍵詞: 語言 , 嵌入式
    【程序27
    題目:利用遞歸函數調用方式,將所輸入的5個字符,以相反順序打印出來。
    1.程序分析:
    2.程序源代碼:
    #include "stdio.h"
    main()
    {
    int i=5;
    void palin(int n);
    printf("\40:");
    palin(i);
    printf("\n");
    }
    void palin(n)
    int n;
    {
    char next;
    if(n<=1)
     {
     next=getchar();
     printf("\n\0:");
     putchar(next);
     }
    else
     {
     next=getchar();
     palin(n-1);
     putchar(next);
     }
    }
    ==============================================================
    【程序28
    題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第
       3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后
       問第一個人,他說是10歲。請問第五個人多大?
    1.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數,需知道
          第四人的歲數,依次類推,推到第一人(10歲),再往回推。
    2.程序源代碼:
    age(n)
    int n;
    {
    int c;
    if(n==1) c=10;
    else c=age(n-1)+2;
    return(c);
    }
    main()
    { printf("%d",age(5));
    }
    ==============================================================
    【程序29
    題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
    1. 程序分析:學會分解出每一位數,如下解釋:(這里是一種簡單的算法,師專數002班趙鑫提供)
    2.程序源代碼:
    main( )
    {
    long a,b,c,d,e,x;
    scanf("%ld",&x);
    a=x/10000;/*分解出萬位*/
    b=x%10000/1000;/*分解出千位*/
    c=x%1000/100;/*分解出百位*/
    d=x%100/10;/*分解出十位*/
    e=x%10;/*分解出個位*/
    if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
    else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
      else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
        else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
          else if (e!=0) printf(" there are 1,%ld\n",e);
    }
    ==============================================================
    【程序30
    題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同!  
    1.程序分析:同29
    2.程序源代碼:
    main( )
    {
    long ge,shi,qian,wan,x;
    scanf("%ld",&x);
    wan=x/10000;
    qian=x%10000/1000;
    shi=x%100/10;
    ge=x%10;
    if (ge==wan&&shi==qian)/*個位等于萬位并且十位等于千位*/
     printf("this number is a huiwen\n");
    else
     printf("this number is not a huiwen\n");
    }
    【程序31
    題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續
       判斷第二個字母。
    1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。
    2.程序源代碼:
    #include
    void main()
    {
    char letter;
    printf("please input the first letter of someday\n");
    while ((letter=getch())!='Y')/*當所按字母為Y時才結束*/
    { switch (letter)
    {case 'S':printf("please input second letter\n");
         if((letter=getch())=='a')
          printf("saturday\n");
         else if ((letter=getch())=='u')
             printf("sunday\n");
           else printf("data error\n");
         break;
    case 'F':printf("friday\n");break;
    case 'M':printf("monday\n");break;
    case 'T':printf("please input second letter\n");
         if((letter=getch())=='u')
          printf("tuesday\n");
         else if ((letter=getch())=='h')
             printf("thursday\n");
           else printf("data error\n");
         break;
    case 'W':printf("wednesday\n");break;
    default: printf("data error\n");
      }
     }
    }
    ==============================================================
    【程序32
    題目:Press any key to change color, do you want to try it. Please hurry up!
    1.程序分析:            
    2.程序源代碼:
    #include
    void main(void)
    {
    int color;
    for (color = 0; color < 8; color++)
     {
     textbackground(color);/*設置文本的背景顏色*/
     cprintf("This is color %d\r\n", color);
     cprintf("Press any key to continue\r\n");
     getch();/*輸入字符看不見*/
     }
    }
    ==============================================================
    【程序33
    題目:學習gotoxy()clrscr()函數   
    1.程序分析:
    2.程序源代碼:
    #include
    void main(void)
    {
    clrscr();/*清屏函數*/
    textbackground(2);
    gotoxy(1, 5);/*定位函數*/
    cprintf("Output at row 5 column 1\n");
    textbackground(3);
    gotoxy(20, 10);
    cprintf("Output at row 10 column 20\n");
    }
    ==============================================================
    【程序34
    題目:練習函數調用
    1. 程序分析:
    2.程序源代碼:
    #include
    void hello_world(void)
    {
    printf("Hello, world!\n");
    }
    void three_hellos(void)
    {
    int counter;
    for (counter = 1; counter <= 3; counter++)
    hello_world();/*調用此函數*/
    }
    void main(void)
    {
    three_hellos();/*調用此函數*/
    }
    ==============================================================
    【程序35
    題目:文本顏色設置
    1.程序分析:
    2.程序源代碼:
    #include
    void main(void)
    {
    int color;
    for (color = 1; color < 16; color++)
     {
     textcolor(color);/*設置文本顏色*/
     cprintf("This is color %d\r\n", color);
     }
    textcolor(128 + 15);
    cprintf("This is blinking\r\n");
    }
    ==============================================================
    【程序36
    題目:求100之內的素數   
    1.程序分析:
    2.程序源代碼:
    #include
    #include "math.h"
    #define N 101
    main()
    {
    int i,j,line,a[N];
    for(i=2;ifor(i=2;i for(j=i+1;j {
      if(a!=0&&a[j]!=0)
      if(a[j]%a==0)
      a[j]=0;}
    printf("\n");
    for(i=2,line=0;i{
     if(a!=0)
     {printf("%5d",a);
     line++;}
     if(line==10)
     {printf("\n");
    line=0;}
    }
    }
    ==============================================================
    【程序37
    題目:對10個數進行排序
    1.程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換,
          下次類推,即用第二個元素與后8個進行比較,并進行交換。       
    2.程序源代碼:
    #define N 10
    main()
    {int i,j,min,tem,a[N];
    /*input data*/
    printf("please input ten num:\n");
    for(i=0;i{
    printf("a[%d]=",i);
    scanf("%d",&a);}
    printf("\n");
    for(i=0;iprintf("%5d",a);
    printf("\n");
    /*sort ten num*/
    for(i=0;i{min=i;
    for(j=i+1;jif(a[min]>a[j]) min=j;
    tem=a;
    a=a[min];
    a[min]=tem;
    }
    /*output data*/
    printf("After sorted \n");
    for(i=0;iprintf("%5d",a);
    }
    ==============================================================

    本文地址:http://www.portaltwn.com/thread-517505-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷