作者:儒雅的天麟 | 来源:互联网 | 2023-10-12 09:17
这段时间在刷算法题,搬水果问题真是绝了,问题描述是这样的
![](https://img8.php1.cn/3cdc5/1e804/61b/af79fa0bb1662c9d.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkwNjgxMA==,size_16,color_FFFFFF,t_70#pic_center)
然后我写了下面的代码,在牛客的提交记录上没有我这样的方法所写的代码,但是我自己觉得这样的写法很好呀,用贪心算法。但是他就是通过不了,用例可以通过,但是最终就是无论如何都通过不了,有哪位大佬能帮我看看嘛,在此感谢!
#include
#include
typedef struct fruits
{int weight;int num;
}fruit[10000];int compar(const void*a,const void*b)
{struct fruits* num1=(struct fruits*)a;struct fruits* num2=(struct fruits*)b;if(num1->weight!=num2->weight)return num1->weight-num2->weight;elsereturn num1->num-num2->num;
}
int main()
{int n,i,k&#61;1,result&#61;0;while(scanf("%d",&n)!&#61;EOF&&n!&#61;0){fruit fru[n];for(i&#61;0;i<n;i&#43;&#43;){scanf("%d ",&fru[i]->weight);fru[i]->num&#61;i;}qsort(fru, n,sizeof(fru[0]), compar);int sum&#61;fru[0]->weight;while(k<n){sum&#43;&#61;fru[k]->weight;result&#43;&#61;sum;k&#43;&#43;;}printf("%d\n",result);}return 0;
}
以下是提交记录&#xff0c;用它的自测示例就可以通过
![在这里插入图片描述](https://img8.php1.cn/3cdc5/1e804/61b/f6cb693df1e11441.jpeg?x-oss-process&#61;image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkwNjgxMA&#61;&#61;,size_16,color_FFFFFF,t_70#pic_center)
但是提交就通过不了
![在这里插入图片描述](https://img8.php1.cn/3cdc5/1e804/61b/d92cd889ab8a1d96.jpeg?x-oss-process&#61;image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkwNjgxMA&#61;&#61;,size_16,color_FFFFFF,t_70#pic_center)
唉。。。。。。。。。。