信息学奥赛一本通 1061:求整数的和与均值 | OpenJudge NOI 1.5 04
生活随笔
收集整理的這篇文章主要介紹了
信息学奥赛一本通 1061:求整数的和与均值 | OpenJudge NOI 1.5 04
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【題目鏈接】
ybt 1061:求整數的和與均值
OpenJudge NOI 1.5 04:求整數的和與均值
【題目考點】
1. while循環
2. for循環
循環n次的兩種寫法
for(int i = 0; i < n; ++i){}
for(int i = 1; i <= n; ++i){}
3. 循環求和
- 設置加和變量s,記住要將其初始化為0。int s = 0;
- 循環讀入數據,將讀入的數據加到變量s之中。cin>>a; s += a;
4. 求均值
- 求和后,將和除以數據個數,即為均值。
【題解代碼】
解法1:使用for循環
#include <bits/stdc++.h> using namespace std; int main() {int n, s = 0, a;cin>>n;for(int i = 0; i < n; ++i){cin>>a;s += a;}cout<<s<<' '<<fixed<<setprecision(5)<<double(s) / n;return 0; }解法2:使用while循環
#include <bits/stdc++.h> using namespace std; int main() {int n, s = 0, a, i = 0;cin>>n;while(i < n){cin>>a;s += a;i++;}cout<<s<<' '<<fixed<<setprecision(5)<<double(s) / n;return 0; }總結
以上是生活随笔為你收集整理的信息学奥赛一本通 1061:求整数的和与均值 | OpenJudge NOI 1.5 04的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息学奥赛一本通(2055:【例3.5】
- 下一篇: 信息学奥赛一本通(1227:Ride t