#include <iostream>
#include <math.h>
using namespace std;
int t;
int main()
{
int x;
cout<<"输入x的值:"<<endl;
cin>>x;
if(x<2)
{
t=0;
}
else if(x>=2&&x<6)
{
t=1;
}
else if(x>=6&&x<10)
{
t=2;
}
else
{
t=3;
}
switch(t)
{
case 0: cout<<"函数值为:"<<x<<endl;
break;
case 1: cout<<"函数值为:"<<pow(x,2)+1<<endl;
break;
case 2: cout<<"函数值为:"<<-x<<endl;
break;
case 3: cout<<"函数值为:"<<-pow(x,2)-1<<endl;
break;
}
return 0;
#include <math.h>
using namespace std;
int t;
int main()
{
int x;
cout<<"输入x的值:"<<endl;
cin>>x;
if(x<2)
{
t=0;
}
else if(x>=2&&x<6)
{
t=1;
}
else if(x>=6&&x<10)
{
t=2;
}
else
{
t=3;
}
switch(t)
{
case 0: cout<<"函数值为:"<<x<<endl;
break;
case 1: cout<<"函数值为:"<<pow(x,2)+1<<endl;
break;
case 2: cout<<"函数值为:"<<-x<<endl;
break;
case 3: cout<<"函数值为:"<<-pow(x,2)-1<<endl;
break;
}
return 0;
}
总结:该算法本来要求用switch语句来写,虽然也用了但是这样的算法含金量不高,后续等我想明白怎么用巧妙的数学方式改善一下再更新吧。233333