鱼C论坛

 找回密码
 立即注册
查看: 1106|回复: 3

[已解决]想知道这个哪里有问题

[复制链接]
发表于 2022-3-28 23:48:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目是:f(x)=1/x+绝对值x  x>20
             f(x)=根号3x-2     10<=x<=20
             f(x)=x*x+3*x+2   x<10
#include <stdio.h>
#include <math.h>
int main()
{
        int x;
        double f;
        scanf("%d",&x);
        if (x<10)
        {
            f=x*x+3*x+2;
             printf("%.2f\n",f);
    }
    else if (10<=x<=20)
    {
            f=sqrt(3*x)-2;
            printf("%.2f\n",f);
        }
        else if (x>20)
        {
                f=1/x+abs(x);
                printf("%f\n",f);
         }
         return 0;
}
输入100时跳出10.25?
最佳答案
2022-3-29 00:02:41
注释地方是改动过的

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     double x;   //
  6.     double f;
  7.     scanf("%lf", &x);   //
  8.     if (x < 10)
  9.     {
  10.         f = x * x + 3 * x + 2;
  11.         printf("%.2lf\n", f);
  12.     }
  13.     else if (x >= 10 && x <= 20)  //
  14.     {
  15.         f = sqrt(3 * x) - 2;
  16.         printf("%.2lf\n", f);
  17.     }
  18.     else if (x > 20)
  19.     {
  20.         f = 1 / x + abs(x);
  21.         printf("%lf\n", f);
  22.     }
  23.     return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-28 23:59:40 | 显示全部楼层
中间的else if改为(x >= 10 && x <= 20)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-29 00:02:41 | 显示全部楼层    本楼为最佳答案   
注释地方是改动过的

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     double x;   //
  6.     double f;
  7.     scanf("%lf", &x);   //
  8.     if (x < 10)
  9.     {
  10.         f = x * x + 3 * x + 2;
  11.         printf("%.2lf\n", f);
  12.     }
  13.     else if (x >= 10 && x <= 20)  //
  14.     {
  15.         f = sqrt(3 * x) - 2;
  16.         printf("%.2lf\n", f);
  17.     }
  18.     else if (x > 20)
  19.     {
  20.         f = 1 / x + abs(x);
  21.         printf("%lf\n", f);
  22.     }
  23.     return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-29 09:34:44 From FishC Mobile | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>

  3. double foo(int x){ return x > 20 ? 1./x + fabs(x) : x >= 10 ? sqrt(3. * x) - 2 : x * x + 3 * x + 2; }

  4. int main(){
  5.         int x;
  6.         double f;
  7.         scanf("%d", &x);
  8.         f = foo(x);
  9.         printf("%.4lf", f);
  10.         return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 14:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表