--- tags: DICE C --- 21-1 找絕對值 === > [name=CHAWTeam] 目錄:[DICE C語言程式破解](/@CHAWTeam/DiceC) 題目 --- ```! 求某數的絕對值 輸入一個浮點數x,使用數學函式fabs求絕對值,重複處理直到輸入的x為0。 請輸出到小數第一位。 輸入範例: -9.0 -3.0 -5.0 5.0 3.0 9.0 0 輸出範例: 9.0 3.0 5.0 5.0 3.0 9.0 C範例程式: #include <stdio.h> #include <stdlib.h> #include <math.h>/*用到fabs*/ int main() { double x;/*宣告一個數x*/ scanf(...);/*輸入x*/ while(...){/*如果x不為0,執行以下敘述式*/ printf...);/*使用fabs()函式,找出x的絕對值*/ scanf(...);/*再次輸入x*/ } return 0; } C++範例程式: #include <cmath>/*用到fabs*/ #include <iostream> #include <iomanip> using namespace std; int main() { double x;/*宣告一個數x*/ cin>>x;/*輸入x*/ while(x!=0){/*如果x不為0,執行以下敘述式*/ cout<<fixed<<setprecision(1)<<fabs(x)<<endl;/*使用fabs()函式,找出x的絕對值*/ cin>>x;/*再次輸入x*/ } return 0; } ``` 程式碼 --- ```c= #include <stdio.h> #include <math.h> int main() { double x; scanf("%lf", &x); while (x != 0) { printf("%.1f\n", fabs(x)); scanf("%lf", &x); } return 0; } ``` 執行 --- ### 輸入 ```! -9.0 -3.0 -5.0 5.0 3.0 9.0 0 ``` ### 輸出 ```! 9.0 3.0 5.0 5.0 3.0 9.0 ``` --- [查看我們在HackMD上的所有筆記](/@CHAWTeam) 目錄:[DICE C語言程式破解](/@CHAWTeam/DiceC) --- {%hackmd Iiu5mOixR7yWkPHKCkabBg %} <iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-21-1" width="100%"></iframe> --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up