首页 > 解决方案 > 结果不是我所期望的

问题描述

问题是删除所有最大的数字,例如: cin: 1 2 3 4 5 cout: 1 2 3 4

cin:5 1 2 3 5 cout:1 2 3

cin:5 5 1 2 5 cout:1 2

cin: 5 5 5 1 5 cout: 1

这就是问题所在:每当我的最大数字位于第一个和最后一个位置以及其他位置时,代码会打印出错误的结果,请查看以下示例以更好地理解:cin:5 5 1 2 5 预期 cout: 1 2 但它 cout: 5 1

cin: 5 5 1 5 5 预期 cout: 1
但它 cout: 5

我认为问题出现在删除功能中,但无论我重新检查多少次,我都无法弄清楚出了什么问题,如果有人能帮我解决这个问题,我会很高兴。对不起我的草率写作和糟糕的英语这是我的代码:

#include <iostream>

using namespace std;
void Insert(int a[] ,int n)
{
    for (int i=0; i<n; i++)
    {
        cout << "a[" << i << "]= ";
        cin >> a[i];
    }
}
void Delete(int a[], int n, int Biggestt)
{
    int BiggestLocation;
    for (int i=0; i<n-1; i++)
    {
        if (a[i]==Biggestt)
        {
            BiggestLocation=i;
        }
    }
    for (int i=BiggestLocation; i<n-1; i++)
    {
        a[i]=a[i+1];
    }
}
int Biggest(int a[],int n)
{
    int Biggesttt=a[0];
    for (int i=0; i<n; i++)
    {
        if (Biggesttt<a[i])
        {
            Biggesttt=a[i];
        }
    }
    return Biggesttt;
}
void PrintOut(int a[],int n)
{
    for (int i=0; i<n; i++)
    {
        cout << a[i] << " ";
    }
}
int main()
{
    int n,OriginalCount;
    int Count=0;
    cout << "Insert n: ";
    cin >>n;
    int a[100];
    Insert(a,n);
    int Biggestttt=Biggest(a,n);
    for (int i=0; i<n-1; i++)
    {
        if(a[i]==Biggestttt)
        {
            Count++;
        }
    }
    OriginalCount=Count;
    while(Count!=0)
    {
        {
            Delete(a,n,Biggestttt);
        }
        Count--;
    }
    if (a[n-1]==Biggestttt && OriginalCount==0)
    {
        PrintOut(a,n-1);
    }
    else if (a[n-1]!=Biggestttt && OriginalCount!=0)
    {
        PrintOut(a,n-OriginalCount);
    }
    else if (a[n-1]==Biggestttt && OriginalCount!=0)
    {
        PrintOut(a,n-OriginalCount-1);
    }

return 0;
}

标签: c++

解决方案


你不远了。您最大的问题与使用void function ()所有功能有关。通过void用作类型,您将失去return有效(和需要)信息的能力。

例如,随着从数组中删除每个匹配的void Delete(int a[], int n, int Biggestt)元素,保留的元素数量将发生变化——但在删除发生后,您无法返回数组中的最终元素数量。您可以将返回类型从to更改为并返回 updated ,也可以将其作为指针参数传递,这样当它在函数内更新时,它的更新值在返回时可以在调用函数中返回。a[]BiggesttvoidintnnDelete()

此外,您的逻辑main()非常混乱。您已经创建了函数来满足您的需求,因此main()应该相对干净并且只需要处理几个变量。您可以执行以下操作:

int main (void)
{
    int n, b,
        a[MAXINT];

    cout << "Insert n: ";
    if (!(cin >> n)) {      /* validate ALL user input */
        cerr << "(invalid conversion or user canceled)\n";
        return 1;
    }

    Insert (a, n);          /* insert all array values */
    cout << "original: ";   /* output the original */
    PrintOut (a, n);

    b = Biggest (a, n);     /* find the biggest number in the arry */
    Delete (a, &n, b);      /* delete all occurrences in array */

    cout << "big deleted: ";    /* output array with biggest removed */
    PrintOut (a, n);

    return 0;
}

