C语言100个经典的算法

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); 阅读详情
AI摘要

本文精选了100个C语言经典算法题目并提供详细解答,覆盖了从基础到进阶的内容,如数列计算、字符串处理、素数判断等,适合初学者和进阶者实践练习。

POJ上做做ACM的题

语言的学习基础,100个经典的算法
C语言的学习要从基础开始,这里是100个经典的算法-1C语言的学习要从基础开始,这里是100个经典的算法

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔

子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数

为多少?


__________________________________________________________________


程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....


___________________________________________________________________

程序源代码:
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;/*前两个月加起来赋值给第三个月*/
 }
}

上题还可用一维数组处理,you try!


题目:判断101-200之间有多少个素数,并输出所有素数。


__________________________________________________________________


程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整

除,则表明此数不是素数,反之是素数。


___________________________________________________________________


程序源代码:
#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);
}


题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位

数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方

+5的三次方+3的三次方。


__________________________________________________________________


程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。


___________________________________________________________________


程序源代码:
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);
   }
 }
printf("\n");
}

 

题目:将一个正整数分解质因数。例如:输入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的值,重复执行第一步。


___________________________________________________________________

 

程序源代码:
/* zheng int is divided yinshu*/
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);
}


题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60

-89分之间的用B表示,60分以下的用C表示。


__________________________________________________________________


程序分析:(a>b)?a:b这是条件运算符的基本例子。


___________________________________________________________________


程序源代码:
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);
}

 

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。


__________________________________________________________________


程序分析:利用辗除法。


___________________________________________________________________


程序源代码:
main()
{
 int a,b,num1,num2,temp;
 printf("please input two numbers:\n");
 scanf("%d,%d",&num1,&num2);
 if(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);
}


题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

。


__________________________________________________________________


程序分析:利用while语句,条件为输入的字符不为'\n'.


___________________________________________________________________


程序源代码:
#include "stdio.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);
}


题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如

2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。


__________________________________________________________________


程序分析:关键是计算出每一项的值。


___________________________________________________________________


程序源代码:
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);
}


题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2

+3.编程找出1000以内的所有完数。


___________________________________________________________________


程序源代码:
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);
 printf("%d\n",k[n]);
 }
}
}

 

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,

求它在第10次落地时,共经过多少米?第10次反弹多高?


___________________________________________________________________


程序源代码:
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);
}


题目:一只猴子摘了N个桃子第一天吃了一半又多吃了一个,第二天又吃了余下的

一半又多吃了一个,到第十天的时候发现还有一个.


___________________________________________________________________


程序源代码:
/* 猴子吃桃问题 */
main()
{
int i,s,n=1;
for(i=1;i<10;i++)
{
s=(n+1)*2
n=s;
}
printf("第一天共摘了%d个桃\n",s);
}

 

迭代法求方程根

___________________________________________________________________

 

/* 迭代法求一个数的平方根 */
#define Epsilon 1.0E-6 /*控制解的精度*/
#include<math.h>
main()
{
float a,x0,x1;
printf("请输入要求的数:");
scanf("%f",&a);
x0=a/2;
x1=(x0+a/x0)/2;
while(fabs(x1-x0)>=Epsilon)
  {
  x0=x1;
  x1=(x0+a/x0)/2;
  }
printf("%f的平方根:%f.5\n",x1);
}

 

/* 上题的另一种算法 */
#define Epsilon 1.0E-6 /*控制解的精度*/
#include <stdio.h>
#include <math.h>
main()
{
float num,pre,this;
do
  {
  scanf("%f",&num);/*输入要求平方根的数*/
  }while(num<0);
if (num==0)
  printf("the root is 0");
else
  {
   this=1;
   do
   {
    pre=this;
    this=(pre+num/pre)/2;
    }while(fabs(pre-this)>Epsilon);/*用解的精度,控制循环次数*/
   }
printf("the root is %f",this);
}


用牛顿迭代法 求方程 2*x*x*x-4*x*x+3*x-6 的根
/* 牛顿迭代法 */
#define Epsilon 1.0E-6 /*控制解的精度*/
#include<math.h>
main()
{
   float x1,x0=1.5;
   x1=x0-(2*x0*x0*x0-4*x0*x0+3*x0-6)/(6*x0*x0-8*x0+3);
   while(fabs(x1-x0>=Epsilon)
   {  
      x0=x1;
      x1=x0-(2*x0*x0*x0-4*x0*x0+3*x0-6)/(6*x0*x0-8*x0+3);
   }
   printf("方程的根为%f\n",x1);
}


用二分法求上题
/* 二分法 */
#define Epsilon 1.0E-5 /*控制解的精度*/
#include<math.h>

main()
{
   folat x1,x2,x0,f1,f2,f0;
   x0=(x1+x2)/2;
   f0=2*x0*x0*x0-4*x0*x0+3*x0-6;   /* 求中点的函数值 */
   while(fabs(f0)>=Epsilon)
   {
     if(f0*f1<0)
     { x2=x0;
f2=2*x2*x2*x2-4*x2*x2+3*x2-6;
     }
     if(f0*f2<0)
     { x1=x0;
f1=2*x1*x1*x1-4*x1*x1+3*x1-6;
     }
     x0=(x1+x2)/2;
     f0=2*x0*x0*x0-4*x0*x0+3*x0-6;
   }
   printf("用二分法求得方程的根:%f\n",x0);
}


题目:打印出如下图案(菱形)

*
***
******
********
******
***
*

___________________________________________________________________


程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利

用双重for循环,第一层控制行,第二层控制列。

___________________________________________________________________


程序源代码:
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");
 }
}


