【程序11】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月   后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 long f1,f2;
6 int i;
7 f1=f2=1;
8 for(i=1;i<=20;i++)
9 {
10 printf("%12ld %12ld",f1,f2);
11 if(i%2==0) printf("\n"); /*控制输出,每行四个*/
12 f1=f1+f2; /*前两个月加起来赋值给第三个月*/
13 f2=f1+f2; /*前两个月加起来赋值给第三个月*/
14 }
15 getch();
16 }



【程序12】题目:判断101-200之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,      则表明此数不是素数,反之是素数。       2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 #include "math.h"
4 main()
5 {
6 int m,i,k,h=0,leap=1;
7 printf("\n");
8 for(m=101;m<=200;m++)
9 {
10 k=sqrt(m+1);
11 for(i=2;i<=k;i++)
12 if(m%i==0)
13 {
14 leap=0;
15 break;
16 }
17 if(leap)
18 {
19 printf("%-4d",m);
20 h++;
21 if(h%10==0)
22 printf("\n");
23 }
24 leap=1;
25 }
26 printf("\nThe total is %d",h);
27 getch();
28 }



【程序13】题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int i,j,k,n;
6 printf("'water flower'number is:");
7 for(n=100;n<1000;n++)
8 {
9 i=n/100;/*分解出百位*/
10 j=n/10%10;/*分解出十位*/
11 k=n%10;/*分解出个位*/
12 if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
13 printf("%-5d",n);
14 }
15 getch();
16 }



【程序14】题目:将一个正整数分解质因数。例如:输入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.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int n,i;
6 printf("\nplease input a number:\n");
7 scanf("%d",&n);
8 printf("%d=",n);
9 for(i=2;i<=n;i++)
10 while(n!=i)
11 {
12 if(n%i==0)
13 {
14 printf("%d*",i);
15 n=n/i;
16 }
17 else
18 break;
19 }
20 printf("%d",n);
21 getch();
22 }



【程序15】题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,   60分以下的用C表示。1.程序分析:(a>b)?a:b这是条件运算符的基本例子。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int score;
6 char grade;
7 printf("please input a score\n");
8 scanf("%d",&score);
9 grade=score>=90?'A':(score>=60?'B':'C');
10 printf("%d belongs to %c",score,grade);
11 getch();
12 }



【程序16】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。1.程序分析:利用辗除法。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int a,b,num1,num2,temp;
6 printf("please input two numbers:\n");
7 scanf("%d,%d",&num1,&num2);
8 if(num1<num2)/*交换两个数,使大数放在num1上*/
9 {
10 temp=num1;
11 num1=num2;
12 num2=temp;
13 }
14 a=num1;b=num2;
15 while(b!=0)/*利用辗除法,直到b为0为止*/
16 {
17 temp=a%b;
18 a=b;
19 b=temp;
20 }
21 printf("gongyueshu:%d\n",a);
22 printf("gongbeishu:%d\n",num1*num2/a);
23 getch();
24 }



【程序17】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。1.程序分析:利用while语句,条件为输入的字符不为'\n'.2.程序源代码:  



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 char c;
6 int letters=0,space=0,digit=0,others=0;
7 printf("please input some characters\n");
8 while((c=getchar())!='\n')
9 {
10 if(c>='a'&&c<='z'||c>='A'&&c<='Z')
11 letters++;
12 else if(c==' ')
13 space++;
14 else if(c>='0'&&c<='9')
15 digit++;
16 else
17 others++;
18 }
19 printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
20 space,digit,others);
21 getch();
22 }



【程序18】题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时   共有5个数相加),几个数相加有键盘控制。1.程序分析:关键是计算出每一项的值。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int a,n,count=1;
6 long int sn=0,tn=0;
7 printf("please input a and n\n");
8 scanf("%d,%d",&a,&n);
9 printf("a=%d,n=%d\n",a,n);
10 while(count<=n)
11 {
12 tn=tn+a;
13 sn=sn+tn;
14 a=a*10;
15 ++count;
16 }
17 printf("a+aa+...=%ld\n",sn);
18 getch();
19 }



【程序19】题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程   找出1000以内的所有完数。1. 程序分析:请参照程序<--上页程序14.2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 static int k[10];
6 int i,j,n,s;
7 for(j=2;j<1000;j++)
8 {
9 n=-1;
10 s=j;
11 for(i=1;i<j;i++)
12 {
13 if((j%i)==0)
14 {
15 n++;
16 s=s-i;
17 k[n]=i;
18 }
19 }
20 if(s==0)
21 {
22 printf("%d is a wanshu",j);
23 for(i=0;i<n;i++)
24 printf("%d,",k);
25 printf("%d\n",k[n]);
26 }
27 }
28 getch();
29 }



【程序20】题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在   第10次落地时,共经过多少米?第10次反弹多高?1.程序分析:见下面注释2.程序源代码:



 1 #include "stdio.h"
