简体   繁体   English

我如何缺少数组的最后一个元素

[英]How am i missing last element of an array

I am facing a problem with我面临一个问题

#include<iostream>
using namespace std;
bool check(int input)
{
    int count = 0;
    int temp;
    int val[] = { 2,0,2,1 };
    temp = input;
    while (temp != 0) {
        count++;
        temp /= 10;
    }
    int *arr = new int[count];
    for (int i = count; i >= 0; i--)
    {
        arr[i] = input % 10;
        input /= 10;
    }
    temp = 0;
    int res = count;
    for (int j = 0; j < 5; j++)
    {
        for (int k = 0; k < res; k++)
        {
            if (val[j] == arr[k])
            {
                cout << "CHECKING : " << arr[k] << endl;;
                j = j + 1;
                for (int l = k; l < (count - 1); l++)
                {
                    arr[l] = arr[l + 1];
                }
                res=res-1;
                temp++;
                k = 0;
                if (temp == 4)
                {
                    return true;
                }
            }
        }
    }
    cout << temp;
    return false;
}
int main()
{
    int input;
    cin >> input;
    if (check(input) == true)
    {
        cout <<endl << "YES!!" << endl;
    }
    else
    {
        cout <<endl <<"NO!!" << endl;
    }
}

this program I have to check if input number have 2021 number or not if input is 2002021 output should be yes or input is 2002024 output should be no because 1(2021) is missing now the thing is my program works fine logically but i dont know how my array last element is missing like if i put 200022021 = then the output will be no but if I am giving 200022012 it is saying yes i dont know how my last element of array is missing.这个程序我必须检查输入数字是否有 2021 数字如果输入是 2002021 输出应该是是或输入是 2002024 输出应该是没有因为现在缺少 1(2021) 事情是我的程序在逻辑上运行良好但我不知道我的数组最后一个元素是如何丢失的,就像我放 200022021 = 那么输出将是 no 但如果我给 200022012 它是说是的,我不知道我的数组的最后一个元素是如何丢失的。

You got the loop counter wrong:你弄错了循环计数器:

for (int i = count; i >= 0; i--)
{
    arr[i] = input % 10;
    input /= 10;
}

In the first iteration i == count and arr[count] is out-of-bounds.在第一次迭代中, i == countarr[count]越界。 Last iteration i == 1 (because when (i >= 0) == false you stop the loop) and you never assign to arr[0] .最后一次迭代i == 1 (因为当(i >= 0) == false您停止循环)并且您永远不会分配给arr[0]

You can call such mistakes history when you use either std::vector or std::array (for dynamic / fixed size, resp.) and use their reverse iterators ( rbegin and rend ) to iterate all elements in reverse.当您使用std::vectorstd::array (对于动态/固定大小,分别)并使用它们的反向迭代器( rbeginrend )反向迭代所有元素时,您可以调用此类错误历史记录。

Just because I can't be bothered looking for the bug:只是因为我懒得去寻找错误:

You can use the same method as you do for separating the single digits to examine the number in groups of four digits.您可以使用与分隔单个数字相同的方法来检查四位数字组中的数字。

x % 10 is the last digit; x % 10是最后一位; x % 100 is the last two digits; x % 100是最后两位数字; x % 1000 is the last three digits, and so on. x % 1000是最后三位数字,依此类推。
Add a division by 10 to "shift" the number:添加除以 10 以“移动”数字:

bool check(int input)
{
    while (input > 2020)
    {
        if (input % 10000 == 2021)
        {
            return true;
        }
        input /= 10;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM