練習2-11 計算分段函數[2] (10 分)
本題目要求計算下列分段函數f(x)的值:
注:可在頭文件中包含math.h
,並調用sqrt
函數求平方根,調用pow
函數求冪。
輸入格式:
輸入在一行中給出實數x。
輸出格式:
在一行中按“f(x) = result”的格式輸出,其中x與result都保留兩位小數。
輸入樣例1:
10
輸出樣例1:
f(10.00) = 3.16
輸入樣例2:
-0.5
輸出樣例2:
f(-0.50) = -2.75
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 7 int main(int argc, char *argv[]) { 8 double x,y; 9 scanf("%lf",&x); 10 11 if (x>=0) 12 y=sqrt(x); 13 14 else 15 y=pow(x+1,2)+2.0*x+1.0/x; 16 printf("f(%.2f) =%.2f",x,y); 17 18 return 0; 19 } 20 21