当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C语言经典程序100例

C语言经典程序100例

2018年10月04日  | 移动技术网IT编程  | 我要评论

李春天的春天全集下载,女子疑被当小三遭当街扒光衣服群殴,挪用资金罪量刑标准

 --------------------------------------------------------------------------------

【程序1】

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月

   后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

2.程序源代码:

#include<stdio.h>

void main(){

         long f1,f2;                                     //前两个月的兔子数

         f1=f2=1;

         for(int i=1;i<=20;i++){                         //i为月份

                   printf("%12ld %12ld ",f1,f2);

                   if(i%2==0) printf("\n");              //每行输出4个

                   f1=f1+f2;                   //前两个月加起来赋值给第三个月

                   f2=f2+f1;

         }

}

/*

           1            1            2            3

           5            8           13           21

          34           55           89          144

         233          377          610          987

        1597         2584         4181         6765

       10946        17711        28657        46368

       75025       121393       196418       317811

      514229       832040      1346269      2178309

     3524578      5702887      9227465     14930352

    24157817     39088169     63245986    102334155

press any key to continue

*/

==============================================================

【程序2】

题目:判断101-200之间有多少个素数,并输出所有素数。

1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,

      则表明此数不是素数,反之是素数。       

2.程序源代码:

#include<stdio.h>

#include<math.h>

void main(){

         int k=0,leap=1;

         for(int n=101;n<=200;n++){              //101--200

                   for(int i=2;i<=sqrt(n);i++){         //2--sqrt(i)

                            if(n%i==0){

                                     leap=0;

                                     break;

                            }

                   }

                   if(leap){

                            printf("%-4d",n);

                            k++;

                            if(k%10==0) printf("\n");

                   }

                   leap=1;

         }

         printf("\nthe total is %d\n",k);

}

/*

101 103 107 109 113 127 131 137 139 149

151 157 163 167 173 179 181 191 193 197

199

the total is 21

press any key to continue

*/

==============================================================

【程序3】

题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数

   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

2.程序源代码:

#include<stdio.h>

void main(){

         int a,b,c;

         int n;

         printf("water flower'munber is: ");

         for(n=100;n<=999;n++){

                   a=n/100;  //百位

                   b=n%100/10;   //十位

                   c=n%10;            //个位

                   if(n=a*a*a+b*b*b+c*c*c){

                            printf("%5d ",n);

                   }

         }

         printf("\n");

}

/*

water flower'munber is:     1     8   729   370   371   378  1099

press any key to continue

*/

==============================================================

【程序4】

题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,

 重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

2.程序源代码:

#include<stdio.h>

void main(){

         int n;

         printf("please input a number: ");

         scanf("%d",&n);

         printf("%d= ",n);

         for(int i=2;i<=n;i++){

                   while(n!=i){

                            if(n%i==0){

                                     printf("%d * ",i);

                                     n=n/i;

                            }else

                                     break;

                   }

         }

         printf("%d",n);

         printf("\n");

}

/*

please input a number: 90

90= 2 * 3 * 3 * 5

press any key to continue

*/

==============================================================

【程序5】

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用a表示,60-89分之间的用b表示,

   60分以下的用c表示。

1.程序分析:(a>b)?a:b这是条件运算符的基本例子。

2.程序源代码:

#include<stdio.h>

void main(){

         int score;

         char grade;

         printf("please input a score: ");

         scanf("%d",&score);

         grade=score>=90?'a':(score>=60?'b':'c');

         printf("%d belongs to %c \n",score,grade);

}

/*

please input a score: 91

91 belongs to a

press any key to continue

 

please input a score: 87

87 belongs to b

press any key to continue

 

please input a score: 50

50 belongs to c

press any key to continue

*/

==============================================================

【程序6】

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

1.程序分析:利用辗除法。

2.程序源代码:

#include<stdio.h>

void main(){

         int m,n,temp,a,b;

         printf("please input two numbers: ");

         scanf("%d %d",&m,&n);

         if(m<n){

                   temp=m;

                   m=n;

                   n=temp;

         }

         a=m;

         b=n;

         while(b!=0){

                   temp=a%b;

                   a=b;

                  b=temp;

         }

         printf("最大公约数为:%d\n",a);

         printf("最小公倍数为:%d\n",n*m/a);

}

/*

please input two numbers: 12 3

最大公约数为:3

最小公倍数为:12

press any key to continue

*/

==============================================================

【程序7】

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

1.程序分析:利用while语句,条件为输入的字符不为'\n'.

2.程序源代码:

#include<stdio.h>

void main(){

         char c;

         int letters=0,space=0,digit=0,others=0;  //字母、空格、数字、其他字符

         printf("please input some characters\n");

         while((c=getchar())!='\n'){

                   if((c>='a'&&c<='z')||(c>='a'&&c<='z'))

                            letters++;

                   else if(c==' ')

                            space++;

                   else if(c>='0'&&c<='9')

                            digit++;

                   else

                            others++;

         }

         printf("all in all: english letters=%d space=%d digit=%d others=%d\n",letters,space,digit,others);

}

/*

please input some characters

qj3409v3o teu40t93ejt934 34erj%j*a4

all in all: english letters=16 space=2 digit=15 others=2

press any key to continue

*/

==============================================================

【程序8】

题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时

   共有5个数相加),几个数相加有键盘控制。

1.程序分析:关键是计算出每一项的值。

2.程序源代码:

#include<stdio.h>

void main(){

         int a,n,count=1;   //数字a,n个数相加

         long sn=0,tn=0;

         printf("please input a and n: ");

         scanf("%d %d",&a,&n);

         printf("a=%d,n=%d\n",a,n);

         while(count<=n){

                   tn=tn+a;

                   sn=sn+tn;

                   a=a*10;

                   ++count;

         }

         printf("a+aa+...=%d\n",sn);

}

/*

please input a and n: 3 4

a=3,n=4

a+aa+...=3702

press any key to continue

*/

==============================================================

【程序9】

题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程

   找出1000以内的所有完数。

1. 程序分析:请参照程序<--上页程序14.

2.程序源代码:

#include<stdio.h>

void main(){

         int k[10];

         int n,s,i,m;

         for(n=2;n<1000;n++){

                   i=-1;

                   s=n;

                   for(m=1;m<n;m++){

                            if(n%m==0){

                                     i++;

                                     s=s-m;

                                     k[i]=m;

                            }

                   }

                   if(s==0){

                            printf("%d is a wanshu\n",n);

                   }

         }

}

/*

6 is a wanshu

28 is a wanshu

496 is a wanshu

press any key to continue

*/

main()

{

static int k[10];

int i,j,n,s;

for(j=2;j<1000;j++)

 {

 n=-1;

 s=j;

  for(i=1;i   {

   if((j%i)==0)

   { n++;

    s=s-i;

    k[n]=i;

   }

  }

 if(s==0)

 {

 printf("%d is a wanshu",j);

 for(i=0;i  printf("%d,",k[i]);

 printf("%d\n",k[n]);

 }

}

}

==============================================================

【程序10】

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在

   第10次落地时,共经过多少米?第10次反弹多高?

1.程序分析:见下面注释

2.程序源代码:

#include<stdio.h>

void main(){

         float sn=100.0,hn=sn/2;

         int n;

         for(n=2;n<=10;n++){

                   sn=sn+2*hn;              //第n次落地时共经过的米数

                   hn=sn/2;            //第n次反弹高度

         }

         printf("the total of road is %f \n",sn);

         printf("the tenth is %f meters\n",hn);

}

/*

the total of road is 51200.000000

the tenth is 25600.000000 meters

press any key to continue

*/

=============================================================

【程序11】

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

2.程序源代码:

#include<stdio.h>

void main(){

         int i,j,k;

         int n=0;

         for(i=1;i<=4;i++){  //以下为三重循环

                   for(j=1;j<=4;j++){

                            for(k=1;k<=4;k++){

                                     if(i!=j&&j!=k&&i!=k){  //排除i、j、k相同的情况

                                               printf("%d,%d,%d     ",i,j,k);

                                               printf("the number is %d%d%d\n",i,j,k);

                                               n++;

                                     }

                            }

                   }

         }

         printf("\na total of number is %d\n",n);

}

/*

1,2,3     the number is 123

1,2,4     the number is 124

1,3,2     the number is 132

1,3,4     the number is 134

1,4,2     the number is 142

1,4,3     the number is 143

2,1,3     the number is 213

2,1,4     the number is 214

2,3,1     the number is 231

2,3,4     the number is 234

2,4,1     the number is 241

2,4,3     the number is 243

3,1,2     the number is 312

3,1,4     the number is 314

3,2,1     the number is 321

3,2,4     the number is 324

3,4,1     the number is 341

3,4,2     the number is 342

4,1,2     the number is 412

4,1,3     the number is 413

4,2,1     the number is 421

4,2,3     the number is 423

4,3,1     the number is 431

4,3,2     the number is 432

 

a total of number is 24

press any key to continue

*/

==============================================================

【程序12】

题目:企业发放的奖金根据利润提成。利润(i)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润i,求应发放奖金总数?

1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。      

2.程序源代码:

#include<stdio.h>

void main(){

         long int i;

         int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;

         printf("please input the profit i:");

         scanf("%ld",&i);

         bonus1=100000*0.1;

         bonus2=bonus1+100000*0.075;

         bonus4=bonus2+200000*0.05;

         bonus6=bonus4+200000*0.03;

         bonus10=bonus6+400000*0.015;

         if(i<=100000)

                   bonus=i*0.1;

         else if(i<=200000)

                   bonus=bonus1+(i-100000)*0.075;

         else if(i<=400000)

                   bonus=bonus2+(i-200000)*0.05;

         else if(i<=600000)

                   bonus=bonus4+(i-400000)*0.03;

         else if(i<=1000000)

                   bonus=bonus6+(i-600000)*0.015;

         else

                   bonus=bonus10+(i-1000000)*0.01;

         printf("bonus=%d\n",bonus);

}

/*

please input the profit i:340000

bonus=24500

press any key to continue

*/

==============================================================

【程序13】

题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:

2.程序源代码:

#include<stdio.h>

#include<math.h>

void main(){

         long int i,x,y;

         for(i=1;i<100000;i++){

                   x=sqrt(i+100);//x为加上100后开方后的结果

                   y=sqrt(i+268);//y为再加上168后开方后的结果

                   if(x*x==i+100&&y*y==i+268)//如果一个数的平方根的平方等于该数,这说明此数是完全平方数

                            printf("the number is %ld\n",i);

         }

}

/*

the number is 21

the number is 261

the number is 1581

press any key to continue

*/

==============================================================

【程序14】

题目:输入某年某月某日,判断这一天是这一年的第几天?

1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

2.程序源代码:

#include<stdio.h>

void main(){

         int day,month,year,sum,leap;

         printf("please input year,month,day: ");

         scanf("%d %d %d",&year,&month,&day);

         switch(month){ //先计算某月以前月份的总天数

                   case 1:sum=0;break;

                   case 2:sum=31;break;

                   case 3:sum=59;break;

                   case 4:sum=90;break;

                   case 5:sum=120;break;

                   case 6:sum=151;break;

                   case 7:sum=181;break;

                   case 8:sum=212;break;

                   case 9:sum=243;break;

                   case 10:sum=273;break;

                   case 11:sum=304;break;

                   case 12:sum=334;break;

                   default:printf("data error");break;

         }

         sum=sum+day;  //再加上某天的天数

         if((year%4==0&&year%100!=0)||(year%400==0)) //判断是否是闰年

                   leap=1;

         else

                   leap=0;

         if(leap==1&&month>2)     //如果是闰年且月份大于2,总天数应该加一天

                   sum++;

         printf("it is the %dth day.\n",sum);

}

/*

please input year,month,day: 2019 3 1

it is the 60th day.

press any key to continue

*/

==============================================================

【程序15】

题目:输入三个整数x,y,z,请把这三个数由小到大输出。

1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

2.程序源代码:

#include<stdio.h>

void main(){

         int x,y,z,temp;

         printf("please input three numbers:");

         scanf("%d %d %d",&x,&y,&z);

         if (x>y){   //交换x,y的值

                   temp=x;

                  x=y;

                   y=temp;

         }

         if(x>z){     //交换x,z的值

                   temp=x;

                   x=z;

                   z=temp;

         }

         if(y>z){     //交换z,y的值

                   temp=y;

                   y=z;

                   z=temp;

         }

         printf("small to big: %d %d %d\n",x,y,z);

}

/*

please input three numbers:34 13 23

small to big: 13 23 34

press any key to continue

*/

==============================================================

【程序16】

题目:用*号输出字母c的图案。

1.程序分析:可先用'*'号在纸上写出字母c,再分行输出。

2.程序源代码:

#include <stdio.h>

void main(){

         printf("hello c-world!\n");

         printf(" ****\n");

         printf(" *\n");

         printf(" * \n");

         printf(" ****\n");

}

/*

hello c-world!

 ****

 *

 *

 ****

press any key to continue

*/

==============================================================

【程序17】

题目:输出特殊图案,请在c环境中运行,看一看,very beautiful!

1.程序分析:字符共有256个。不同字符,图形不一样。      

2.程序源代码:

#include <stdio.h>

void main(){

         char a=32,b=64;

         printf("%c%c%c%c%c\n",b,a,a,a,b);

         printf("%c%c%c%c%c\n",a,b,a,b,a);

         printf("%c%c%c%c%c\n",a,a,b,a,a);

         printf("%c%c%c%c%c\n",a,b,a,b,a);

         printf("%c%c%c%c%c\n",b,a,a,a,b);

}

/*

@   @

 @ @

  @

 @ @

@   @

press any key to continue

*/

==============================================================

【程序18】

题目:输出9*9口诀。

1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

2.程序源代码:

#include <stdio.h>

void main(){

         int i,j,result;

         for (i=1;i<=9;i++){

                   for(j=1;j<=9;j++){

                            result=i*j;

                            printf("%d*%d=%-3d",i,j,result);//-3d表示左对齐,占3位

                   }

                   printf("\n"); //每一行后换行

         }

}

/*

1*1=1  1*2=2  1*3=3  1*4=4  1*5=5  1*6=6  1*7=7  1*8=8  1*9=9

2*1=2  2*2=4  2*3=6  2*4=8  2*5=10 2*6=12 2*7=14 2*8=16 2*9=18

3*1=3  3*2=6  3*3=9  3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27

4*1=4  4*2=8  4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36

5*1=5  5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45

6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54

7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63

8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72

9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

press any key to continue

*/

==============================================================

【程序19】

题目:要求输出国际象棋棋盘。

1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。

2.程序源代码:

#include<stdio.h>

void main(){

         int i,j;

         for(i=0;i<8;i++){

                   for(j=0;j<8;j++){

                            if((i+j)%2==0)

                                     printf("%c%c",219,219);

                            else

                                     printf(" ");

                   }

                   printf("\n");

         }

}

/*圹 圹 圹 圹

 圹 圹 圹 圹

圹 圹 圹 圹

 圹 圹 圹 圹

圹 圹 圹 圹

 圹 圹 圹 圹

圹 圹 圹 圹

 圹 圹 圹 圹

press any key to continue

*/

==============================================================

【程序20】

题目:打印楼梯,同时在楼梯上方打印两个笑脸。

1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。

2.程序源代码:

#include<stdio.h>

void main(){

         int i,j;

         printf("\1\1\n"); //输出两个笑脸

         for(i=1;i<11;i++){

                   for(j=1;j<=i;j++){

                            printf("%c%c",219,219);

                   }

                   printf("\n");

         }

}

/*

..

圹圹

圹圹圹

圹圹圹圹

圹圹圹圹圹

圹圹圹圹圹圹

圹圹圹圹圹圹圹

圹圹圹圹圹圹圹圹

圹圹圹圹圹圹圹圹圹

圹圹圹圹圹圹圹圹圹圹

press any key to continue

*/

============================================================

【程序21】

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个

   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下

   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

1.程序分析:采取逆向思维的方法,从后往前推断。

2.程序源代码:

#include<stdio.h>

void main(){

         int day,x1,x2=1;

         for(day=9;day>0;day--){

                   x1=(x2+1)*2;  //第一天的桃子数是第2天桃子数加1后的2倍

                   x2=x1;

         }

         printf("the total is %d\n",x1);

}

/*

the total is 1534

press any key to continue

*/

==============================================================

【程序22】

题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定

   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出

   三队赛手的名单。

1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,

      则表明此数不是素数,反之是素数。       

2.程序源代码:

#include<stdio.h>

void main(){

         char i,j,k;  //i是a的对手,j是b的对手,k是c的对手

         for(i='x';i<='z';i++){

                   for(j='x';j<='z';j++){

                            if(i!=j){

                                     for(k='x';k<='z';k++){

                                               if(i!=k&&j!=k){

                                                        if(i!='x'&&k!='x'&&k!='z')

                                                                 printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);

                                               }

                                     }

                            }

                   }

         }

}

/*

order is a--z   b--x    c--y

press any key to continue

*/

==============================================================

【程序23】

题目:打印出如下图案(菱形)

 

*

***

******

********

******

***

*

1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重

      for循环,第一层控制行,第二层控制列。

2.程序源代码:

#include<stdio.h>

void main(){

         int i,j,k;

         for(i=0;i<=3;i++){

                   for(j=0;j<=2-i;j++){

                            printf(" ");

                   }

                   for(k=0;k<=2*i;k++){

                            printf("*");

                   }

                   printf("\n");

         }

         for(i=0;i<=2;i++){

                   for(j=0;j<=i;j++){

                            printf(" ");

                   }

                   for(k=0;k<=4-2*i;k++){

                            printf("*");

                   }

                   printf("\n");

         }

}

/*

   *

  ***

 *****

*******

 *****

  ***

   *

press any key to continue

*/

==============================================================

【程序24】

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

1.程序分析:请抓住分子与分母的变化规律。

2.程序源代码:

#include<stdio.h>

void main(){

         int n,t;

         float a=2,b=1,s=0;

         for(n=1;n<=20;n++){

                   s=s+a/b;

                   t=a;

                   a=a+b;

                   b=t;

         }

         printf("sum is %9.6f\n",s);

}

/*

sum is 32.660259

press any key to continue

*/

==============================================================

【程序25】

题目:求1+2!+3!+...+20!的和

1.程序分析:此程序只是把累加变成了累乘。

2.程序源代码:

#include<stdio.h>

void main(){

         float n,s=0,t=1;

         for(n=1;n<=20;n++){

                   t*=n;

                   s+=t;

         }

         printf("1+2!+3!...+20!=%e\n",s);

}

/*

1+2!+3!...+20!=2.561327e+018

press any key to continue

*/

==============================================================

【程序26】

题目:利用递归方法求5!。

1.程序分析:递归公式:fn=fn_1*4!

2.程序源代码:

#include<stdio.h>

int fact(int j){

         int sum;

         if(j==0)

                   sum=1;

         else

                   sum=j*fact(j-1);

         return sum;

}

void main(){

         int i;

         int fact(int);

         for(i=0;i<6;i++)

                   printf(" %d!=%d\n",i,fact(i));

}

/*

 0!=1

 1!=1

 2!=2

 3!=6

 4!=24

 5!=120

press any key to continue

*/

==============================================================

【程序27】

题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

1.程序分析:

2.程序源代码:

#include<stdio.h>

void main(){

         int i=5;

         void palin(int n);

         printf("please input five characters: ");

         palin(i);

         printf("\n");

}

void palin(int n){

         char next;

         if(n<=1){

                   next=getchar();

                   printf("the opposite characters is: ");

                   putchar(next);

         }

         else{

                   next=getchar();

                   palin(n-1);

                   putchar(next);

         }

}

/*

please input five characters: rterd

the opposite characters is: dretr

press any key to continue

*/

==============================================================

【程序28】

题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第

   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后

   问第一个人,他说是10岁。请问第五个人多大?

1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道

      第四人的岁数,依次类推,推到第一人(10岁),再往回推。

2.程序源代码:

#include<stdio.h>

age(int n){

         int c;

         if(n==1)

                   c=10;

         else

                   c=age(n-1)+2;

         return c;

}

void main(){

         printf("the age of the five is: %d\n",age(5));

}

/*

the age of the five is: 18

press any key to continue

*/

==============================================================

【程序29】

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)

2.程序源代码:

#include<stdio.h>

void main( ){

         long a,b,c,d,e,x;

         printf("please input the number: ");

         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);

}

/*

please input the number: 23458

there are 5,  8 5 4 3 2

press any key to continue

*/

==============================================================

【程序30】

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   

1.程序分析:同29例

2.程序源代码:

#include<stdio.h>

void main( ){

    

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网