如何在数组中的所有最小值之后放置任何值 X?

How do I put any value X after all the minimums in an array?

如果我输入 array ,首先代码会找到 minimums 然后我想在所有最小值之后放零。例如 given an array = 1,1,3,1,1 我们看到 1s 是最小值,所以结果应该是 = 1,0,1,0,3,1,0,1,0

代码

#include <pch.h>
#include <iostream>


int main()
{

    int min = 10000;
    int n;                                       
    std::cout << "Enter the number of elements (n): "; //no of elements in the 
    std::cin >> n;                                     //array

    int *array = new int[2 * n];
    
    std::cout << "Enter the elements" << std::endl;



    for (int i = 0; i < n; i++) {                      
        std::cin >> array[i];
        if (array[i] > min)
            min = array[i];
    }

    for (int i = 0; i < n; i++) {                

        if (array[i] == min) {                   // Not very clear about this
            for (int k = n; k > i; k--)          // part of the code, my teacher
                 array[k] = array[k - 1];        //explained it to me , but i 
             array[i + 1] = 0;               // didn't understand (from the      
             i++;                            // `for loop k` to be precise)
             n++;
        }
        std::cout << array[i] << ", 0";
    }
        
                                  
    
    return 0;
}

But my answer doen't put zeroes exactly after minimums

第一个问题是 min 的计算:< 而不是 >

如果您正在修改循环内的参数 in,则会出现另一个问题。这是相当危险的,暗示要非常小心。

另一个问题是它应该是 i++; n++; 而不是 i--,n--;

代码如下:

//  #include <pch.h>
#include <iostream>


int main()
{
    int min = 1000000;
    int n;                                       
    std::cout << "Enter the number of elements (n): "; //no of elements in the 
    std::cin >> n;                                     //array

    int *array = new int[2 * n];

    std::cout << "Enter the elements" << std::endl;

    for (int i = 0; i < n; i++) {                      
        std::cin >> array[i];
        if (array[i] < min)
            min = array[i];
    }

    for (int i = 0; i < n; i++) {                
        if (array[i] == min) {                   // Not very clear about this
            for (int k = n; k > i; k--)          // part of the code, my teacher
                 array[k] = array[k - 1];        //explained it to me , but i 
            array[i + 1] = 0;               // didn't understand (from the)      
            i++;
            n++;
        }
    }

    for (int i = 0; i < n; i++) {                
        std::cout << array[i] << " ";
    }
    std::cout << "\n";

    return 0;
}

你的代码有几个问题,首先你的 min 是错误的。我已经修复了您的代码,并对我所做的修复发表了评论。请看一看:

#include "stdafx.h"
#include <iostream>


int main()
{
    int min = 10000;
    bool found = 0;
    int n;
    std::cout << "Enter the number of elements (n): "; //no of elements in the 
    std::cin >> n;                                     //array

    int *array = new int[2 * n];

    std::cout << "Enter the elements" << std::endl;



    for (int i = 0; i < n; i++) {
        std::cin >> array[i];
        if (array[i] < min) //< instead of >
            min = array[i];
    }

    for (int i = 0; i < n; i++) {

        if (array[i] == min) 
        {                  
            for (int k = n; k > i; k--)
            {
                array[k] = array[k - 1];
            }
            array[i + 1] = 0;               
            i++; //increment i here because you don't want to consider 0 that you have just added above.                           
            n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
        }
    }

    //print the array separately 
    for (int i = 0; i < n; i++)
    {
        std::cout << array[i];
        if (i != n - 1)
        {
            std::cout << ",";
        }
    }

    return 0;
}