当前位置:网站首页>一本通循环结构的程序设计题解(2)
一本通循环结构的程序设计题解(2)
2022-07-30 12:45:00 【竹林居士-】
1、求平均年龄
原题链接
http://ybt.ssoier.cn:8088/problem_show.php?pid=1059
【题目描述】
班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。
【输入】
第一行有一个整数n(1 ≤ n ≤ 100),表示学生的人数。其后n行每行有1个整数,表示每个学生的年龄,取值为15到25。
【输出】
输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。
【输入样例】
2
18
17
【输出样例】
17.50
代码:
#include<iostream>
using namespace std;
int n,a[120];
float ans;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) ans+=a[i];
ans/=n;
printf("%.2lf",ans);
return 0;
}2、均值
原题链接
http://ybt.ssoier.cn:8088/problem_show.php?pid=1060【题目描述】
给出一组样本数据,包含n个浮点数,计算其均值,精确到小数点后4位。
【输入】
输入有两行,第一行包含一个整数n(n小于100),代表样本容量;第二行包含n个绝对值不超过1000的浮点数,代表各个样本数据。
【输出】
输出一行,包含一个浮点数,表示均值,精确到小数点后4位
【输入样例】
2
1.0 3.0
【输出样例】
2.0000
代码:
#include<iostream>
using namespace std;
int n;
float a[1020];
float ans;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
ans+=a[i];
}
ans/=n;
printf("%.4lf",ans);
return 0;
}3、求整数的和与均值
原题链接
http://ybt.ssoier.cn:8088/problem_show.php?pid=1061
【题目描述】
读入n(1≤n≤10000)个整数,求它们的和与均值。
【输入】
输入第一行是一个整数n,表示有n个整数。
第2~n+1行每行包含1个整数。每个整数的绝对值均不超过10000。
【输出】
输出一行,先输出和,再输出平均值(保留到小数点后5位),两个数间用单个空格分隔。
【输入样例】
4
344
222
343
222
【输出样例】
1131 282.75000
代码:
#include<iostream>
using namespace std;
int n;
int a[10020];
double ans;
int sum;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
ans+=a[i];
sum+=a[i];
}
ans/=n;
printf("%d %.5lf",sum,ans);
return 0;
}4、最高的分数
【题目描述】
孙老师讲授的《计算概论》这门课期中考试刚刚结束,他想知道考试中取得的最高分数。因为人数比较多,他觉得这件事情交给计算机来做比较方便。你能帮孙老师解决这个问题吗?
【输入】
输入两行,第一行为整数n(1 ≤ n < 100),表示参加这次考试的人数.第二行是这n个学生的成绩,相邻两个数之间用单个空格隔开。所有成绩均为0到100之间的整数。
【输出】
输出一个整数,即最高的成绩。
【输入样例】
5
85 78 90 99 60
【输出样例】
99
代码:
#include<iostream>
#include<cmath>
using namespace std;
int n;
int a[120];
int res=-1;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
res=max(res,a[i]);
}
cout<<res<<endl;
return 0;
}新手,请多多指教
边栏推荐
猜你喜欢

来n遍剑指--04. 二维数组中的查找

【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解

云主机上的MongoDB被威胁,开启AUTH认证

How to display an Excel table in the body of an email?

JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.

Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线

奇异值分解(SVD)原理与在降维中的应用(附带例题讲解)(纯理论)

ModelCoder状态机:对柴油机工况判断策略进行建模

13-GuliMall Basics Summary

【Kaggle比赛常用trick】K折交叉验证、TTA
随机推荐
Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
[BJDCTF2020]Cookie is so stable-1|SSTI注入
忆联:激活数据要素价值潜能,释放SAS SSD创新红利
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
【语音识别】基于GMM-HMM的语音识别系统
datax开启hana支持以及dolphinscheduler开启datax任务
【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解
无人艇轨迹跟踪的预设性能抗扰控制研究
There is no one of the strongest kings in the surveillance world!
JS事件参数对象event
Execution order of select, from, join, on where groupby, etc. in MySQL
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
并行化快速排序设想
for循环的3个表达式执行顺序
R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化(平方、开平方、指数化等函数类似使用)
亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人
【ASP.NET Core】选项类的依赖注入
What happened when the computer crashed?
13-GuliMall Basics Summary