[YTU]_2718 (求最高同学位置)
生活随笔
收集整理的这篇文章主要介绍了
[YTU]_2718 (求最高同学位置)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Description
设一维数组存放了n(<100)名同学的身高,编写函数求身高最高同学的位置,如果结果有多个,需要输出所有人的位置。
部分代码已给定如下,只需要提交缺失的代码。
#include <iostream>
using namespace std;
int main()
{
int getHeight(float height[],int n,float result[]);
float height[100],result[100];
int i,num,n;
cin>>n;
for(i=0; i<n; i++)
cin>>height[i];
num=getHeight( height,n,result);
for(i=0; i<num; i++)
cout<<i+1<<":"<<result[i]<<endl;
return 0;
}
Input
n和n名同学的身高
Output
身高最高同学的位置,多个结果每行显示一个。
Sample Input
10 1.7 1.69 1.82 1.59 1.93 1.77 1.93 1.78 1.93 1.72Sample Output
1:5 2:7 3:9#include <iostream> using namespace std;int main() {int getHeight(float height[],int n,float result[]);float height[100],result[100];int i,num,n;cin>>n;for(i=0; i<n; i++)cin>>height[i];num=getHeight( height,n,result);for(i=0; i<num; i++)cout<<i+1<<":"<<result[i]<<endl;return 0; } int getHeight(float height[],int n,float result[]) {float*p=height;float max=height[0];int i,m=0;for(i=0;i<n;i++,p++){if(*p>max)max=*p;}int i1=0;for(i=0,p=height;i<n;i++){ if(max==*p++){result[i1]=i+1;i1++;}}return i1; }总结
以上是生活随笔为你收集整理的[YTU]_2718 (求最高同学位置)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: [YTU]_2716 统计不及格人数
- 下一篇: [YTU]_1055 (输入字符串以及输