C/C++求给定数组的次大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | int subMax( int a[], int n) { int max=a[0]; int sub_max=a[0]; for ( int i = 1; i < n; i++) { if (a[i]>max) { //update max and sub max sub_max=max; max=a[i]; } else if (a[i]<max) { if (a[i]>sub_max) { //update sub max sub_max=a[i]; } } else { //do nothing } } return sub_max; } |
本文作者:无极至上
本文链接:https://www.cnblogs.com/songweiren/p/13863050.html
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步