【题目描述】小王是公司的仓库管理员一天他接到了这样一个任务:从仓库中找出一根钢管。这听起来不算什么但是这根钢管的要求可真是让他犯难了要求如下:1这根钢管一定要是仓库中最长的2这根钢管一定要是最长的钢管中最细的3这根钢管一定要是符合前两条的钢管中编码最大的每根钢管都有一个互不相同的编码越大表示生产日期越近相关的资料到是有可是手工从几百份钢管材料中选出符合要求的那根要不还是请你编写个程序来帮他解决这个
#include <iostream>
using namespace std;
struct SteelPipe {
int length;
int thickness;
int code;
};
int main() {
int n;
cin >> n;
SteelPipe *pipes = new SteelPipe[n];
for (int i = 0; i < n; i++) {
cin >> pipes[i].length >> pipes[i].thickness >> pipes[i].code;
}
int maxLength = 0;
int maxThickness = INT_MAX;
int maxCode = INT_MIN;
int maxIndex = -1;
for (int i = 0; i < n; i++) {
if (pipes[i].length > maxLength) {
maxLength = pipes[i].length;
maxThickness = pipes[i].thickness;
maxCode = pipes[i].code;
maxIndex = i;
} else if (pipes[i].length == maxLength) {
if (pipes[i].thickness < maxThickness) {
maxThickness = pipes[i].thickness;
maxCode = pipes[i].code;
maxIndex = i;
} else if (pipes[i].thickness == maxThickness) {
if (pipes[i].code > maxCode) {
maxCode = pipes[i].code;
maxIndex = i;
}
}
}
}
cout << pipes[maxIndex].length << " " << pipes[maxIndex].thickness << " " << pipes[maxIndex].code << endl;
delete[] pipes;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/hDHc 著作权归作者所有。请勿转载和采集!