reads n integers from the keyboard into the array, writes the function compactintegers, deletes all elements with a value of 0 in the array, and then moves the elements to the first end of the array. Note that the Compactintegers function needs to accept the array and its number of elements as arguments, and the function return value should be the number of new elements of the array after the delete operation executes. outputs the number of elements in the array after deletion, and sequentially prints the array elements. Sample input: (Input format Description: 5 is the number of input data, 3 4 0 0 2 is a space-separated 5 integers)
5
3 4 0) 0 2
Sample output: (Output Format Description: 3 is the number of non-0 data, 3 4 2 is a space-separated 3 non-zero integers)
3
3 4 2
Sample Input
7
0 0 7 0 0 9 0
Sample Output
2
7 9
Sample Input
3
0 0 0
Sample Output
0
Code:
#include <iostream>
#include <cstdio>
#define MAXN 10000
using namespace Std;
int A1[MAXN];
int count = 0;
int compactintegers (int b[], int Num1)
{
Count = 0;
for (int j = 0; J < Num1; J + +)
{
if (b[j]! = 0)
{
a1[count++] = B[j];
}
}
return count;
}
int main ()
{
int n;
CIN >> N;
for (int i = 0; i < n; i++)
{
CIN >> A1[i];
}
Cout<<compactintegers (a1, N) <<endl;
for (int k = 0; k < count; k++)
{
cout<<a1[k]<< "";
}
return 0;
}
Delete Array 0 elements---blue Bridge cup