C语言程序设计经典50例,会用到的

古典问题(兔子生崽) 古典问题(兔子生崽) 题目:古典问题(兔子生崽):有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?(输出前40个月即可) 程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....,即下个月是上两个月之和(从第三个月开始)。 三种方法实现: 方法一代码 #include in 阅读详情
【程序1】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
      掉不满足条件的排列。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i,j,k;
  printf("\n");
  for(i=1;i<5;i++) /*以下为三重循环*/
    for(j=1;j<5;j++)
      for (k=1;k<5;k++)
      {
        if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/
        printf("%d,%d,%d\n",i,j,k);
      }
  getch();
}
==============================================================
【程序2】
题目:企业发放的奖金根据利润提成。利润(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"
#include "conio.h"
main()
{
  long int i;
  int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
  scanf("%ld",&i);
  bonus1=100000*0. 1;
  bonus2=bonus1+100000*0.75;
  bonus4=bonus2+200000*0.5;
  bonus6=bonus4+200000*0.3;
  bonus10=bonus6+400000*0.15;
  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",bonus);
  getch();
}
==============================================================
【程序3】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后
      的结果满足如下条件,即是结果。请看具体分析:
2.程序源代码:
#include "math.h"
#include "stdio.h"
#include "conio.h"
main()
{
  long int i,x,y,z;
  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("\n%ld\n",i);
  }
  getch();
}
==============================================================
【程序4】
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int day,month,year,sum,leap;
  printf("\nplease input year,month,day\n");
  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%400==0||(year%4==0&&year%100!=0)) /*判断是不是闰年*/
    leap=1;
  else
    leap=0;
  if(leap==1&&month>2) /*如果是闰年且月份大于2,总天数应该加一天*/
    sum++;
  printf("It is the %dth day.",sum);
  getch();
}
==============================================================
【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
      然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int x,y,z,t;
  scanf("%d%d%d",&x,&y,&z);
  if (x>y)
    {t=x;x=y;y=t;} /*交换x,y的值*/
  if(x>z)
    {t=z;z=x;x=t;} /*交换x,z的值*/
  if(y>z)
    {t=y;y=z;z=t;} /*交换z,y的值*/
  printf("small to big: %d %d %d\n",x,y,z);
  getch();
}
==============================================================
【程序6】
题目:用*号输出字母C的图案。
1.程序分析:可先用'*'号在纸上写出字母C,再分行输出。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  printf("Hello C-world!\n");
  printf(" ****\n");
  printf(" *\n");
  printf(" * \n");
  printf(" ****\n");
  getch();
}
==============================================================
【程序7】
题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
1.程序分析:字符共有256个。不同字符,图形不一样。      
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  char a=176,b=219;
  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);
  getch();
}
==============================================================
【程序8】
题目:输出9*9口诀。
1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i,j,result;
  printf("\n");
  for (i=1;i<10;i++)
  {
    for(j=1;j<10;j++)
    {
      result=i*j;
      printf("%d*%d=%-3d",i,j,result); /*-3d表示左对齐,占3位*/
    }
    printf("\n"); /*每一行后换行*/
  }
  getch();
}
==============================================================
【程序9】
题目:要求输出国际象棋棋盘。
1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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");
  }
  getch();
}
==============================================================
【程序10】
题目:打印楼梯,同时在楼梯上方打印两个笑脸。
1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
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");
  }
  getch();
}


  .:.:经典c程序100例==11--20 :.:.

    经典c程序100例==11--20
【程序11】
题目:古典问题:有一对兔子,从出生后第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++;
        else if(c>='0'&&c<='9')
          digit++;
        else
          others++;
  }
  printf("all in all:char=%d space=%d digit=%d others=%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 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);
      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();
}


  .:.:经典c程序100例==21--30:.:.

    经典c程序100例==21--30
【程序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();
}


  .:.:经典c程序100例==31--40:.:.

    经典c程序100例==31--40
