藍橋杯2020年第十一屆C++省賽第六題-成績統計

 2023-12-25 阅读 26 评论 0

摘要:Idea 此題考查的對語言四舍五入的理解使用 Code 1 #include<iostream> using namespace std;int main() {int n;cin>>n;int greatCount = 0;int goodCount = 0;int i = 0;while(i<n){int grade;cin>>grade;if(grade>=60) goodCount&

請添加圖片描述
請添加圖片描述

Idea

此題考查的對語言四舍五入的理解使用

Code 1

#include<iostream>
using namespace std;int main()
{int n;cin>>n;int greatCount = 0;int goodCount = 0;int i = 0;while(i<n){int grade;cin>>grade;if(grade>=60) goodCount++;if(grade>=85) greatCount++;i++;}float good = 100.0*goodCount/n;float great = 100.0*greatCount/n;printf("%.0f%\n",good);printf("%.0f%\n",great);return 0;
}

Code 2

round()四舍五入函數

#include<iostream>
#include<cmath>
using namespace std;int main()
{int n;cin>>n;int greatCount = 0;int goodCount = 0;int i = 0;while(i<n){int grade;cin>>grade;if(grade>=60) goodCount++;if(grade>=85) greatCount++;i++;}cout << round(100.0 * goodCount / n) << "%" << endl;cout << round(100.0 * greatCount / n) << "%" << endl;return 0;
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/194709.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息