鱼C论坛

 找回密码
 立即注册
查看: 1040|回复: 2

[已解决]返回传入int数组中的最小值,求空白处代码

[复制链接]
发表于 2021-12-28 11:43:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
using namespace std;
int min(const int* numbers, int cnt)
{
        int res =  __________  ;
        for(  __________ ; i<cnt ; i++)
        {
                if(   __________)
                        res = numbers[i];
        }
        return res;
}
int main()
{
        int nums[] = {8, 12, 23, 4, 11};
        cout << min(nums, 5) <<endl;  //输出:4  
        return 0;
}
最佳答案
2021-12-28 13:49:17

  1. #include <iostream>
  2. using namespace std;
  3. int min(const int* numbers, int cnt)
  4. {
  5.         int res =numbers[0];//将数组第一个数假定为最小值
  6.         for( int i = 0 ; i<cnt ; i++)
  7.         {
  8.                 if(res>numbers[i])//出现比假定最小值还小的数就将更小的值赋给变量 res
  9.                         res = numbers[i];
  10.         }
  11.         return res;
  12. }
  13. int main()
  14. {
  15.         int nums[] = {8, 12, 23, 4, 11};
  16.         cout << min(nums, 5) <<endl;  //输出:4  
  17.         return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-28 13:49:17 | 显示全部楼层    本楼为最佳答案   

  1. #include <iostream>
  2. using namespace std;
  3. int min(const int* numbers, int cnt)
  4. {
  5.         int res =numbers[0];//将数组第一个数假定为最小值
  6.         for( int i = 0 ; i<cnt ; i++)
  7.         {
  8.                 if(res>numbers[i])//出现比假定最小值还小的数就将更小的值赋给变量 res
  9.                         res = numbers[i];
  10.         }
  11.         return res;
  12. }
  13. int main()
  14. {
  15.         int nums[] = {8, 12, 23, 4, 11};
  16.         cout << min(nums, 5) <<endl;  //输出:4  
  17.         return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-28 14:12:15 | 显示全部楼层
  1. #include <iostream>
  2. using namespace std;
  3. int min(const int* numbers, int cnt)
  4. {
  5.         int res =  numbers[0];
  6.         for(   int i = 1 ; i<cnt ; i++)
  7.         {
  8.                 if( numbers[i]<numbers[i-1])
  9.                         res = numbers[i];
  10.         }
  11.         return res;
  12. }
  13. int main()
  14. {
  15.         int nums[] = {8, 12, 23, 4, 11};
  16.         cout << min(nums, 5) <<endl;  //输出:4  
  17.         return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 14:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表