3219: 求最高同学位置—C语言版

3219: 求最高同学位置—C语言版

时间限制: 1 Sec  内存限制: 128 MB
提交: 207  解决: 115
[提交][状态][讨论版][命题人:smallgyy]

题目描述

设一维数组存放了n(<100)名同学的身高,编写函数求身高最高同学的位置,如果结果有多个,需要输出所有人的位置。
部分代码已给定如下,只需要提交缺失的代码。

#include<stdio.h>

int main()

{

    int getHeight(float height[],int n,int result[]);

    float height[100];

         int result[100];

    int i,num,n;

         scanf("%d",&n);

    for(i=0; i<n; i++)

            scanf("%f",&height[i]);       

    num=getHeight( height,n,result);

    for(i=0; i<num; i++)

                   printf("%d:%d ",i+1,result[i]);

    return 0;

}

输入

n和n名同学的身高

输出

身高最高同学的位置,多个结果每行显示一个。

样例输入

10
1.7 1.69 1.82 1.59 1.93 1.77 1.93 1.78 1.93 1.72

样例输出

1:5
2:7
3:9

int getHeight(float height[],int n,int result[])
{
    int i, j = 0;
    for(i = 0; i < n; ++i)
        result[i] = 0;
    float max = 0;
    for(i = 0; i < n; ++i)
    {
        if(height[i] > max)
            max = height[i];
    }
    for(i = 0; i < n; ++i)
    {
        if(height[i] == max)
        {
            result[j++] = i + 1;  //若有多个,则打印这些高的同学的所在位置(先用数组装起来)
        }
    }
    return j;
}

  

原文地址:https://www.cnblogs.com/mjn1/p/9898446.html