--- tags: DICE C --- 14-8 計算平均 === > [name=CHAWTeam] --- 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) --- 題目 --- ```! 請撰寫一程式,計算所有輸入整數的平均值,輸出到小數點後2位。 輸入的第1個整數,代表後面輸入整數的個數。 接下來,逐一輸入各個整數。 以輸入範例為例,第一個數字6表示會輸入6個整數, 6個整數為接下來的「1、2、3、5、6、7」。 輸入範例: 6 1 2 3 5 6 7 輸出範例: 4.00 C++小數點位數的處理: #include <iostream> #include <iomanip> using namespace std; int main(){ average=(float)sum/n; std::cout<<std::fixed<<std::setprecision(2)<<average; return 0; 輸出範例: 4.00 ``` 程式碼 --- ```c= #include <stdio.h> int main() { float x, n, sum = 0, i = 0; scanf("%f", &n); while (i < n) { scanf("%f", &x); sum += x; i++; } printf("%.2f", sum / n); return 0; } ``` 執行 --- ### 輸入 ```! 6 1 2 3 5 6 7 ``` ### 輸出 ```! 4.00 ``` --- [查看我們在HackMD上的所有筆記](https://hackmd.io/@CHAWTeam) 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) --- {%hackmd Iiu5mOixR7yWkPHKCkabBg %} <iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-14-8" 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