【程序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"
#include "conio.h"
void main(void)
{
  int color;
  for (color = 1; color < 16; color++)
  {
    textcolor(color);/*设置文本颜色*/
    cprintf("This is color %d\r\n", color);
  }
  textcolor(128 + 15);
  cprintf("This is blinking\r\n");
  getch();
}
==============================================================
【程序36】
题目:求100之内的素数   
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "math.h"
#define N 101
main()
{
  int i,j,line,a[N];
  for(i=2;i<N;i++) a=i;
    for(i=2;i<sqrt(N);i++)
      for(j=i+1;j<N;j++)
      {
        if(a!=0&&a[j]!=0)
          if(a[j]%a==0)
            a[j]=0;
      }
  printf("\n");
  for(i=2,line=0;i<N;i++)
  {
    if(a!=0)
    {
      printf("%5d",a);
      line++;
    }
    if(line==10)
    {
      printf("\n");
      line=0;
    }
  }
  getch();
}
==============================================================
【程序37】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,
      下次类推,即用第二个元素与后8个进行比较,并进行交换。       
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define N 10
main()
{
  int i,j,min,tem,a[N];
  /*input data*/
  printf("please input ten num:\n");
  for(i=0;i<N;i++)
  {
    printf("a[%d]=",i);
    scanf("%d",&a);
  }
  printf("\n");
  for(i=0;i<N;i++)
    printf("%5d",a);
  printf("\n");
  /*sort ten num*/
  for(i=0;i<N-1;i++)
  {
    min=i;
    for(j=i+1;j<N;j++)
      if(a[min]>a[j])
        min=j;
    tem=a;
    a=a[min];
    a[min]=tem;
  }
  /*output data*/
  printf("After sorted \n");
  for(i=0;i<N;i++)
  printf("%5d",a);
  getch();
}
==============================================================
【程序38】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a累加后输出。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
/* 如果使用的是TC系列编译器则可能需要添加下句 */
static void dummyfloat(float *x){ float y; dummyfloat(&y);}
main()
{
  float a[3][3],sum=0;
  int i,j;
  printf("please input rectangle element:\n");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("%f",&a[j]);
  for(i=0;i<3;i++)
    sum=sum+a;
  printf("duijiaoxian he is %6.2f",sum);
  getch();
}
==============================================================
【程序39】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后
     此元素之后的数,依次后移一个位置。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int a[11]={1,4,6,9,13,16,19,28,40,100};
  int temp1,temp2,number,end,i,j;
  printf("original array is:\n");
  for(i=0;i<10;i++)
    printf("%5d",a);
  printf("\n");
  printf("insert a new number:");
  scanf("%d",&number);
  end=a[9];
  if(number>end)
    a[10]=number;
  else
  {
    for(i=0;i<10;i++)
    {
      if(a>number)
      {
        temp1=a;
        a=number;
        for(j=i+1;j<11;j++)
        {
          temp2=a[j];
          a[j]=temp1;
          temp1=temp2;
        }
        break;
      }
    }
  }
  for(i=0;i<11;i++)
    printf("%6d",a);
  getch();
}
==============================================================
【程序40】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define N 5
main()
{
  int a[N]={9,6,5,4,1},i,temp;
  printf("\n original array:\n");
  for(i=0;i<N;i++)
    printf("%4d",a);
  for(i=0;i<N/2;i++)
  {
    temp=a;
    a=a[N-i-1];
    a[N-i-1]=temp;
  }
  printf("\n sorted array:\n");
  for(i=0;i<N;i++)
    printf("%4d",a);
  getch();
}


  .:.:经典c程序100例==41--50:.:.

    经典c程序100例==41--50
【程序41】
题目:学习static定义静态变量的用法   
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
varfunc()
{
  int var=0;
  static int static_var=0;
  printf("\40:var equal %d \n",var);
  printf("\40:static var equal %d \n",static_var);
  printf("\n");
  var++;
  static_var++;
}
void main()
{
  int i;
  for(i=0;i<3;i++)
    varfunc();
  getch();
}
==============================================================
【程序42】
题目:学习使用auto定义变量的用法
1.程序分析:      
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i,num;
  num=2;
  for(i=0;i<3;i++)
  {
    printf("\40: The num equal %d \n",num);
    num++;
    {
      auto int num=1;
      printf("\40: The internal block num equal %d \n",num);
      num++;
    }
  }
  getch();
}
==============================================================
【程序43】
题目:学习使用static的另一用法。   
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i,num;
  num=2;
  for(i=0;i<3;i++)
  {
    printf("\40: The num equal %d \n",num);
    num++;
    {
      static int num=1;
      printf("\40:The internal block num equal %d\n",num);
      num++;
    }
  }
  getch();
}
==============================================================
【程序44】
题目:学习使用external的用法。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
int a,b,c;
void add()
{
  int a;
  a=3;
  c=a+b;
}
void main()
{
  a=b=4;
  add();
  printf("The value of c is equal to %d\n",c);
  getch();
}
==============================================================
【程序45】
题目:学习使用register定义变量的方法。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void main()
{
  register int i;
  int tmp=0;
  for(i=1;i<=100;i++)
  tmp+=i;
  printf("The sum is %d\n",tmp);
  getch();
}
==============================================================
【程序46】
题目:宏#define命令练习(1)   
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
void main()
{
  int num;
  int again=1;
  printf("\40: Program will stop if input value less than 50.\n");
  while(again)
  {
    printf("\40:Please input number==>");
    scanf("%d",&num);
    printf("\40:The square for this number is %d \n",SQ(num));
    if(num>=50)
      again=TRUE;
    else
      again=FALSE;
  }
  getch();
}
==============================================================
【程序47】
题目:宏#define命令练习(2)
1.程序分析:            
2.程序源代码:
#include "stdio.h"
#include "conio.h"
/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/
#define exchange(a,b) { \
                        int t;\
                        t=a;\
                        a=b;\
                        b=t;\
                      }
