从长度为n的数组里选出m个数使和为固定值sum

6 篇文章 0 订阅

原文章地址:https://blog.csdn.net/a987073381/article/details/52016960

2018年6月22日

 话不多说,这个题很可能出现在你笔试的编程题当中,因此还是有必要做一做的。

刚开始碰到这个问题,准备用穷尽法进行作答,但是发现自己并不知道怎么怎么进行穷举,利用位操作很好的了这个问题。

比方10 个数 我们可以穷举100000 00000   ~1 11111 11111,这样一个11位来进行,一个数组{-10, 6, 9, 10, 17, 18, 20, 35, 45, 99},对应其中的某位,0表示选中,1表示没选中,因为要选出里面的m个数来使得 和为sum,所以是穷举所有的这个11位  中有6位的情况(因为第一个占一个1),

int NumOf1(int num)              //统计其中的1的个数
{  
    int count = 0;  
    while (num)  
    {  
        num = num & (num - 1);  
        count++;  
    }  
    return count;  
}  
  
void CalSum(vector<int> &nums, int result, int m)    
{  
    int len = nums.size();  
    int bit = 1 << len;  
    for (int i = 1; i < bit; i++)                //从1循环到2^N    
    {    
        int sum = 0;    
        vector<int> tmp;  
        if (NumOf1(i) == m)  
        {  
            for (int j = 0; j < len; j++)    
            {    
                if ((i & 1 << j) != 0)        //用i与2^j进行位与运算,若结果不为0,则表示第j位不为0,从数组中取出第j个数    
                {    
                    sum += nums[j];    
                    tmp.push_back(nums[j]);    
                }    
            }    
            if (sum == result)  
            {  
                for (vector<int>::iterator iter = tmp.begin(); iter != tmp.end(); iter++)  
                {  
                    cout << *iter << " ";  
                }  
                cout << endl;  
            }  
        }  
    }    
}   

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C++代码如下:#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> arr = {1,2,3,4,5,6,7,8,9,10}; random_shuffle(arr.begin(), arr.end()); vector<int> three_numbers(arr.begin(), arr.begin()+3); for (int i=0; i<three_numbers.size(); i++) cout << three_numbers[i] << " "; return 0; } ### 回答2: 以下是一个实现C++中通过PickPivot随机从数组选出个数的代码: #include <iostream> #include <random> using namespace std; int main() { // 定义一个数组 int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 初始化随机数生成器 random_device rd; mt19937 gen(rd()); uniform_int_distribution<int> dist(0, 9); // 从数组中随机选三个数 int p1 = arr[dist(gen)]; int p2 = arr[dist(gen)]; int p3 = arr[dist(gen)]; // 打印选出的三个数 cout << "选出的三个数分别为:" << p1 << ", " << p2 << ", " << p3 << endl; return 0; } 以上代码首先定义了一个包含10个元素的整数数组arr,然后使用random_device和mt19937初始化了随机数生成器gen,并使用uniform_int_distribution定义了一个整数分布dist,范围为0到9(数组的索引范围)。接下来,通过dist(gen)从数组arr中随机选取三个数,分别赋给p1、p2和p3。最后,打印选出的三个数。 ### 回答3: 下面是用C++写的通过PickPivot随机从数组选出个数的代码: ```cpp #include <iostream> #include <ctime> #include <cstdlib> int PickPivot(int arr[], int size) { srand(time(0)); // 设置随机数种子为当前时间 // 生成一个0到size-1的随机索引 int index = rand() % size; return arr[index]; } int main() { int size = 10; int arr[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 使用PickPivot函数从数组选出个数 int pivot1 = PickPivot(arr, size); int pivot2 = PickPivot(arr, size); int pivot3 = PickPivot(arr, size); std::cout << "选出的三个数为:" << pivot1 << ", " << pivot2 << ", " << pivot3 << std::endl; return 0; } ``` 上述代码首先定义了一个函数`PickPivot`,它接受一个整型数组数组大小作为参数。函数内部使用`srand`函数设置随机数种子为当前时间,并使用`rand`函数生成一个0到`size-1`的随机索引。然后根据随机索引从数组选出个数作为结果返回。 在`main`函数中,我们定义了一个大小为10的整型数组,并初始化了数组元素。然后使用`PickPivot`函数三次,分别选出了三个随机数`pivot1`、`pivot2`和`pivot3`。最后将结果输出到控制台。 这样,我们就通过PickPivot函数随机从数组选出了三个数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值