練習2-11 計算分段函數[2] (10 分)


練習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  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM