我应该从现有数组中取出新数组(例如1 0 4 5 4 3 1),以便新数组包含已存在于现有数组中的数字及其出现次数。因此,新的看起来像这样:1 2 0 1 4 2 5 1 3 1(1出现2次,0出现1次... 3出现1次;它们出现在第一个数组中的次序应该也保存在新的一个);我知道如何计数。一个值出现在数组中的时间,但是我如何插入no.of外观? (C语言)如何通过计算值的出现次数并将其打印在该值旁边来创建新数组?
#include <stdio.h>
#define max 100
int main() {
int b, n, s, i, a[max], j, k;
printf("Enter the number of array elements:\n");
scanf("%d", &n);
if ((n > max) || (n <= 0)) exit();
printf("Enter the array:\n");
for (i = 0; i < n; i++)
scanf("%d", a[i]);
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (a[j] == a[i]) {
for (k = j; k < n; k++) {
a[k] = a[k + 1];
}}}}
//in the last 5 rows i've tried to compare elements, and if they are same, to increment the counter, and I've stopped here since I realised I don't know how to do that for every digit/integer that appears in array//
你尝试过这么远吗? – InternetAussie
由于您没有向我们展示您的进展,我将从顶部开始。按下电源按钮打开电脑... – DeiDei
让我们来看看。 “新阵列”需要是原始长度的两倍。每个奇数元素将保持一个等于原始元素的值。每个偶数元素将包含元素之前的值出现在原始数组中的次数。根据该描述生成算法应该是微不足道的。但有很多方法可以做到这一点。 – Peter