题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,

十位与千位相同。

___________________________________________________________________


程序分析:同29例

___________________________________________________________________


程序源代码:
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");
}

 

题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,

则继续判断第二个字母。

___________________________________________________________________


程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语

句判断第二个字母。

___________________________________________________________________


程序源代码:
#include <stdio.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");
  }
 }
}


题目:Press any key to change color, do you want to try it. Please

hurry up!

___________________________________________________________________


程序源代码:
#include <conio.h>
void main(void)
{
int color;
for (color = 0; color < 8; color++)
 {
 textbackground(color); /*设置文本的背景颜色*/
 cprintf("This is color %d\r\n", color);
 cprintf("ress any key to continue\r\n");
 getch(); /*输入字符看不见*/
 }
}


题目:学习gotoxy()与clrscr()函数

___________________________________________________________________


程序源代码:
#include <conio.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");
}

 

题目:练习函数调用

___________________________________________________________________


程序源代码:
#include <stdio.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();/*调用此函数*/
}

 

题目:文本颜色设置

___________________________________________________________________


程序源代码:
#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");
}


题目:求100之内的素数

___________________________________________________________________


程序源代码:
#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;}
}
}


题目:对10个数进行排序

___________________________________________________________________


程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个

元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。    

   
程序源代码:
#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);
}


题目:求一个3*3矩阵对角线元素之和

___________________________________________________________________


程序分析:利用双重for循环控制输入二维数组,再将a累加后输出。

___________________________________________________________________


程序源代码:
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);
}


题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数

组中。

___________________________________________________________________


程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况

,插入后此元素之后的数,依次后移一个位置。

___________________________________________________________________


程序源代码:
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);
}


题目:将一个数组逆序输出。

___________________________________________________________________


程序分析:用第一个与最后一个交换。

___________________________________________________________________


程序源代码:
#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);
}

 

题目:学习static定义静态变量的用法

___________________________________________________________________


程序源代码:
#include "stdio.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();
}

 

题目:学习使用auto定义变量的用法

___________________________________________________________________


程序源代码:
#include "stdio.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++;
  }
 }
}


C语言的学基础,100个经典的算法-2
程序源代码:
#include "stdio.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++;
}
}
}

 

题目:学习使用external的用法。

___________________________________________________________________


程序源代码:
#include "stdio.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);
}

 

题目:学习使用register定义变量的方法。

___________________________________________________________________


程序源代码:
void main()
{
register int i;
int tmp=0;
for(i=1;i<=100;i++)
tmp+=i;
printf("The sum is %d\n",tmp);
}

 

题目:宏#define命令练习(1)

___________________________________________________________________


程序源代码:
#include "stdio.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("\40lease 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;
}
}

 

题目:宏#define命令练习(2)

___________________________________________________________________


程序源代码:
#include "stdio.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);
}

 

题目:宏#define命令练习(3)

___________________________________________________________________


程序源代码:
#define LAG >
#define SMA <
#define EQ ==
#include "stdio.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");
}

 

题目:#if #ifdef和#ifndef的综合应用。

___________________________________________________________________


程序源代码:
#include "stdio.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
}

 

题目:#include 的应用练习

___________________________________________________________________


程序源代码:
test.h 文件如下:
#define LAG >
#define SMA <
#define EQ ==
#include "test.h" /*一个新文件50.c,包含test.h*/
#include "stdio.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");
}


题目:学习使用按位与 & 。   

___________________________________________________________________


程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1

___________________________________________________________________


程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a&3;
printf("\40: The a & b(decimal) is %d \n",b);
b&=7;
printf("\40: The a & b(decimal) is %d \n",b);
}

 

题目:学习使用按位或 | 。

___________________________________________________________________


程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1            

___________________________________________________________________


程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a|3;
printf("\40: The a & b(decimal) is %d \n",b);
b|=7;
printf("\40: The a & b(decimal) is %d \n",b);
}

 

题目:学习使用按位异或 ^ 。   

___________________________________________________________________


程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0

___________________________________________________________________


程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a^3;
printf("\40: The a & b(decimal) is %d \n",b);
b^=7;
printf("\40: The a & b(decimal) is %d \n",b);
}

 

题目:取一个整数a从右端开始的4~7位。

___________________________________________________________________


  程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。

___________________________________________________________________


程序源代码:
main()
{
unsigned a,b,c,d;
scanf("%o",&a);
b=a>>4;
c=~(~0<<4);
d=b&c;
printf("%o\n%o\n",a,d);
}

 

题目:学习使用按位取反~。   

___________________________________________________________________


程序分析:~0=1; ~1=0;

___________________________________________________________________


程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=234;
b=~a;
printf("\40: The a's 1 complement(decimal) is %d \n",b);
a=~a;
printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
}

 

题目:画图,学用circle画圆形。

___________________________________________________________________


程序源代码:
/*circle*/
#include "graphics.h"
main()
{
int driver,mode,i;
float j=1,k=1;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
for(i=0;i<=25;i++)
  {
   setcolor(8);
   circle(310,250,k);
   k=k+j;
   j=j+0.3;
  }
}

 

题目:画图,学用line画直线。

___________________________________________________________________

     
程序源代码:
#include "graphics.h"
main()
{
int driver,mode,i;
float x0,y0,y1,x1;
float j=12,k;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(GREEN);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
  {
   setcolor(5);
   line(x0,y0,x0,y1);
   x0=x0-5;
   y0=y0-5;
   x1=x1+5;
   y1=y1+5;
   j=j+10;
  }
x0=263;y1=275;y0=263;
for(i=0;i<=20;i++)
  {
   setcolor(5);
   line(x0,y0,x0,y1);
   x0=x0+5;
   y0=y0+5;
   y1=y1-5;
  }
}

 

题目:画图,学用rectangle画方形。   

___________________________________________________________________

 
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

___________________________________________________________________

 
程序源代码:
#include "graphics.h"
main()
{
int x0,y0,y1,x1,driver,mode,i;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
  {
   setcolor(1);
   rectangle(x0,y0,x1,y1);
   x0=x0-5;
   y0=y0-5;
   x1=x1+5;
   y1=y1+5;
  }
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(150,40,"How beautiful it is!");
line(130,60,480,60);
setcolor(2);
circle(269,269,137);
}

 

02-C语言经典算法100例 阅读详情

相关推荐

C语言编程:掌握100个经典算法

C语言是一种广泛使用的计算机编程语言,它以其高效和灵活性著称,非常适合系统编程和嵌入式开发。为了开始C语言的编程之旅,首先需要配置一个合适的开发环境。对于Windows用户,可以选择安装MinGW或Cygwin,而Linux和macOS系统通常预装了GCC编译器。安装完成后,可以通过编写一个简单的“Hello World”程序来验证环境是否搭建成功。C语言的核心语法包括数据类型、变量、运算符、控制结构等。掌握这些基础语法是理解后续复杂算法的基石。

weixin_33816734的博客 977

C语言经典算法100道实战题

C语言经典算法100道实战题

C/C++编程指南 2万+

C语言经典例题100例(含答案)

100道经典例题包含答案,做完收获很大,适合掌握了C语言基本语法以后想做练习的同学学习

C语言经典算法