注意:由于您的Delete()函数已留下void一个指向n作为参数传递的指针,因此n元素删除后的最终值将在调用函数(main()此处)中可用)

把它放在一起并对 中的逻辑进行调整Delete(),您可以执行以下操作:

#include <iostream>

using namespace std;

#define MAXINT 100

void Insert (int a[], int n)
{
    for (int i=0; i<n; i++)
    {
        cout << "a[" << i << "]= ";
        cin >> a[i];
    }
}

void Delete(int *a, int *n, int Biggestt)
{
    for (int i = 0; i < *n;)
    {
        if (*n > 1 && a[i] == Biggestt)
        {
            for (int j = i + 1; j < *n; j++)
                a[j-1] = a[j];
            (*n)--;     /* if biggest removed, decrement n */
        }
        else
            i++;        /* only advance if biggest not removed at index */
    }
}

int Biggest(int a[],int n)
{
    int Biggesttt=a[0];
    for (int i=1; i<n; i++)
    {
        if (Biggesttt<a[i])
        {
            Biggesttt=a[i];
        }
    }
    return Biggesttt;
}

void PrintOut(int a[],int n)
{
    for (int i=0; i<n; i++)
    {
        cout << " " << a[i];
    }
    cout << '\n';
}

int main (void)
{
    int n, b,
        a[MAXINT];

    cout << "Insert n: ";
    if (!(cin >> n)) {      /* validate ALL user input */
        cerr << "(invalid conversion or user canceled)\n";
        return 1;
    }

    Insert (a, n);          /* insert all array values */
    cout << "original: ";   /* output the original */
    PrintOut (a, n);

    b = Biggest (a, n);     /* find the biggest number in the arry */
    Delete (a, &n, b);      /* delete all occurrences in array */

    cout << "big deleted: ";    /* output array with biggest removed */
    PrintOut (a, n);

    return 0;
}

示例使用/输出

$ ./bin/remove_biggest
Insert n: 5
a[0]= 5
a[1]= 1
a[2]= 2
a[3]= 3
a[4]= 5
original:  5 1 2 3 5
*n: 3
*n: 3
*n: 3
big deleted:  1 2 3

$ ./bin/remove_biggest
Insert n: 4
a[0]= 5
a[1]= 5
a[2]= 1
a[3]= 5
original:  5 5 1 5
*n: 1
big deleted:  1

如果所有数字a[...]都相同怎么办?你必须能够处理这种情况。如果它们都是相同的数字,那么现在的逻辑会Delete()保留 1 个数字。您也可以选择将它们全部保留,因为没有Biggestt. 它们同时是最大的和最小的。你如何处理它取决于你。

$ ./bin/remove_biggest
Insert n: 4
a[0]= 5
a[1]= 5
a[2]= 5
a[3]= 5
original:  5 5 5 5
*n: 1
big deleted:  5

如果它们都是相同的大数字,我们将删除所有它们,留下 1,因为它也是最小值。

使用引用int& n而不是指针

为了回应您的评论和飞翔的建议,C++ 允许您传递对nin的引用Delete()而不是指针,以确保n在调用函数(main此处)中可以看到对的更改。问题的症结在于,当您简单地将参数传递给函数时,函数会收到一份副本,并且对函数内的变量所做的任何更改都会在返回时丢失。C++ 提供了一个引用(例如int& n),它本质上将别名传递给原始文件,并且对引用所做的任何更改都是对原始文件所做的更改。这是对传递变量地址的改进,因为它确实避免了取消引用指针。

使用参考,Delete()可以重写如下:

void Delete (int *a, int& n, int Biggestt)
{
    for (int i = 0; i < n;)
    {
        if (n > 1 && a[i] == Biggestt)
        {
            for (int j = i + 1; j < n; j++)
                a[j-1] = a[j];
            n--;        /* if biggest removed, decrement n */
        }
        else
            i++;        /* only advance if biggest not removed at index */
    }
}

调用Delete()inmain()将是:

Delete (a, n, b);       /* delete all occurrences in array */

而你已经摆脱了所谓的'*'标记:) (那个飞)


推荐阅读