二分法求一个数列中是否存在一个数
提示:在书写的时候要注意if中的判断等号要两个!!!!
#define M 10
#include<stdio.h>
void main()
{
static int a[M]={-12,0,6,16,23,56,80,100,110,115};
int b,mid,low,high,n;
printf("please input a number:\n");
scanf("%d",&b);
low=0;
high=M-1;
while(low<=high)
{
mid=(low+high)/2;
if(b>a[mid])
{
low=mid+1;
}
else if(b==a[mid])
{
n=1;
break;
}
else
{
high=mid-1;
}
}
if(n==1)
{
printf("the index is no.%d\n",mid);
}
else
{
printf("the number is not exit\n");
}
}
自己编写pow()函数,即求a的b次方是多少
#define M 10
#include<stdio.h>
void main()
{
float power(float x,float y);
float a,b,c;
printf("please input the number\n");
scanf("%f,%f",&a,&b);
c=power(a,b);
printf("the %f 的 %f 次方等于%f\n",a,b,c);
}
float power(float x,float y)
{
float z=1.0;
int i;
for(i=0;i<y;i++)
{
z=x*z;
}
return(z);
}
自己编写sqrt()函数,默认输入的数据都是整数。
#include<stdio.h>
void main()
{
int sqrt(int x);
int a,b;
printf("please input the number\n");
scanf("%d",&a);
b=sqrt(a);
if(b!=0)
{
printf("the %d 开方等于%d\n",a,b);
}
else
{
printf("the number is wrong!");
}
}
int sqrt(int x)
{
int y,z,i;
for(i=1;i<=x/2;i++)
{
y=i*i;
if(y==x)
{
z=i;
break;
}
z=0;
}
return(z);
}
先求2和3的平方再求阶乘
方法一:
#include<stdio.h>
long ping(int x); //求平方
long jie(int y); //求阶乘
void main()
{
int i;
long s=0;
for(i=2;i<=3;i++)
{
s=s+ping(i);
}
printf("%ld\n",s);
}
long ping(int x)
{
int k;
long r;
long jie(int y);
k=x*x;
r=jie(k);
return(r);
}
long jie(int y)
{
long c=1;
int i;
for(i=1;i<=y;i++)
{
c*=i;
}
return(c);
}
#include<stdio.h>
void main()
{
long ping(int x);
long jie(int y);
int a=2;
int b=3;
int s,s1,s2;
s1=ping(a);
s2=ping(b);
s=s1+s2;
printf("the anser is %d\n",s);
}
long ping(int x) //ping调用jie
{
int d;
long u;
d=x*x;
u=jie(d);
return(u);
}
long jie(int y)
{
int i;
int z=1;
for(i=1;i<y;i++)
{
z=i*z;
}
return(z);
}
#include<stdio.h>
void main()
{
long ping(int x);
long jie(int y);
int a=2;
int b=3;
int s,s1,s2;
s1=jie(a);
s2=jie(b);
s=s1+s2;
printf("the anser is %d\n",s);
}
long jie(int y) //jie调用ping
{
int i;
int z=1;
int u;
u=ping(y);
for(i=1;i<u;i++)
{
z=i*z;
}
return(z);
}
long ping(int x)
{
int d;
d=x*x;
return(d);
}
用递归调用求阶乘:
#include<stdio.h>
void main()
{
long f(int x); //递归函数
int a,b;
printf("please input a number:\n");
scanf("%d",&a);
b=f(a);
printf("the number %d 's jiecheng is %d\n",a,b);
}
long f(int x)
{
long i,y;
if(x==0 || x==1)
{
y=1;
}
else
{
y=f(x-1)*x;
}
return(y);
}
汉诺塔(hanoi)问题
#include<stdio.h>
void main()
{
void hanoi(int n,char x,char y,char z);
void move(char w,char u);
int a;
printf("please input the dishes:\n");
scanf("%d",&a);
printf("the step is :\n");
hanoi(a,'A','B','C');
}
void hanoi(int n,char x,char y,char z)
{
if(n==1)
{
move(x,z);
}
else
{
hanoi(n-1,x,z,y); //把n-1个先借助C放到B
move(x,z); //把第n个放到C
hanoi(n-1,y,x,z); //把n-1个借助A放到C
}
}
void move(char w,char u)
{
printf("%c->%c\n",w,u);
}
输入一组成绩,求成绩的平均值。
#include<stdio.h>
void main()
{
float average(int b[10]);
int a[10];
int i;
float b;
printf("please input the grade:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
b=average(a); //传递数组的名称即可
printf("the average grade is :%f\n",b);
}
float average(int b[10])
{
float u;
int j;
int w=0;
for(j=0;j<10;j++)
{
w=w+b[j];
}
u=w/10.0;
return(u);
}