教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高中教育 >

C语言经典算法100例(2)

来源:网络收集 时间:2026-10-04
导读: C语言经典算法100例 #include stdio.h #include math.h main() { float num,pre,this; do { scanf(%f,num);/*输入要求平方根的数*/ }while(num0); if (num==0) printf(the root is 0); else { this=1; do { pre=thi

C语言经典算法100例

#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);

}

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

*

***

******

********

******

***

C语言经典算法100例

*

___________________________________________________________________程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重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语句判断第二个字母。

___________________________________________________________________

C语言经典算法100例

程序源代码:

#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");

}

题目:练习函数调用

___________________________________________________________________

C语言经典算法100例

程序源代码:

#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(v …… 此处隐藏:3327字,全部文档内容请下载后查看。喜欢就下载吧 ……

C语言经典算法100例(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/132794.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)