#大庆师范学院[超话]##include
int main()
{
void hanoi(int n,char one,char two,char three);
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to move %d diskes:\n",m);
hanoi(m,'A','B','C');
return 0;
}
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
int main()
{
void hanoi(int n,char one,char two,char three);
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to move %d diskes:\n",m);
hanoi(m,'A','B','C');
return 0;
}
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
C语言-统计奇数偶数
#c语言#
①命令行和说明
#include< stdio.h>
struct pair {int n1,n2;};
/*结构体pair中有2个成员,
n1表示偶数个数,n2表示奇数个数。*/
~
②定义函数
struct pair fun(int*c,int d)
/*c是d个整数组成的数组,
d是数组中整数的个数。*/
{/*定义变量和赋初值*/
int i;
struct pair p;
/*p为结构体变量,
p.n1和p.n2是成员访问操作符。*/
p.n1=p.n2=0;
/*循环函数嵌套条件函数*/
for(i=0;i
/*循环计数器,遍历下标i。*/
{if(c[i]%2==0)p.n1++;
/*整除2为偶数,存入p.n1。*/
else p.n2++;}
/*否则为奇数,存入p.n2。*/
return p;
}
~
③主函数
int main()
{int i,c[100],d;
struct pair p;
printf("输入整数的个数:");
scanf("%d",&d);
printf("请输入%d个整数:",d);
for(i=0;i
scanf("%d",&(c[i]));
p=fun(c,d);
printf("n1=%d,n2=%d",p.n1,p.n2);
return 0;}
#c语言#
①命令行和说明
#include< stdio.h>
struct pair {int n1,n2;};
/*结构体pair中有2个成员,
n1表示偶数个数,n2表示奇数个数。*/
~
②定义函数
struct pair fun(int*c,int d)
/*c是d个整数组成的数组,
d是数组中整数的个数。*/
{/*定义变量和赋初值*/
int i;
struct pair p;
/*p为结构体变量,
p.n1和p.n2是成员访问操作符。*/
p.n1=p.n2=0;
/*循环函数嵌套条件函数*/
for(i=0;i
/*循环计数器,遍历下标i。*/
{if(c[i]%2==0)p.n1++;
/*整除2为偶数,存入p.n1。*/
else p.n2++;}
/*否则为奇数,存入p.n2。*/
return p;
}
~
③主函数
int main()
{int i,c[100],d;
struct pair p;
printf("输入整数的个数:");
scanf("%d",&d);
printf("请输入%d个整数:",d);
for(i=0;i
scanf("%d",&(c[i]));
p=fun(c,d);
printf("n1=%d,n2=%d",p.n1,p.n2);
return 0;}
C语言-迭代平方根
#c语言#
①源程序
#include
main()
/*定义变量类型。*/
{double a,x0,x1;
printf("求一个实数a的平方根x1\n");
/*do-while循环:
满足条件 a<=0时,执行循环输入实数a。不满足条件a<=0时,退出循环,即保证了a>0。*/
do
scanf("%lf",&a);
while(a<=0);
/*赋初值给x1。*/
x1=a/2.0;
/*do-while 循环:
满足条件(>误差1e-6)时,则继续循环迭代。
不满足条件(>误差1e-6)时,则停止循环迭代。
*/
do
{x0=x1;
x1=(x0+a/x0)/2.0;}
/*平方根迭代公式,用牛顿迭代法求得。*/
while((x1-x0)>1e-6||(x1-x0)<-1e-6);
/*迭代误差为1e-6,即e的负6次方,约等于2.71821的负6次方为0.000001。*/
printf("%f的平方根是%\n",a,x1);
}
~
②牛顿迭代法
曲线:y=f(x1)
曲线外一点:(x0,f(x0))
过点做曲线切线,切线的斜率:
f'(x1)=(y-f(x0))/(x1-x0)
当y=f(x1)=x1^2,即为平方函数时:
2x1=(x1^2-x0^2)/(x1-x0)
2x1^2-2x1x0=x1^2-x0^2
x1^2-2x1x0=-x0^2
x1^2+x0^2=2x1x0
a+x0^2=2x1x0
x1=(x0+a/x0)/2
即求得平方根迭代公式。
#c语言#
①源程序
#include
main()
/*定义变量类型。*/
{double a,x0,x1;
printf("求一个实数a的平方根x1\n");
/*do-while循环:
满足条件 a<=0时,执行循环输入实数a。不满足条件a<=0时,退出循环,即保证了a>0。*/
do
scanf("%lf",&a);
while(a<=0);
/*赋初值给x1。*/
x1=a/2.0;
/*do-while 循环:
满足条件(>误差1e-6)时,则继续循环迭代。
不满足条件(>误差1e-6)时,则停止循环迭代。
*/
do
{x0=x1;
x1=(x0+a/x0)/2.0;}
/*平方根迭代公式,用牛顿迭代法求得。*/
while((x1-x0)>1e-6||(x1-x0)<-1e-6);
/*迭代误差为1e-6,即e的负6次方,约等于2.71821的负6次方为0.000001。*/
printf("%f的平方根是%\n",a,x1);
}
~
②牛顿迭代法
曲线:y=f(x1)
曲线外一点:(x0,f(x0))
过点做曲线切线,切线的斜率:
f'(x1)=(y-f(x0))/(x1-x0)
当y=f(x1)=x1^2,即为平方函数时:
2x1=(x1^2-x0^2)/(x1-x0)
2x1^2-2x1x0=x1^2-x0^2
x1^2-2x1x0=-x0^2
x1^2+x0^2=2x1x0
a+x0^2=2x1x0
x1=(x0+a/x0)/2
即求得平方根迭代公式。
✋热门推荐