2 #include "stdio.h"
3 main()
4 {
5 float sn=100.0,hn=sn/2;
6 int n;
7 for(n=2;n<=10;n++)
8 {
9 sn=sn+2*hn;/*第n次落地时共经过的米数*/
10 hn=hn/2; /*第n次反跳高度*/
11 }
12 printf("the total of road is %f\n",sn);
13 printf("the tenth is %f meter\n",hn);
14 getch();
15 }



【程序21】题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。1.程序分析:采取逆向思维的方法,从后往前推断。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int day,x1,x2;
6 day=9;
7 x2=1;
8 while(day>0)
9 {
10 x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
11 x2=x1;
12 day--;
13 }
14 printf("the total is %d\n",x1);
15 getch();
16 }



【程序22】题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出   三队赛手的名单。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,      则表明此数不是素数,反之是素数。       2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/
6 for(i='x';i<='z';i++)
7 for(j='x';j<='z';j++)
8 {
9 if(i!=j)
10 for(k='x';k<='z';k++)
11 {
12 if(i!=k&&j!=k)
13 {
14 if(i!='x'&&k!='x'&&k!='z')
15 printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);
16 }
17 }
18 }
19 getch();
20 }



【程序23】题目:打印出如下图案(菱形)   *  ********************  ***   *1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重      for循环,第一层控制行,第二层控制列。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int i,j,k;
6 for(i=0;i<=3;i++)
7 {
8 for(j=0;j<=2-i;j++)
9 printf(" ");
10 for(k=0;k<=2*i;k++)
11 printf("*");
12 printf("\n");
13 }
14 for(i=0;i<=2;i++)
15 {
16 for(j=0;j<=i;j++)
17 printf(" ");
18 for(k=0;k<=4-2*i;k++)
19 printf("*");
20 printf("\n");
21 }
22 getch();
23 }



【程序24】题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。1.程序分析:请抓住分子与分母的变化规律。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int n,t,number=20;
6 float a=2,b=1,s=0;
7 for(n=1;n<=number;n++)
8 {
9 s=s+a/b;
10 t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/
11 }
12 printf("sum is %9.6f\n",s);
13 getch();
14 }



【程序25】题目:求1+2!+3!+...+20!的和1.程序分析:此程序只是把累加变成了累乘。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 float n,s=0,t=1;
6 for(n=1;n<=20;n++)
7 {
8 t*=n;
9 s+=t;
10 }
11 printf("1+2!+3!...+20!=%e\n",s);
12 getch();
13 }



【程序26】题目:利用递归方法求5!。1.程序分析:递归公式:fn=fn_1*4!2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int i;
6 int fact();
7 for(i=0;i<5;i++)
8 printf("\40:%d!=%d\n",i,fact(i));
9 getch();
10 }
11 int fact(j)
12 int j;
13 {
14 int sum;
15 if(j==0)
16 sum=1;
17 else
18 sum=j*fact(j-1);
19 return sum;
20 }



【程序27】题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。1.程序分析:2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main()
4 {
5 int i=5;
6 void palin(int n);
7 printf("\40:");
8 palin(i);
9 printf("\n");
10 getch();
11 }
12 void palin(n)
13 int n;
14 {
15 char next;
16 if(n<=1)
17 {
18 next=getchar();
19 printf("\n\0:");
20 putchar(next);
21 }
22 else
23 {
24 next=getchar();
25 palin(n-1);
26 putchar(next);
27 }
28 }



【程序28】题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后   问第一个人,他说是10岁。请问第五个人多大?1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道      第四人的岁数,依次类推,推到第一人(10岁),再往回推。2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 age(n)
4 int n;
5 {
6 int c;
7 if(n==1) c=10;
8 else c=age(n-1)+2;
9 return(c);
10 }
11 main()
12 {
13 printf("%d",age(5));
14 getch();
15 }



【程序29】题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main( )
4 {
5 long a,b,c,d,e,x;
6 scanf("%ld",&x);
7 a=x/10000;/*分解出万位*/
8 b=x%10000/1000;/*分解出千位*/
9 c=x%1000/100;/*分解出百位*/
10 d=x%100/10;/*分解出十位*/
11 e=x%10;/*分解出个位*/
12 if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
13 else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
14 else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
15 else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
16 else if (e!=0) printf(" there are 1,%ld\n",e);
17 getch();
18 }



【程序30】题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   1.程序分析:同29例2.程序源代码:



 1 #include "stdio.h"
2 #include "conio.h"
3 main( )
4 {
5 long ge,shi,qian,wan,x;
6 scanf("%ld",&x);
7 wan=x/10000;
8 qian=x%10000/1000;
9 shi=x%100/10;
10 ge=x%10;
11 if(ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/
12 printf("this number is a huiwen\n");
13 else
14 printf("this number is not a huiwen\n");
15 getch();
16 }