"); -6ren">
gpt4 book ai didi

c - 如何删除数组中找到的元素并将数组元素向左移动?

转载 作者:行者123 更新时间:2023-12-02 05:21:53 25 4
gpt4 key购买 nike

int search(int a[]) {

int i,V,index;
printf("Enter the element (V),That you want to find:>");
scanf("%d",&V);

for (i=0;i<N;i++) {
if(a[i]==V) {
V=a[i];
index=i;
}
}
printf("%d is located in a[%d].",V,index
)

最佳答案

如果您不关心元素的顺序,您可以在 O(1) 时间内删除找到的元素。

// Find the element you're looking for.
int index = find(A, V);

// Stuff the last element into the index found.
A[index] = A[N-1];

// Reduce the total number of elements.
N--;

关于c - 如何删除数组中找到的元素并将数组元素向左移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com