编写函数CompactIntegers,删除数组中所有值为0的元素,其后元素向数组首端移动。注意,CompactIntegers函数需要接收数组及其元素个数作为参数,函数返回值应为删除操作执行后数组的新元素个数。
输入时首先读入数组长度,再依次读入每个元素。
将调用此函数后得到的数组和函数返回值输出。
样例输入
7
2 0 4 3 0 0 5
样例输出
2 4 3 5
4
#include <cstring>
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n, a, sum = 0;
cin >> n;
while(n--)
{
cin >> a;
if(a) {
cout << a << " ";
sum++;
}
}
cout << endl << sum << endl;
return 0 ;
}

本文介绍了一个名为CompactIntegers的函数,该函数用于从数组中移除所有值为0的元素,并将后续元素向前移动。通过示例输入输出展示了函数的工作原理,即输入数组长度及元素,输出处理后的数组及新元素数量。
2043

被折叠的 条评论
为什么被折叠?



