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