【程序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:    153  370  371  407
Press any key to continue
*/

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

【程序4】
题目:将一个正整数分解质因数。例如:输入90,打印出90=233*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以内的所有完数。
程序源代码:

#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次反弹多高?
程序源代码:

#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
*/

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

更多推荐

专接本C语言必备程序