#include<stdio.h> #include<math.h> int main (){ double x,m; scanf("%lf",&x); if(x>=0){ m=sqrt(x,0.5); }else { m=pow(x+1,2)+2*x+1/x; } printf("f(%.2f) = %.2f",x,m); return 0; } 哪里出错
时间: 2024-03-09 20:51:06 浏览: 20
在使用sqrt函数时,只需要传入一个参数,即被开方的数,而不需要传入第二个参数。如果您想计算x的平方根,您可以这样写代码:m=sqrt(x);
此外,当x小于0时,计算公式也有错误,应该写成 m=pow(x+1,2)+2*x+1.0/x;
修改后的代码如下:
```c
#include<stdio.h>
#include<math.h>
int main (){
double x,m;
scanf("%lf",&x);
if(x>=0){
m=sqrt(x);
}else {
m=pow(x+1,2)+2*x+1.0/x;
}
printf("f(%.2f) = %.2f",x,m);
return 0;
}
```
相关问题
求两点间的距离#include <stdio.h> #include <math.h> typedef struct { double x, y; } POINT; int main() { POINT a, b; double s, p, q; scanf("%lf %lf", &a.x, &a.y); scanf("%lf %lf", &b.x, &b.y); printf("%g\n", s);
你可以使用勾股定理求解两点之间的距离,具体代码如下:
```c
#include <stdio.h>
#include <math.h>
typedef struct {
double x, y;
} POINT;
int main() {
POINT a, b;
double s, p, q;
scanf("%lf %lf", &a.x, &a.y);
scanf("%lf %lf", &b.x, &b.y);
p = a.x - b.x;
q = a.y - b.y;
s = sqrt(p * p + q * q);
printf("%g\n", s);
return 0;
}
```
其中,p和q分别代表两个点在x轴和y轴上的坐标差值,s为两点之间的距离,使用了数学库中的sqrt函数来求解根号。
#include <stdio.h> #include <math.h> typedef struct { double x, y; } POINT; int main() { POINT a, b; double s, p, q; scanf("%lf %lf", &a.x, &a.y); scanf("%lf %lf", &b.x, &b.y); /* 你提交的代码将被嵌在这里 */ printf("%g\n", s); return 0; }
下面是计算两点间距离并输出的代码:
```c
#include <stdio.h>
#include <math.h>
typedef struct {
double x, y;
} POINT;
int main() {
POINT a, b;
double s, p, q;
scanf("%lf %lf", &a.x, &a.y);
scanf("%lf %lf", &b.x, &b.y);
// 计算两点间距离
s = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
// 输出结果
printf("%g\n", s);
return 0;
}
```
输入格式为两行,每行两个实数,分别代表两个点的坐标。输出格式为一个实数,代表两点间距离。