top1编程
← 返回题目
题解

分段函数

1 条题解

  • 0
    @ 2026-7-28 22:45:10
    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main(){
    	int a;
    	double x,y;
    	cin>>x;
    	if(0<=x&&x<5)
    	a=1;
    	else if(5<=x&&x<10)
    	a=2;
    	else if(10<=x&&x<20)
    	a=3;
    	switch(a){
    		case 1:
    			y=-x+2.5;
    			cout<<fixed<<setprecision(3)<<y;break;
    		case 2:
    			y=2-1.5*(x-3)*(x-3);
    			cout<<fixed<<setprecision(3)<<y;break;
    		case 3:
    			y=x/2.0-1.5;
    			cout<<fixed<<setprecision(3)<<y;break;
    	}
    	return 0;	
    }
    
    • 1