C語(yǔ)言經(jīng)典算法27-37

發(fā)布時(shí)間:2017-10-7 15:18    發(fā)布者:ludi
關(guān)鍵詞: 語(yǔ)言 , 嵌入式
【程序27
題目:利用遞歸函數調用方式,將所輸入的5個(gè)字符,以相反順序打印出來(lái)。
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個(gè)人坐在一起,問(wèn)第五個(gè)人多少歲?他說(shuō)比第4個(gè)人大2歲。問(wèn)第4個(gè)人歲數,他說(shuō)比第
   3個(gè)人大2歲。問(wèn)第三個(gè)人,又說(shuō)比第2人大兩歲。問(wèn)第2個(gè)人,說(shuō)比第一個(gè)人大兩歲。最后
   問(wèn)第一個(gè)人,他說(shuō)是10歲。請問(wèn)第五個(gè)人多大?
1.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個(gè)階段。要想知道第五個(gè)人歲數,需知道
      第四人的歲數,依次類(lèi)推,推到第一人(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
題目:給一個(gè)不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
1. 程序分析:學(xué)會(huì )分解出每一位數,如下解釋?zhuān)?/font>(這里是一種簡(jiǎn)單的算法,師專(zhuān)數002班趙鑫提供)
2.程序源代碼:
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出萬(wàn)位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出個(gè)位*/
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
題目:一個(gè)5位數,判斷它是不是回文數。即12321是回文數,個(gè)位與萬(wàn)位相同,十位與千位相同!  
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)/*個(gè)位等于萬(wàn)位并且十位等于千位*/
 printf("this number is a huiwen\n");
else
 printf("this number is not a huiwen\n");
}
【程序31
題目:請輸入星期幾的第一個(gè)字母來(lái)判斷一下是星期幾,如果第一個(gè)字母一樣,則繼續
   判斷第二個(gè)字母。
1.程序分析:用情況語(yǔ)句比較好,如果第一個(gè)字母一樣,則判斷用情況語(yǔ)句或if語(yǔ)句判斷第二個(gè)字母。
2.程序源代碼:
#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*當所按字母為Y時(shí)才結束*/
{ 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();/*輸入字符看不見(jiàn)*/
 }
}
==============================================================
【程序33
題目:學(xué)習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個(gè)數進(jìn)行排序
1.程序分析:可以利用選擇法,即從后9個(gè)比較過(guò)程中,選擇一個(gè)最小的與第一個(gè)元素交換,
      下次類(lèi)推,即用第二個(gè)元素與后8個(gè)進(jìn)行比較,并進(jìn)行交換。       
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://selenalain.com/thread-517505-1-1.html     【打印本頁(yè)】

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

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復 返回頂部 返回列表
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页