一、冒泡排序 随机输入十个数,按从大到小排序 int main() -----------冒泡排序 { int a[10]; int i,j,t; printf("input 10 nimber:\n"); for(i=0;i<10;i++) { scanf("%d",&a[i]); } printf("\n"); for(j=0;j<9;j++) for(i=0;i<9-j;i++) { if(a[i]>a[i+1])

飞翔的企鹅的博客 2万+

避坑指南:GROMACS模拟中.tpr、.xtc、.edr、.cpt文件你用对了吗?从生成到分析全流程解析

本文详细解析GROMACS分子动力学模拟中.tpr、.xtc、.edr、.cpt等核心文件的使用技巧,从文件生成到轨迹优化、能量分析的全流程避坑指南。重点介绍拓扑文件校验、轨迹格式选择、周期性边界处理等实战经验,帮助科研人员高效管理模拟数据并准确提取关键能量信息。

weixin_30631587的博客 124

在PLC中将十六进制数据转换为浮点数(通常遵循IEEE 754标准)需要理解数据存储格式,并按以下步骤操作:

通过以上步骤,您可以在PLC中实现十六进制到浮点数的转换。T DBD20 // 将数据传送到浮点数变量(如DB1.DBD20)指数位(8位):实际指数 = 存储值 - 127(偏移量)。:必须确认PLC的存储顺序(大端或小端),否则结果错误。将两个16位寄存器合并为32位数据,需注意。尾数位(23位):隐含前导1,实际值为。:确保32位数据在寄存器中连续且对齐。符号位(1位):0表示正,1表示负。:高位寄存器在前(如西门子PLC)。:低位寄存器在前(如三菱FX系列)。,误差因截断导致)。

K11mvp的博客 4428

C语言经典的100个算法

程序分析:兔子的规律为数列1,1,2,3,5,8,13,21…上题还可用一维数组处理,you try!

Naiva的博客 1474

Cocos Creator发布微信小游戏:深度定制与去除默认加载页面的实践指南

本文详细介绍了如何使用Cocos Creator发布微信小游戏时去除默认加载页面的实践方法。通过修改构建配置和重写启动逻辑,开发者可以自定义加载体验,提升品牌一致性。文章还提供了进阶优化方案和常见问题解决方案,帮助开发者实现更专业的微信小游戏发布流程。

weixin_27972919的博客 575

C语言100个算法经典例题

C语言100个算法经典例题 去发现同类优质开源项目:https://gitcode.com/ 欢迎来到这个资源下载仓库!这里我们提供了《C语言100个算法经典例题》的资源文件,完全免费共享给需要的你。 本资源包含100个精选的C语言算法例题,覆盖了C语言编程的基础到进阶内容,旨在帮助编程爱好者通过实战练习来提升编程技巧和理解算法原理。 不论你是C语言的初学者,还是有一定基础的编程爱好者,这些例题都...

gitblog_06773的博客 457

Android 白名单保活实战:主流厂商适配指南

本文详细解析了Android应用白名单保活的实战策略,重点针对华为、小米、OPPO、vivo等主流厂商的系统特性,提供了精准的后台设置页面跳转适配方案。文章从基础的前台服务实现讲起,深入讲解了白名单的判断、申请与厂商适配的核心代码与避坑指南,旨在帮助开发者合规、稳定地提升应用后台存活率。

motor的博客 1870

线性模型:AR、MA、ARMA、ARMAX、ARX、ARARMAX、OE、BJ等

线性模型:AR、MA、ARMA、ARMAX、ARX、ARARMAX、OE、BJ等

zephyr_wang的博客 1万+

c语言经典算法100例百度云,C语言经典算法100例(强烈推荐).

当前位置:编程学习 > C#/ASP.NET >>C语言经典算法100例(强烈推荐).C语言经典算法100例.RAR--------------------编程问答--------------------真的很经典--------------------编程问答--------------------呵呵.--------------------编程问答-------------...

weixin_42403771的博客 531

华为Atlas 300I推理卡驱动安装避坑指南:不同安装方式的选择与优化

本文详细解析华为Atlas 300I推理卡驱动安装的三种主流方式(.run、.rpm和源码编译),提供关键环境检查步骤、安装流程优化建议及常见问题解决方案,帮助开发者高效完成芯片推理卡部署并规避典型安装陷阱。

q9w8e7r6t5的博客 439

C语言的100个经典算法

C语言的学习要从基础开始,这里是100个经典的算法-1C语言的学习要从基础开始,这里是100个经典的算法 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数 为多少? ______________________________________________

qq_24928349的专栏 587

yolov5改进之Fasternet主干网络替换

fasternet替换yolov5主干网络

qq_41920323的博客 5153

2026华为OD机试(C++)真题【E卷+A卷+B卷+C卷+D卷】目录+在线OJ

华为OD机试真题题库【E卷+A卷+B卷+C卷+D卷】目录,C++实现。

MISAYAONE的博客 20万+

C语言算法题作用,C语言经典算法100例题目

《C语言经典算法100例题目》由会员分享,可在线阅读,更多相关《C语言经典算法100例题目(11页珍藏版)》请在人人文库网上搜索。1、看懂一个程序,分三步:1、流程;2、每个语句的功能;3、试数;小程序:1、尝试编程去解决他;2、看答案;3、修改程序,不同的输出结果;4、照答案去敲;5、调试错误;6、不看答案,自己把答案敲出来;7、实在不会就背会。周而复始,反复的敲。【程序1】 题目:有1、2、3...

weixin_42339883的博客 224

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

C语言的100个经典算法资源集锦

C语言的100个经典算法资源集锦 【下载地址】C语言的100个经典算法资源集锦 这是一份精心整理的C语言经典算法资源集锦,涵盖100个实用算法,是C语言编程爱好者和面试者的必备宝典。内容从基础排序、查找算法到高级的动态规划、图算法,全面覆盖了字符串处理、数组矩阵、栈队列、递归等核心领域。每个算法均配有详细注释和实现代码,...

gitblog_06760的博客 482

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

深度学习工具包 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")

上一篇: VS2005创建CLR自定义触发器
下一篇: 在PHP语言中使用JSON
深度Java
深度Java 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄22年 1万+粉丝 1675原创
评论 13
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值