当前位置:网站首页>一本通循环结构的程序设计题解(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;
}新手,请多多指教
边栏推荐
- 基于卷积神经网络与双向长短时融合的锂离子电池剩余使用寿命预测
- Markdown 3 - 流程图表
- Execution order of select, from, join, on where groupby, etc. in MySQL
- There is no one of the strongest kings in the surveillance world!
- Tutorial on using the one-key upgrade function of the RTSP/Onvif video platform EasyNVR service
- 缓存一致性
- Greenplum 6.0有哪些不可错过的硬核升级与应用?
- Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
- DeFi 巨头进军 NFT 领域 用户怎么看?
- CMake library search function does not search LD_LIBRARY_PATH
猜你喜欢

忆联:激活数据要素价值潜能,释放SAS SSD创新红利

来n遍剑指--04. 二维数组中的查找
![[BJDCTF2020]Cookie is so stable-1|SSTI injection](/img/48/34955bbe3460ef09a5b8213c7cc161.png)
[BJDCTF2020]Cookie is so stable-1|SSTI injection

亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人

重建丢失的数据

【Kaggle比赛常用trick】K折交叉验证、TTA

Homework 7.29 correlation function directory and file attributes related functions

Markdown 1 - 图文音视频等

Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD

13-GuliMall 基础篇总结
随机推荐
物理服务器与虚拟机:主要区别和相似之处
int a=8,a=a++,a? int b=8,b=b+1,b?
int a=8,a=a++,a? int b=8,b=b+1,b?
JS事件参数对象event
手慢无!阿里亿级流量高并发系统设计核心原理全彩笔记现实开源
R语言使用方差分析ANOVA比较回归模型的差异、anova函数比较两个模型并报告它们是否存在显著差异(两个模型的数据相同,一个模型使用的预测特征包含另外一个模型的特征)
dolphinscheduler添加hana支持
DOM常用方法以及项目
第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
多表联查的学习
Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
13-GuliMall 基础篇总结
Rust from entry to proficient 02-installation
外包干了七年,废了。。。
no matching host key type found. Their offer: ssh-rsa
Hand tearing read-write lock performance test
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
基于空洞补全的动态SLAM方法
dolphinscheduler单机化改造
Current and voltage acquisition module DAM-6160