1、输入x,计算并输出下列分段函数sign(x)的值,要求定义和调用函数sign(x)实现该分段函数。
输入输出示例(共运行3次)
第一次运行:
Enter x: 10
sign(10)=1
第二次运行:
Enter x: -5
sign(-5)=-1
第三次运行:
Enter x: 0
sign(0)=0
参考程序:
#include
int sign(float x);
void main()
{
float x;
int y;
cout<<"Enter x:";
cin>>x;
y=sign(x);
cout<<"sign("<
}
int sign(float x)
{
if(x>0)
return 1;
else if(x==0)
return 0;
else
return -1;
}
2、输入一批正整数(以零或负数为结束标志),求其中的奇数和。要求定义和调用函数even(n)判断数的奇偶性,当n为偶数时返回1,否则返回0。
输入输出示例
Input integer: 12 9 7 18 3 11 20 0
The sum of the odd numbers is 30
参考程序:
#include
int even(int n);
void main()
{
int x,jsh=0;
cout<<"Input integer: ";
cin>>x;
while(x>0)
{
if(even(x)==0)
jsh+=x;
cin>>x;
}
cout<<"The sum of the odd numbers is "<
}
int even(int n)
{
if(n%2==0)
return 1;
else
return 0;
}
3、给定平面任意两个坐标(x1,y1)和(x2,y2),求这两点之间的距离(保留2为小数)。要求定义和调用函数dist(x1,y1,x2,y2)计算两点间的距离。
输入输出示例
Input(x1,y1): 10 10
Input(x2,y2): 200 100
Distance=210.24
参考程序:
#include
#include
float dist(float x1,float y1,float x2,float y2);
void main()
{
float x1,y1,x2,y2,distance;
printf("Input(x1,y1):");
scanf("%f%f",&x1,&y1);
printf("Input(x2,y3):");
scanf("%f%f",&x2,&y2);
distance=dist(x1,y1,x2,y2);
printf("Distance=%.2f",distance);
}
float dist(float x1,float y1,float x2,float y2)
{
float dis;
dis=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return dis;
}
4、计算1!+2!+…+10!的值,要求定义并调用函数fact(n)计算n!,函数类型是double。(结果是:4.03791e+006)
参考程序:
#include
double fact(int n);
void main()
{
int i;
double res=0;
for(i=1;i<=10;i++)
res+=fact(i);
cout<
}
double fact(int n)
{
double jie=1;
int i;
for(i=1;i<=n;i++)
jie*=i;
return jie;
}
5、输入精度e,输入x,用下列公式求cos x的近似值,精确到最后一项的绝对值小于e。要求定义和调用函数funcos(e,x) 求余弦函数的近似值。
输入输出示例
Input e:0.0001
Input x:2.1
cos(2.1)=-0.504861
参考程序:
#include
#include
float funcos(float e,float x);
void main()
{
float e,x,res;
cout<<"Input e:";
cin>>e;
cout<<"Input x:";
cin>>x;
res=funcos(e,x);
cout<<"cos("<
}
float funcos(float e,float x)
{
float t,fuhao,fenzi,fenmu,res=0;
int i=1;
fuhao=1;
fenzi=1;
fenmu=1;
t=fuhao*fenzi/fenmu;
while(fabs(t)>=e)
{
res+=t;
fuhao=-fuhao;
fenzi=fenzi*x*x;
fenmu=fenmu*(2*i-1)*(2*i);
i++;
t=fuhao*fenzi/fenmu;
}
return res;
}
6、输入2个整数,分别将其逆向输出,要求定义并调用函数fun(n),它的功能是返回n的逆向值。例如,fun(123)的返回值是321。
输入输出示例
Input m1: 123
Input m2:-910
123的逆向是321
-910的逆向是-19
参考程序:
#include
int fun(int n);
void main()
{
int m1,m2,r1,r2;
cout<<"Input m1:";
cin>>m1;
cout<<"Input m2:";
cin>>m2;
r1=fun(m1);
r2=fun(m2);
cout<
cout<
}
int fun(int n)
{
int res=0,m;
while(n!=0)
{
m=n%10;
res=res*10+m;
n=n/10;
}
return res;
}
7、输入2个正整数m和n(m>=1,n<=10 000),输出m~n之间所有的Fibonacci数。Fibonacci数列(第一项起):1,1,2,3,5,8,13,21,…。要求定义并调用函数fib(n),它的功能是返回第n项Fibonacci数。例如,fib(7)的返回值是13。
输入输出示例(括号内为说明文字)
Input m:20
Input n:100
21 34 55 89(20到100之间的Fibonacci数)
参考程序:
#include
int fib(int n);
void main()
{
int m,n,i;
cout<<"Input m:";
cin>>m;
cout<<"Input n:";
cin>>n;
i=1;
while(fib(i)
i++;
while(fib(i)<=n) //通过循环将大于等于m,并且小于等于n的Fibonacci数输出出来
{
cout<
i++;
}
}
int fib(int n)
{
int n1=1,n2=1,i,temp;
if((n==1)||(n==2))
return 1;
for(i=3;i<=n;i++)
{
temp=n1+n2;
n1=n2;
n2=temp;
}
return n2;
}
本文来源:https://www.2haoxitong.net/k/doc/561614dfa58da0116c17493b.html
文档为doc格式