我编写了以下程序来删除用户输入的数组元素。
#include <stdio.h>
#include <conio.h>
void main() {
int j, i, a[100], n, key, l;
clrscr();
printf("Enter the number of elements:");
scanf("%d", &n);
printf("\nEnter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nEnter the element to delete:");
scanf("%d", &key);
l = n; //Length of the array
for (i = 0; i < l; i++) {
if (a[i] == key) {
for (j = i; j < l; j++)
a[j] = a[j + 1];
l--; //Decreasing the length of the array
}
}
printf("\nThe new array is \n");
for (i = 0; i < l; i++)
printf("%d ", a[i]);
getch();
}
它适用于大多数输入但是当输入类似于:1 2 2 3 5
(此处2
连续重复)并且要删除的元素是2
时,输出为{{ 1}}。
如何修改程序,以便删除所输入元素的所有实例?
答案 0 :(得分:4)
在l--把我 - 也如下所示
之后if(a[i]==key)
{
for(j=i;j<l;j++)
a[j]=a[j+1];
l--; //Decreasing the length of the array
i--; //check again from same index i
}
答案 1 :(得分:2)
其他海报给了你2个解决方案......我认为理解为什么会发生这种情况也很好:)。
我们以您的示例1, 2, 2, 3, 5
为例,逐行遵循代码
i = 0; /* first time through the loop; i "points" to 1 */
if (a[i] == 2) ... /* nope; next loop */
i = 1;
if (a[1] == 2) ... /* yes! let's go inside the if */
/* move all elements back
** and "decrease" array length */
/* array is now 1, 2, 3, 5 */
/* next loop */
i = 2;
if (a[i] == 2) ... /* nope; OH! Wait ...
** a[1] is the new 2 and it wasn't checked */
答案 2 :(得分:1)
将“if”改为“while”:
for(i=0;i<l;i++) { while (i<l && a[i]==key) { for(j=i;j<l;j++) a[j]=a[j+1]; l--; //Decreasing the length of the array } }
答案 3 :(得分:1)
如果您不关心数组中元素的顺序,可以将数组的最后一个元素移动到新形成的间隙中(巧妙地将数组的长度减少一个)。这比分解元素要高效得多:在计算机科学术语中,这使得删除元素O(1)而不是O(N)。
a[i] = a[--l];
如果您的i索引在数组上循环,您将需要再次循环遍历此元素:
a[i--] = a[--l];
例如,要从长度为'l'的数组中删除所有元素'3':
for (i = 0; i < l; ++i) {
if (a[i] == 3) {
a[i--] = a[--l];
}
}
如果你做关心数组中元素的顺序,那么使用memmove而不是手动移动元素是最有效的。它被设计用于源和目标内存重叠的地方。
memmove(a + i, a + i + 1, sizeof(a[0]) * (l - i - 1));
答案 4 :(得分:0)
使用新阵列。
int array[l];
int k=0;
for(i=0;i<l;i++)
{
if(a[i]!=key)
{
array[k]=a[i];
k++;
}
}
答案 5 :(得分:0)
#include<stdio.h>
int main(){
int size;
int array[20];
int delete_pos;
int i;
printf("Enter the Size of the Array :");
scanf("%d",&size);
for(i=0;i<=size-1;i++){ //no of elements taken are 1 less than size of the array asked.
printf("\nEnter the element[%d] :",i+1);
scanf("%d",&array[i]);
}
printf("\nEnter the Position of the array to be deleted :");
scanf("%d",&delete_pos);
for(i=delete_pos-1;i<=size;i++){ //every element of the array is replaced by array on next position.
array[i]=array[i+1];}
size=size-1; // Reducing the size of the array as one element is deleted.
printf("Your new array is \n");
for(i=0;i<=size-1;i++){ //printing the entire new array.
printf("%d ",array[i]);
}
printf("\n\n");
return 0;
}
答案 6 :(得分:0)
您的带有2个嵌套for
循环的方法太复杂了。您可以简单地使用索引i
扫描数组,并复制具有不同索引key
的所有与len
不同的元素。所得的数组长度为len
的最终值。
这是修改后的版本:
#include <stdio.h>
#include <conio.h>
int main(void) {
int a[100];
int i, n, key, len;
clrscr();
printf("Enter the number of elements: ");
if (scanf("%d", &n) != 1) {
printf("invalid input\n");
return 1;
}
if (n < 0 || n > 100) {
printf("invalid number of elements\n");
return 1;
}
printf("\nEnter the elements:\n");
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
printf("\nEnter the element to delete: ");
if (scanf("%d", &key) != 1) {
printf("invalid input\n");
return 1;
}
for (i = len = 0; i < n; i++) {
if (a[i] != key)
a[len++] = a[i];
}
printf("\nThe new array is:\n");
for (i = 0; i < len; i++)
printf("%d ", a[i]);
printf("\n");
getch();
return 0;
}
注意:
main
(不带参数)的原型为int main(void)
,返回0
获得成功被认为是很好的样式。
始终测试scanf()
的返回值。这样可以防止许多错误和无效输入的不确定行为。当输入只是无效的时候,它还节省了很多时间在错误的地方查找。
避免在许多固定螺距字体中为变量l
命名,因为它看起来太接近1
。
总是以换行符终止程序输出。