2089 - 计算分段函数的值
编程,输入n后:输入n个数,根据下式计算并输出y值。
/ x2-sinx x<-2
y={ 2x+x -2<=x<=2
| ___________
\ √ X2+X+1 x>2
* 输出保留两位小数
Input
n
n个数
Output
y
Examples
Input
1 1
Output
3.00
Solution C
#include<stdio.h> #include<math.h> main() { int a; float n,sum; scanf("%d",&a); while(a--) { scanf("%f",&n); if(n<-2) printf("%.2f\n",pow(n,2)-sin(n)); else if(n>=-2&&n<=2) printf("%.2f\n",pow(2,n)+n); else printf("%.2f\n",pow(n*n+n+1,1/2.0)); } }
Solution C++
#include<iostream> #include<cmath> #include<cstdio> using namespace std; int main() { int n; cin>>n; for (int i=1; i<=n; i++) { double x,y=0; cin>>x; if (x<-2) y=x*x-sin(x); else if (x>2) y=sqrt(x*x+x+1); else y=pow(2,x)+x; printf("%.2lf\n",y); } return 0; }