c++ For循环算法

C++ For loop algorithm

本文关键字:算法 循环 For c++      更新时间:2023-10-16

我是c++的新手,我需要一些帮助,在我的doubleUp函数中的for循环算法。doubleUp函数的目的是,如果数组具有相同的连续值,则每个数字将被加倍。一个例子是,如果一个数组有元素4,4,4和4。通过使用doubleUp函数,新的数组将是8,8,8,8。再次感谢大家!: D

#include <iostream>
using namespace std;
void collectScores(double scores[], int SIZE);
void printScores(double scores[], int stopPlace);
void doubleUp(double scores[], int SIZE);
void collectScores(double scores[], int SIZE)
{
    for (int n = 0; n < SIZE; n++)
    {
        cout << "Please enter a value for element " <<  (n) << " : ";
        cin >> scores[n];
    }
    cout << endl;
}
void printScores(double scores[], int stopPlace)
{
    for (int n = 0; n < stopPlace; n++)
    {
        cout << "The value of element " << (n) << " : " << scores[n] << endl;
    }
}
void doubleUp(double scores[], int SIZE)
{
    for (int n = 0; n < SIZE; n++)
    {
        if (scores[n] == scores[n + 1])
        {
            scores[n] *= 2;
        }
    }
}
int main()
{
    double r;
    const int SIZE = 4;
    double scores[SIZE];
    collectScores(scores, SIZE);
    printScores(scores, SIZE);
    cout << endl;
    doubleUp(scores, SIZE);
    printScores(scores, SIZE);

    system("pause");
    return 0;
}

除了你在最后错过了一个边缘情况之外,还有一个更大的问题。由于您是在一个方向上向前看,对于双精度对象,这意味着您将错过对的每个岛的相邻相等数字的最后一个元素进行加倍。在下面的代码中,我添加了一个名为isDouble的状态变量来解决这个问题。

void doubleUp(double scores[], int SIZE)
{
    if (SIZE < 2) return;        // 0 or 1 element just return
    bool isDouble = false;
    for (int n = 1; n < SIZE; n++)
    {
        if (scores[n] == scores[n - 1])
        {
            isDouble = true;
            scores[n - 1] = 2*scores[n - 1];
        }
        else if (isDouble)
        {
            isDouble = false;
            scores[n - 1] = 2*scores[n - 1];
        }
    }
    // edge case: possibly double the last element in the array
    //            if it be identical to the second to last element
    if (isDouble) scores[SIZE-1] = 2*scores[SIZE-1];
}

正如人们指出的那样,代码中的错误是最后一个元素没有被加倍。这是因为对于数组中的最后一个元素,没有后续元素,这就是代码中"n+1"部分失败的地方。有很多方法可以解决这个问题。我使用了两个for循环——一个用于除最后两个元素以外的所有元素,另一个用于最后两个元素。

void doubleUp(double scores[], int SIZE)
{
    for (int n = 0; n < SIZE-2; n++)
    {
        if (scores[n] == scores[n + 1])
        {
            scores[n] *= 2;
        }
    }
    for (int n = SIZE-2; n <SIZE; n++)
    {
        if (scores[n] == scores[n + 1])
        {
            scores[n] *= 2;
            scores[n+1] *= 2;
        }
    }  
}

我认为你的doubleUp函数应该稍微修改一下:

void doubleUp(double scores[], int SIZE)
{
    for (int n = 0; n < SIZE; n++)
    {
        if (n < SIZE-2)
        {    
            if (scores[n] == scores[n + 1])
            {
                scores[n] *= 2;               
            }
        }
        else
        {
            if (scores[n] == scores[n + 1])
            {
                scores[n] *= 2;
                scores[n+1] *= 2;   
                n++;             
            }
        }
    }
}

添加的部分实际上允许数组中的最后一项被加倍,而原始代码不允许(数组中最后一项之后没有n+1索引)