c - 返回数组中的最大数
问题描述
typedef struct {int PID;
int Priority;
char *Status[8];
char *Program[20];}Process;
硬编码值
void initTable()
{
processList[0].PID = 112;
processList[0].Priority = 1;
strcpy(processList[0].Status,"Ready");
strcpy(processList[0].Program,"Alex.txt");
processList[1].PID = 101;
processList[1].Priority = 3;
strcpy(processList[1].Status,"Running");
strcpy(processList[1].Program,"Alex.txt");
processList[2].PID = 103;
processList[2].Priority = 2;
strcpy(processList[2].Status,"Ready");
strcpy(processList[2].Program,"Alex.txt");
}
我想返回在这个例子中具有最高优先级值的 i = 1 是最高优先级值
int getNextProcess()
{
for (int i = 0; i < len; ++i) {
// return the i with the highest Priority value
}
return i;
}
解决方案
// assuming `Priority` value is never less than 0
// returns -1 if something bad happens
int getNextProcess(Process *processList, int len)
{
int greatest = -1;
int greatest_i = -1;
for (int i = 0; i < len; ++i) {
if (processList[i].Priority > greatest) {
greatest = processList[i].Priority;
greatest_i = i;
}
}
return greatest_i;
}
推荐阅读
- neural-network - 我的神经网络是否达到了局部最小值,因为我的验证损失在达到 90% 的验证准确度后会发生变化?
- python-3.x - 使用 Google Directory API 创建 gmail 组的权限不足
- java - android.view.InflateException: Binary XML file line #11: Error inflating class EditText in android
- tensorflow - How can I modify ModelCheckPoint in keras to monitor both val_acc and val_loss and save accordingly the best model?
- recursion - (M68k) 为什么我的值没有传递到 D0?
- javascript - 点击计算数字
- kubernetes - 运行“top”时,linkerd cli 返回“无效参数”
- amazon-web-services - AWS Lambda 控制台 - 升级 boto3 版本
- profiling - 如何在 Pytroch 中逐层分析?
- ldap - ldapsearch 没有条目错误 32(openldap)