递归向量中的最小正数 [英] Smallest positive number in a vector recursively

查看:54
本文介绍了递归向量中的最小正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,该函数应该递归地找到给定整数向量中的最小正数.

I'm writing a function that is supposed to recursively find the smallest positive number in a give vector of ints.

现在我的代码是这样的:

Right now my code looks like this:

 #include <vector>
 using namespace std;
 int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
    return -1;
    } else {
        int min = rec_min_pos(nums, size - 1);
        if(min < 0){
            if(nums[size - 1] <= 0){
                return -1;
            }else{
                return nums[size-1];
            }
        }else {
            if (min < nums[size - 1]) {
                return min;
            } else {
                return nums[size - 1];
            }
        }
    }
}
int main(){
    vector<int> nums = {2, -3, 6, -21, 10, -80, 8};
    cout << rec_min_post(nums, nums.size());
}

如果我运行这个,我得到 8,这大概是因为下一个元素是负数.

If I run this, I get 8, which is presumably because the next element is negative.

之前的版本:

#include <vector>
using namespace std;
int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
        return -1;
    } else {
        int min = rec_min_pos(nums, size - 1);

        if (min < nums[size - 1]) {
            return min;
        } else {
            return nums[size - 1];
        }
    }
}
int main(){
    vector<int> nums = {2, -3, 6, -21, 10, -80, 8};
    cout << rec_min_post(nums, nums.size());
}

我会得到 -80 这实际上是最小值,但我需要最小的正值.

I would get -80 which is in fact the smallest value, but I need the smallest positive value.

如何从整数向量中得到最小的正值?

What can I do to get the smallest positive value from the integer vector?

推荐答案

def SPN(nums, s):
  if s == 0:
    # Empty array
    return +∞
  if nums[s-1] > 0:
    # num[s] is admissible, recurse and keep the smallest
    return min(nums[s-1], SPN(nums, s-1))
  else:
    # Just recurse
    return SPN(nums, s-1)

print SPN(nums, len(nums)

C++ 版本:

#include <vector>
using namespace std;
int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
        return INT_MAX;
    }
    if(nums[size-1] > 0){
        return min(nums[size-1], rec_min_pos(nums, size-1));
    }
    else{
        return rec_min_pos(nums, size-1);
    }
}

<小时>

更新:

假设不允许保留 INT_MAX 表示 +∞,我们可以使用负数代替,例如 -1,约定为 min(x,-1) = x.

Assuming that reserving INT_MAX is not allowed to represent +∞, we can use a negative instead, say -1, with the convention that min(x,-1) = x.

Infty= -1 # Conventional +∞

def Min(a, b):
  if b == Infty:
    return a
  else:
    return min(a, b)

def SPN(nums, s):
  if s == 0:
    # Empty array
    return Infty 
  if nums[s-1] > 0:
    # num[s] is admissible, recurse and keep the smallest
    return Min(nums[s-1], SPN(nums, s-1))
  else:
    # Just recurse
    return SPN(nums, s-1)

如果我们使用任何负值表示 +∞ 的约定,这将导致我们得到一个更优雅的版本:

That leads us to a more elegant version if we use the convention that any negative value represents +∞:

def Min(a, b):
  if a < 0:
    return b
  if b < 0:
    return a
  return min(a, b)

def SPN(nums, s):
  if s == 1:
    return nums[0] # Assumes len(nums) > 0

  return Min(nums[s-1], SPN(nums, s-1))

优雅的 C++ 版本:

Elegant version in C++:

#include <vector>
using namespace std;
int minimum(int a, int b){
    if(a < 0){
        return b;
    }
    if(b < 0){
        return a;
    }
    return min(a, b);
}
int SPN(vector<int> nums, int size){
    if(size == 1){
        return nums[0];
    }
    return minimum(nums[size-1], SPN(nums, size-1));
}

这篇关于递归向量中的最小正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