有疑问可以评论,学长看到会回复
简单分段函数:
y=2*x+1,x>=0
y=x,x<0
#include <iostream>
using namespace std;
int main() {
int x, y, a, b;
cin >> x;
y= x>=0 ? 2*x+1 : x; //x>=0,y=2*x+1;x<0,y=x;
cout << y << endl;
return 0;
}
复杂分段函数
y=-x+1(x<0)
y=x+1(0<=x<3)
y=2x+2(3<=x)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double x,y1,y2,y3;
cin>>x;
if(0>=x&&1){ //后面直接写1就行
y1=-x+1;
cout<<fixed<<setprecision(3)<<y1<<endl;}
if(0<=x&&x<3){
y2=x+1;
cout<<fixed<<setprecision(3)<<y2<<endl;}
if(3<=x&&x){
y3=2*x+2;
cout<<fixed<<setprecision(3)<<y3<<endl;}
return 0;
}
运行结果不在展示。