⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.txt

📁 经典c程序1--50
💻 TXT
📖 第 1 页 / 共 3 页
字号:
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int a,n,count=1;
  long int sn=0,tn=0;
  printf("please input a and n\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[i]);
      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();
}
【程序21】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析:采取逆向思维的方法,从后往前推断。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int day,x1,x2;
  day=9;
  x2=1;
  while(day>0)
  {
    x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
    x2=x1;
    day--;
  }
  printf("the total is %d\n",x1);
  getch();
}
==============================================================
【程序22】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
   三队赛手的名单。 
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
      则表明此数不是素数,反之是素数。       
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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);
         }
      }
    }
  getch();
}
==============================================================
【程序23】 
题目:打印出如下图案(菱形)

   *
  ***
 *****
*******
 *****
  ***
   *
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重
      for循环,第一层控制行,第二层控制列。 
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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");
  }
  getch();
}
==============================================================
【程序24】 
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。 
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int n,t,number=20;
  float a=2,b=1,s=0;
  for(n=1;n<=number;n++)
  {
    s=s+a/b;
    t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/
  }
  printf("sum is %9.6f\n",s);
  getch();
}
==============================================================
【程序25】 
题目:求1+2!+3!+...+20!的和
1.程序分析:此程序只是把累加变成了累乘。 
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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);
  getch();
}
==============================================================
【程序26】 
题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i;
  int fact();
  for(i=0;i<5;i++)
  printf("\40:%d!=%d\n",i,fact(i));
  getch();
}
int fact(j)
int j;
{
  int sum;
  if(j==0)
    sum=1;
  else
    sum=j*fact(j-1);
  return sum;
}
==============================================================
【程序27】 
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i=5;
  void palin(int n);
  printf("\40:");
  palin(i);
  printf("\n");
  getch();
}
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个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第
   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后 
   问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道
      第四人的岁数,依次类推,推到第一人(10岁),再往回推。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
age(n)
int n;
{
  int c;
  if(n==1) c=10;
  else c=age(n-1)+2;
  return(c);
}
main()
{
  printf("%d",age(5));
  getch();
}
==============================================================
【程序29】 
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供) 
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main( )
{
  long a,b,c,d,e,x;
  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);
  getch();
}
==============================================================
【程序30】 
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   
1.程序分析:同29例
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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)/*个位等于万位并且十位等于千位*/
    printf("this number is a huiwen\n");
  else
    printf("this number is not a huiwen\n");
  getch();
}
【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
   判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void main()
{
  char letter;
  printf("please input the first letter of someday\n");
  while((letter=getch())!='Y')/*当所按字母为Y时才结束*/
  {
    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");
    }
  }
  getch();
}
==============================================================
【程序32】
题目:Press any key to change color, do you want to try it. Please hurry up!
1.程序分析:            
2.程序源代码:
#include "conio.h"
#include "stdio.h"
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();/*输入字符看不见*/
  }
}
==============================================================
【程序33】
题目:学习gotoxy()与clrscr()函数   
1.程序分析:
2.程序源代码:
#include "conio.h"
#include "stdio.h"
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");
  getch();
}
==============================================================
【程序34】
题目:练习函数调用
1. 程序分析: 
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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();/*调用此函数*/
  getch();
}
==============================================================
【程序35】
题目:文本颜色设置
1.程序分析:
2.程序源代码:
#include "stdio.h"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -