68分求助

B2047 分段函数

LandyLanty @ 2024-04-04 12:02:20

#include<bits/stdc++.h>
using namespace std;
double f(int x){
    if(x<5) return -x+2.5;
    else if(x<10) return 2-1.5*(x-3)*(x-3);
    else return x/2-1.5;
}
int main(){
double n;cin>>n;
    printf("%.3f",f(n));
    return 0;
}

by lzacode @ 2024-04-04 12:21:35

你的实参是double为哈你的形参是int(


by lzacode @ 2024-04-04 12:22:12

你可以尝试:不用函数,在int main里面算


by lzacode @ 2024-04-04 12:25:43

@LandyLanty AC:

#include<bits/stdc++.h>
using namespace std;
double f(double x){
    if(x<5) return -x+2.5;
    else if(x<10) return 2-1.5*(x-3)*(x-3);
    else return x/2-1.5;
}
int main(){
double n;cin>>n;
    printf("%.3lf",f(n));
    return 0;
}

by LandyLanty @ 2024-04-04 12:39:58

@lzacode 我去谢谢大佬!已关!萌新太久没打oi人变傻了(悲)


by lzacode @ 2024-04-05 00:38:08

@LandyLanty ^V^不用客气


|