我的删除有问题 [英] Something is wrong with my deletion

查看:57
本文介绍了我的删除有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,该程序假设要删除一个具有重复项的元素,到目前为止,它仅删除了两对。我需要帮助。

So this program is suppose to delete an element which has a duplicate and so far it only deleted only two pairs. I need help.

#include <stdio.h>

int main(void)
{
    int arr[11] = { 1, 1, 2, 3, 4, 5, 4, 7, 8, 9, 8 };
    int col[11];
    int i;
    int j;
    int k;
    int ctr = 0;

    for (i = 0; i < 11; i++)
    {
        for (j = i + 1; j < 10; j++)
        {
            if (arr[i] == arr[j])
            {
                i++;
            }
        }
        printf("%d", arr[i]);
    }

    getch();
    return 0;
}


推荐答案

像这样的修复

#include <stdio.h>

int main(void){
    int arr[] = { 1, 1, 2, 3, 4, 5, 4, 7, 8, 9, 8 };
    int col[sizeof arr/sizeof *arr] = { 0 };
    int len = sizeof arr/sizeof *arr;//11
    int i, j;

    for (i = 0; i < len-1; i++){
        for (j = i + 1; j < len; j++){
            if (arr[i] == arr[j]){
                col[j] = 1;
            }
        }
        if(col[i] == 0)
            printf("%d ", arr[i]);
    }
    puts("");
    return 0;
}

这篇关于我的删除有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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