3092 - 2001年秋浙江省计算机等级考试二级C 编程题(1)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

编程,输入n后:输入n个数,根据下式计算并输出y值。

     / x2-sinx    x<-2

y={  2x+x       -2<=x<=2

     |    ___________

     \ √  X2+X+1                  x>2

* 输出保留两位小数

  

题目输入

n

n个数

题目输出

y

输入/输出样例

输入格式

1
1

输出格式

3.00

C语言解答

#include<stdio.h>
#include<math.h>
int main(){
    double x,y;
    int i,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%lf",&x);
        if(x<-2)
          y=x*x-sin(x);
        if(x>=-2&&x<=2)
          y=pow(2,x)+x;
        if(x>2)
          y=sqrt(x*x+x+1);
          printf("%.2lf\n",y);
    }
    return 0;
    }

C++解答

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
	int n;
	cin>>n;
	double x,y;
	for (int i=1; i<=n; i++)
	{
		cin>>x;
		if (x<-2) y=x*x-sin(x);
		else if(x>=-2 && x<=2) y=pow(2,x)+x;
		else y=sqrt(x*x+x+1);
		printf("%.2lf\n",y);
	}
	return 0;
}