void main(void)
{
  int x=10;
  int y=20;
  printf("x=%d; y=%d\n",x,y);
  exchange(x,y);
  printf("x=%d; y=%d\n",x,y);
  getch();
}
==============================================================
【程序48】
题目:宏#define命令练习(3)   
1.程序分析:
2.程序源代码:
#define LAG >
#define SMA <
#define EQ ==
#include "stdio.h"
#include "conio.h"
void main()
{
  int i=10;
  int j=20;
  if(i LAG j)
    printf("\40: %d larger than %d \n",i,j);
    else if(i EQ j)
      printf("\40: %d equal to %d \n",i,j);
      else if(i SMA j)
        printf("\40:%d smaller than %d \n",i,j);
      else
        printf("\40: No such value.\n");
  getch();
}
==============================================================
【程序49】
题目:#if #ifdef和#ifndef的综合应用。
1. 程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define MAX
#define MAXIMUM(x,y) (x>y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
void main()
{
  int a=10,b=20;
#ifdef MAX
  printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
  printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
  printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
  printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
  printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
  printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
  printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
  printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
  getch();
}

c语言基础程序——100实例 /根据公示Π/4=1-1/3+1/5-1/7+1/9-1/11+......,计算Π的近似值,当最后一项的绝对值小于0.000001为止。int f=1;//变号用//i是分母,sum是Π/4的值while (fabs(1.0/i)>=1e-6)//当绝对值小于10的-5次方认为为0//累加每项的值f*=-1;//变号i+=2;//分母+2变化printf("Π的近似值为%.5lf",sum*4); 阅读详情

相关推荐

C语言程序设计100个经典例子

<1>数字排列。 #include <stdio.h> int main() { int sum= 0; for(int i= 1; i< 5; i++) { for(int a= 1; a< 5; a++) { for(int b= 1; b< 5; b++) { if(i!=a && i!=b && a!=b) { printf("%d%d%d", i, a, ...

llt20020710的博客 3万+

C语言项目源代码大全2023最新!

一、C语言源码大全 C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。 博主已经把大量C语言源码整理为一个压缩包关注 微 信 公 众 号:C和C加加 回复:“源码”即可获取 二、C语言

chengxuyuanlaow的博客 4万+

C语言程序设计经典50例

【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 解答: #include int main() { int i,j,k,n=0; for(i=1;i//只要这样的三重循环就可以直接保证不重复,而不必再使用条件来判断 { for(j=1;j { for(k=1;k { if(i!=j&&i!=k&&j!=k) { prin

初学者 1万+

C语言必背经典程序代码

题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

qq_52049228的博客 3万+

【C语言】常见的五个C语言基础代码示例

下面是一些有趣的C语言代码示例,展示了C语言的一些基本特性和技巧,可以帮助您更好地理解和掌握C语言的基础知识。

若北辰 5555

C语言经典100例

