[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 (输入字符串以及输