如何从整数数组中删除所有出现的数字? [英] How do I delete all the occurrences of a number from an integer array?

查看:84
本文介绍了如何从整数数组中删除所有出现的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入3个元素{1,1,2}。我的输出为{1,2}。它只删除元素一次。鉴于,我想删除所有事件。我该怎么办。



我尝试了什么:



<预>#包括< stdio.h中> 
#define max 100
int main()
{
int pos,a [max],n,i,num,count = 0;
printf(输入元素数量:);
scanf(%d,& n);
printf(输入数组的元素:);
for(i = 0; i< n; i ++)
scanf(%d,& a [i]);
printf(输入您要删除的号码:);
scanf(%d,& num);
for(i = 0; i< n; i ++)
if(a [i] == num){
pos = i;
count ++;
for(i = pos; i< n; i ++)
a [i] = a [i + 1];
}
//两者都有效。但两者都只删除一次。
/ * for(i = 0; i< n; i ++){
if(a [i] == num){
a [i] = a [i + 1];
count ++;
}
}
* /
printf(新数组是:);
for(i = 0; i< n-count; i ++)
printf(\ n%d,a [i]);
printf(\ n);
}
~

解决方案

首先使用两个数组索引,最初都设置为0: inp outp

inp 是你从中得到一个值,然后每次循环上升一个。

outp 是你复制值的地方,而不是每次循环上升。



循环遍历整个数组,每次加一个 inp

  for (inp =  0  ; inp< n; inp ++)



在循环中,将当前元素与要删除的valeu进行比较。

如果是同样,什么也不做!

否则,将其复制到输出中,然后递增 outp

 data [outp ++] = data [inp]; 

循环后,剩余的元素数量为 outp

这样可行,因为你了重新删除项目:所以你要复制你想要保留在同一阵列的较短版本中的项目!



哎呀,我甚至会给你的代码:

  for (inp =  0 < /跨度>; inp< N; inp ++)
{
if (data [inp]!= removeThisValue)
{
data [outp ++] = data [INP];
}
}
for int i = 0 ; i< outp; i ++)
{
printf( %d \ n,data [i]);
}

想一想,看看你能不知道它的作用。


引用:

它只删除元素一次。然而,我想要删除所有的事件。



算法错误,问题是当你检测到匹配并移动剩余的值时,你不会检查被替换的值比赛。

-----

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


If I give an input of 3 elements {1,1,2}. I'm getting output as {1,2}. It is deleting the element only once. Whereas, I want to delete all the occurrences. How do I proceed.

What I have tried:

<pre>#include<stdio.h>
#define max 100
int main()
{
        int pos,a[max],n,i,num,count=0;
        printf("Enter the number of elements : ");
        scanf("%d",&n);
        printf("Enter elements of the array : ");
        for(i=0;i<n;i++)
                scanf("%d",&a[i]);
        printf("Enter the number you want to delete : ");
        scanf("%d",&num);
        for(i=0;i<n;i++)
                if(a[i] == num){
                        pos = i;
                        count++;
                for(i=pos;i<n;i++)
                        a[i] = a[i+1];
                }
//Both works. But both deletes only once. 
/*      for(i=0;i<n;i++){
                if(a[i] == num){
                        a[i] = a[i+1];
                        count ++;
                }
        }
*/
        printf("The new array is : ");
        for(i=0;i<n-count;i++)
                printf("\n%d",a[i]);
        printf("\n");
}
~          

解决方案

Start by using two array indexes, both initially set at 0: inp and outp.
inp is where you get a value from, and goes up by one each time round the loop.
outp is where you copy values to, and does not go up each time round the loop.

The loop through the whole array, adding one to inp each time:

for (inp = 0; inp < n; inp++)


Inside the loop, compare the current element with the valeu you want to remove.
If it's the same, do nothing at all!
Otherwise, copy it to the output, and increment outp

data[outp++] = data[inp];

After the loop, the number of elements left is in outp
This works because you are removing items: so you are copying the items you want to keep into place in a "shorter version" of the same array!

Heck, I'll even give you the code:

for (inp = 0; inp < n; inp++)
    {
    if (data[inp] != removeThisValue)
        {
        data[outp++] = data[inp];
        }
    }
for (int i = 0; i < outp; i++)
    {
    printf("%d\n", data[i]);
    }

Have a think about it, and see if you can work out exactly what it does.


Quote:

It is deleting the element only once. Whereas, I want to delete all the occurrences.


The algorithm is wrong, the problem is that when you detect a match, and move remaining values, you do not check the value that replaced the match.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何从整数数组中删除所有出现的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