【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。程序源代码: int _tmain(int argc, _TCHAR* argv[]){ int i, j, k,n=0; for (i = 1; i < 5;

编程与实战的博客 4834

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

【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去       掉不满足条件的排列。 #include “stdio.h” #include “conio.h” main() { int i,j,k; printf(“\n”); for(i=1;i<5;i++

sinat_35787546的博客 17万+

从Transformer到Mamba:显著性检测领域的计算效率革命(Samba框架深度解析)

本文深入解析了Samba框架在显著性目标检测(Salient Object Detection)领域的创新应用,通过结合Mamba架构的优势,实现了计算效率的革命性提升。Samba框架采用显著性引导的Mamba模块(SGMB)和上下文感知上采样(CAU),有效解决了传统CNN和Transformer的局限性,同时在RGB-D等多模态任务中展现出卓越性能。

mmm90的博客 557

经典C语言程序设计100例

经典C语言程序设计100例 【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 当然这里的去掉并不是减去而是条件判断,比如符合这个条件(i!=k&&i!=j&&j!=k)的才打印出来 程序源代码: main() { int i,j,k; printf("\n"); for(i=1;i&...

Dr_abandon的博客 9601

PLS-PM with R 偏最小二乘路径建模 (R语言)

1. 引入 1.1. 安装plspm包 plspm 是一个用于执行偏最小二乘路径建模分析的 r 程序包。 在 CRAN 上可以免费下载: Http://cran.r-project.org/web/packages/plspm/index.html. 此后,通过以下代码在r中安装并加载该程序包即可。 install.packages("plspm") library("plspm") 2. 一些简单概念 2.1. 潜变量和观测变量 不少变量如满意度、成功指数是无法通过观测得到

Wowlittlemoon的博客 1万+

使用 CFD 仿真进行阀门性能分析:第一部分

了解如何使用 Ansys Discovery 评估阀门性能,使工程师能够设计出更好的产品。

jkh920184196的博客 3147

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

C语言程序设计50例(经典收藏)本篇文章是对C语言程序设计的50个小案例进行了详细的分析介绍 需要的朋友参考下【程序1】题目 有1、2、3、4个数字 能组成多少个互不相同且无重复数字的三位数 都是多少1.程序分析 可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。2.程序源代码代码如下:#include stdio.hint main()int i,j,k;p...

weixin_42476888的博客 1万+

【红外DDE算法】一种应用于高动态范围红外可视化的算法

【红外DDE算法】一种应用于高动态范围红外可视化的算法 - 这个算法背后的动机 - 展示这个算法的流程 - 然后讨论各个可调参数的意义。

sunwindl91181的博客 1683

c语言万能编程模板_C语言程序设计50例(经典收藏)

本篇文章是对C语言程序设计的50个小案例进行了详细的分析介绍,需要的朋友参考下【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。2.程序源代码:#include "stdio.h"#include "conio.h"main(){int i,j,k;pri...

weixin_39785422的博客 8820

uniapp - 实现 App 端 “半屏幕“ 连续扫码功能,页面只有半个屏幕是扫码框并且可持续连续扫码获取数据(不使用官方 uni.scanCode API,因为无法连续扫码!)详细示例源码插件教程

uniapp app端实现半屏幕扫码,uniapp 扫一扫半个屏幕,uniapp 扫一扫,uniapp持续扫码实现,uniapp怎么自定义扫码和连续扫码,uni-app App端半屏连续扫码,uniapp 扫一扫半个屏幕,uniapp自定义扫码页面样式,实现连续扫码功能(uniapp),uniapp - 实现 App 端 "半屏幕" 连续扫码功能,页面只有半个屏幕是扫码框并且可持续连续扫码获取数据(不使用官方 uni.scanCode API,因为无法连续扫码!)详细示例源码插件教程。

✅ 精选推荐文章 1万+

Altium Designer(AD24/25)PCB编辑的几种方法介绍

本文介绍了Altium Designer(AD24/25)软件中PCB编辑的四种操作方法:1)通过菜单栏"Place→Component"激活;2)点击工具栏元器件图标;3)右键快捷菜单选择;4)快捷键P+C组合操作。建议初学者先通过菜单栏和快捷菜单熟悉操作,熟练后转为工具栏和快捷键以提高效率。文章配有操作示意图,帮助用户快速掌握PCB元器件放置技巧。

fydar的博客 946

经典C语言程序设计100例,部分有个人注解

C语言经典程序设计,前面40道题都是比较基础的,后面的有点难~~~如果哪一个题目需要我详细注释的,可以留言我或加评论

wenzhi的博客 3万+

RK3568平台(camera篇)点亮imx586摄像头

mipi摄像头硬件确认点:1.供电:5V,2.8V,1.2V,1.8V,reset脚(硬拉3.3,上电的时候从低到高),pwron脚外接3.3V。2,时钟:MCLKOUT是24兆时钟。3.4组差分mipi线。mipi的data线的波形为方波。

weixin_49303682的博客 7258

【C语言基础案例目录】经典C语言程序设计100例目录(附100例题目)

《C语言基础案例目录》精选100个经典程序设计案例,分为10个部分(每部分10例),涵盖数字组合、奖金计算、完全平方数等基础算法。案例由浅入深,包含素数判断、水仙花数、斐波那契数列等数学问题,以及递归、绘图、位运算等进阶内容。每个案例提供题目描述和源码解析,适合C语言学习者系统练习编程思维和实战能力。从基础控制结构到复杂算法实现,全面培养程序设计能力。

yuhuasheng1997的博客 1513

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【C语言基础案例】经典C语言程序设计100例附源码解析(1-10例)

本文介绍了5个经典C语言程序示例:(1)用1-4数字组成不重复三位数的排列组合问题;(2)根据企业利润计算奖金的分段函数实现;(3)求解满足特定完全平方数条件的整数;(4)计算指定日期是该年第几天的日期处理程序;(5)三个数从小到大排序的简单算法。每个程序包含题目分析、核心知识点和完整源代码,涵盖了循环结构、条件判断、数学运算、日期计算等基础编程概念,适合C语言初学者学习实践。

yuhuasheng1997的博客 1056

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

C语言程序设计50例(经典分享)

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,60万到100万之间时,高于60万元的部分,可提成1.5%,高于。(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提。

小李学习笔记的博客 707

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

上一篇: 76个中国文化常识
下一篇: 学习PHP中自己遇到的不熟悉的或掌握不够牢固的知识点(待续1)
yanfangphp
博客等级 码龄15年 126粉丝 126原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值