C语言程序设计50例(经典收藏)(二)

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

代码如下:

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

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

代码如下:

#include"stdio.h"

#include"conio.h"

#include"math.h"

main()

{

 int m,i,k,h=0,leap=1;

 printf("\n");

 for(m=101;m<=200;m++)

 {

   k=sqrt(m+1);

   for(i=2;i<=k;i++)

   if(m%i==0)

   {

     leap=0;

     break;

   }

   if(leap)

   {

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

     h++;

     if(h%10==0)

       printf("\n");

   }

   leap=1;

 }

 printf("\nThe total is %d",h);

 getch();

}

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

代码如下:

#include"stdio.h"
#include "conio.h"
main()
{
  int i,j,k,n;
  printf("'water flower'number is:");
  for(n=100;n<1000;n++)
  {
    i=n/100;/*分解出百位*/
    j=n/10%10;/*分解出十位*/
    k=n%10;/*分解出个位*/
    if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
      printf("%-5d",n);
  }
  getch();
}

【程序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.程序源代码:

代码如下:

/*zheng int is divided yinshu*/
#include "stdio.h"
#include "conio.h"
main()
{
  int n,i;
  printf("\nplease input a number:\n");
  scanf("%d",&n);
  printf("%d=",n);
  for(i=2;i<=n;i++)
    while(n!=i)
    {
      if(n%i==0)
      {
        printf("%d*",i);
        n=n/i;
      }
      else
        break;
    }
  printf("%d",n);
  getch();
}

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

代码如下:

#include"stdio.h"
#include "conio.h"
main()
{
  int score;
  char grade;
  printf("please input a score\n");
  scanf("%d",&score);
  grade=score>=90?'A':(score>=60?'B':'C');
  printf("%d belongs to %c",score,grade);
  getch();
}

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

代码如下:

#include"stdio.h"
#include "conio.h"
main()
{
  int a,b,num1,num2,temp;
  printf("please input two numbers:\n");
  scanf("%d,%d",&num1,&num2);
  if(num1<num2)/*交换两个数,使大数放在num1上*/
  {
    temp=num1;
    num1=num2;
    num2=temp;
  }
  a=num1;b=num2;
  while(b!=0)/*利用辗除法,直到b为0为止*/
  {
    temp=a%b;
    a=b;
    b=temp;
  }
  printf("gongyueshu:%d\n",a);
  printf("gongbeishu:%d\n",num1*num2/a);
  getch();
}

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

代码如下:

#include"stdio.h"
#include "conio.h"
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++;
        elseif(c>='0'&&c<='9')
          digit++;
        else
          others++;
  }
  printf("all in all:char=%d space=%d digit=%dothers=%d\n",letters,
  space,digit,others);
  getch();
}

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

代码如下:

#include"stdio.h"
#include "conio.h"
main()
{
       int a,n,count=1;
     long int sn=0,tn=0;
       printf("please input a andn\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+...=%ld\n",sn);
     getch();
}

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

代码如下:

#include"stdio.h"
#include "conio.h"
main()
{
  static int k[10];
  int i,j,n,s;
  for(j=2;j<1000;j++)
  {
    n=-1;
    s=j;
    for(i=1;i<j;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<n;i++)
      printf("%d,",k);
      printf("%d\n",k[n]);
    }
  }
  getch();
}

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

代码如下:

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

猜你喜欢

转载自blog.csdn.net/youyoumo123/article/details/80982644