在结构数组中查找最大值

给定类型为Height的结构数组,找到max

null
struct Height{
  int feet;
  int inches;
}

问题来源: 微软面试体验集127 |(IDC校园)

这个想法很简单,遍历数组,并跟踪最大值 数组元素的值(英寸)=12*英尺+英寸

// CPP program to return max
// in struct array
#include <iostream>
#include <climits>
using namespace std;
// struct Height
// 1 feet = 12 inches
struct Height {
int feet;
int inches;
};
// return max of the array
int findMax(Height arr[], int n)
{
int mx = INT_MIN;
for ( int i = 0; i < n; i++) {
int temp = 12 * (arr[i].feet)
+ arr[i].inches;
mx = max(mx, temp);
}
return mx;
}
// driver program
int main()
{
// initialize the array
Height arr[] = {
{ 1, 3 },
{ 10, 5 },
{ 6, 8 },
{ 3, 7 },
{ 5, 9 }
};
int res = findMax(arr, 5);
cout << "max :: " << res << endl;
return 0;
}


输出:

max :: 125

本文由 曼迪星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享