我试图解决这个HackerRank问题:
测试此输入:第一行包含三个空格分隔的整数: b、n和m-预算、键盘型号数和USB驱动器型号数。第二行包含n个空格分隔的整数keyboard[i],每个键盘模型的价格.第三行包含m个空格分隔的整数drives[i],即USB驱动器的价格.
输入:
10 2 3
3 1
5 2 8
它返回产出8,而不是预算10下的9。
int getMoneySpent(int keyboards_count, int* keyboards, int drives_count, int* drives, int b) {
int i,j,set_max=0,current=-1;
for(j=0;j<=drives_count;j++)
{
for(i=0;i<=keyboards_count;i++)
{
if(drives[j]+keyboards[i]>=set_max)
{
set_max=drives[j]+keyboards[i];
if(set_max<=b)
{
current=set_max;
}
}
}
}
return current;
}发布于 2020-11-21 06:09:31
在这里这个很管用。你的逻辑实现有点错误。如果和大于当前和,则需要检查它,而不是set_max以获得最大和作为输出。
int getMoneySpent(int keyboards_count, int* keyboards, int drives_count, int* drives, int b) {
int i,j,set_max=0,current=-1;
for(j=0;j<drives_count;j++)
{
for(i=0;i<keyboards_count;i++)
{
if(drives[j]+keyboards[i]>=current)
{
set_max=drives[j]+keyboards[i];
if(set_max<=b)
{
current = set_max;
}
}
}
}
return current;
}https://stackoverflow.com/questions/64940240
复制相似问题