当前位置:网站首页>一本通循环结构的程序设计题解(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;
}新手,请多多指教
边栏推荐
- 并行化快速排序设想
- 企业如何成功完成云迁移?
- MySQL【多表查询】
- WinForm枚举容器中的控件,实现控件统一事件处理机制
- int a=8,a=a++,a? int b=8,b=b+1,b?
- 基于柔性人机接口的人机协调运动控制方法
- Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
- R语言使用方差分析ANOVA比较回归模型的差异、anova函数比较两个模型并报告它们是否存在显著差异(两个模型的数据相同,一个模型使用的预测特征包含另外一个模型的特征)
- MySQL查询性能优化
- ModelCoder状态机:对柴油机工况判断策略进行建模
猜你喜欢
随机推荐
dolphinscheduler simple task definition and complex cross-node parameter transfer
666666
How to solve the problem that the page does not display the channel configuration after the EasyNVR is updated to (V5.3.0)?
CMake library search function does not search LD_LIBRARY_PATH
datax enables hana support and dolphinscheduler enables datax tasks
What happened when the computer crashed?
Heshu Group: Make smart cities smarter and make real life better
大手笔!两所“双一流”大学,获75亿元重点支持!
CV-Model【2】:MobileNet v1
每天学一点Scala之 伴生类和伴生对象
Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
元宇宙的六大支撑技术
重建丢失的数据
int a=8,a=a++,a? int b=8,b=b+1,b?
常见的云计算安全问题以及如何解决
MySQL【多表查询】
双击Idea图标打不开——解决办法
DOM常用方法以及项目
JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
Jackson 的JAR包冲突问题









