使用指针算术删除重复的数组元素
这是从数组中删除重复元素的算法:
#include <stdio.h>
int main() {
int i, n = 5, j, k, arr[5] = {1, 2, 1, 2, 4};
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
for (k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
如何用指针算术编写?
我需要这样的东西:
int *p=arr;
while(p<arr+n){
p++;
}
This is algorithm for removing duplicate elements from array:
#include <stdio.h>
int main() {
int i, n = 5, j, k, arr[5] = {1, 2, 1, 2, 4};
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
for (k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
How could this be written with pointer arithmetic?
I need something like this:
int *p=arr;
while(p<arr+n){
p++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种非常昂贵的做事方式。一种更便宜的方法是使用链表或二叉树结构而不是数组,但如果您坚持使用数组,您的问题可以在此链接上得到解答:
https://engineeringstudentshub.wordpress.com/c-programs-for-u/array_programs/4-remove-duplicate-elements-in-an-array-using-c-program/
我希望帮助
this is a very expensive way to do things. A much cheaper way would be to use a linked list or binary tree structure instead of arrays, but if you insist on using arrays, your question is answered on this link:
https://engineeringstudentshub.wordpress.com/c-programs-for-u/array_programs/4-remove-duplicate-elements-in-an-array-using-c-program/
I hope that helps