Ideas: In a one-dimensional array elements are inside the 1, so 0 random replace less than half the number
1: Get the size of the array through the keyboard.
2: Set a one-dimensional array elements are 1.
3: Setting a variable less than half of the array
4: () function to generate random numbers by rang, so that these random numbers generated as the array index, to change the number of subscripts 0
5: outputting only a one-dimensional array of 0, 0 and the number is half of the length of the array
int main()
{
int a = 0;
int b = 0;
int n = 0;
cout << "请输入数组大小:";
cin >> n;
b = n;
int chip[100];
int num = n - (n / 2 + 1);
for (int i = 0; i < n; i++) {
chip[i] = 1;
}
srand((int)time(NULL));
for (int i = 0; i < num; i++) {
int nu = (int)(rand() % (b - a)) + a;
chip[nu] = 0;
}
for (int i = 0; i < n; i++) {
cout << chip[i];
}
cout << endl;
Note: You also need to add header files
#include <stdlib.h>
#include <time.h>
Renderings: