首页 > 后端开发 > C++ > 正文

重新排列一个数组,以使连续一对元素的乘积之和最小,使用C++编写

PHPz
发布: 2023-08-26 10:57:06
转载
1139人浏览过

重新排列一个数组,以使连续一对元素的乘积之和最小,使用c++编写

我们有一个正整数类型的数组,假设是arr[],大小任意。任务是重新排列数组,使得当我们将一个元素与其相邻元素相乘,然后将所有结果元素相加时,返回最小的和。

让我们看看不同的输入输出情况:

输入 - int arr[] = {2, 5, 1, 7, 5, 0, 1, 0}

输出 - 重新排列数组以最小化和,即连续一对元素的乘积为:7 0 5 0 5 1 2 1

解释 - 我们有一个大小为8的整数数组。现在,我们将重新排列数组,即7 0 5 0 5 1 2 1。我们将检查是否返回最小和,即7 * 0 + 5 * 0 + 5 * 1 + 2 * 1 = 0 + 0 + 5 + 2 = 7。

立即学习C++免费学习笔记(深入)”;

输入 - int arr[] = {1, 3, 7, 2, 4, 3}

输出 - 重新排列数组以最小化和,即连续一对元素的乘积为:7 1 4 2 3 3

解释 - 我们有一个大小为6的整数数组。现在,我们将重新排列数组,即7 1 4 2 3 3。我们将检查是否返回最小和,即7 * 1 + 4 * 2 + 3 * 3 = 7 + 8 + 9 = 24。

下面程序中使用的方法如下:

  • 输入一个整数类型的数组并计算数组的大小。

  • 使用C++ STL的sort方法对数组进行排序,将数组和数组的大小传递给sort函数。

  • 声明一个整数变量,并将其设置为调用函数的返回值。

    AI图像编辑器
    AI图像编辑器

    使用文本提示编辑、变换和增强照片

    AI图像编辑器 46
    查看详情 AI图像编辑器
Rearrange_min_sum(arr, size)
  • Inside the function Rearrange_min_sum(arr, size)

    • Create a variable, let's say, ‘even’ and ‘odd’ type of type vector which stores integer variables.

    • Declare a variable as temp and total and initialise it with 0.

    • Start loop FOR from i to 0 till i less than size. Inside the loop, check IF i is less than size/2 then push arr[i] to odd vector ELSE, push arr[i] to even vector

    • Call the sort method by passing even.begin(), even.end() and greater<int>().

    • Start loop FOR from i to 0 till i less than even.size(). Inside the loop, set arr[temp++] to even[j], arr[temp++] to odd[j] and total to total + even[j] * odd[j]

    • Return total

  • Print the result.

  • Example

    #include <bits/stdc++.h>
    using namespace std;
    int Rearrange_min_sum(int arr[], int size){
       vector<int> even, odd;
       int temp = 0;
       int total = 0;
       for(int i = 0; i < size; i++){
          if (i < size/2){
             odd.push_back(arr[i]);
          }
          else{
             even.push_back(arr[i]);
          }
       }
       sort(even.begin(), even.end(), greater<int>());
       for(int j = 0; j < even.size(); j++){
          arr[temp++] = even[j];
          arr[temp++] = odd[j];
          total += even[j] * odd[j];
       }
       return total;
    }
    int main(){
       int arr[] = { 2, 5, 1, 7, 5, 0, 1, 0};
       int size = sizeof(arr)/sizeof(arr[0]);
       //sort an array
       sort(arr, arr + size);
       //call function
       int total = Rearrange_min_sum(arr, size);
       cout<<"Rearrangement of an array to minimize sum i.e. "<<total<<" of product of consecutive pair elements is: ";
       for(int i = 0; i < size; i++){
          cout << arr[i] << " ";
       }
       return 0;
    }
    登录后复制

    输出

    如果我们运行上面的代码,它将生成以下输出

    Rearrangement of an array to minimize sum i.e. 7 of product of consecutive pair elements is: 7 0 5 0 5 1 2 1
    登录后复制

    以上就是重新排列一个数组,以使连续一对元素的乘积之和最小,使用C++编写的详细内容,更多请关注php中文网其它相关文章!

    c++速学教程(入门到精通)
    c++速学教程(入门到精通)

    c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:tutorialspoint网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号