信息学奥赛一本通 1060:均值 | OpenJudge NOI 1.5 03
生活随笔
收集整理的這篇文章主要介紹了
信息学奥赛一本通 1060:均值 | OpenJudge NOI 1.5 03
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【題目鏈接】
ybt 1060:均值
OpenJudge NOI 1.5 03:均值
【題目考點】
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() {double s = 0, a;int n;cin>>n;for(int i = 0; i < n; ++i){cin>>a;s += a;}cout<<fixed<<setprecision(4)<<s / n;return 0; }解法2:使用while循環
#include<bits/stdc++.h> using namespace std; int main() {double s = 0, a;int n, i = 0;cin>>n;while(i < n){cin>>a;s += a;i++;}cout<<fixed<<setprecision(4)<<s / n;return 0; }總結
以上是生活随笔為你收集整理的信息学奥赛一本通 1060:均值 | OpenJudge NOI 1.5 03的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息学奥赛一本通 1087:级数求和 |
- 下一篇: 信息学奥赛一本通 2072:【例2.15