检查其中是否包含另一个数字的程序

Program that checks whether another number is contained within it

我最近才开始涉足编码,我 运行 遇到了一个我几天都无法解决的问题,也是我在网上能找到的最接近的问题是一个检查数字是否包含特定数字的程序,但我认为这并不适用于我的情况。问题是让用户输入两个正数并检查第二个数字的倒数是否包含在第一个数字中。例如,如果您输入 654321 和 345,它会说它包含它,因为 345 的反向是 543,而 654321 包含它。这是我一直在尝试的方法,但它是一场灾难。

P.S: 变量在整个程序中应该保持整数。

#include <iostream>
using namespace std;
bool check(int longer, int shorter)
{
    int i = 1;
    int rev=0;
    int digit;
    while (shorter > 0)
    {
        digit = shorter%10;
        rev = rev*10 + digit;
        shorter = shorter/10;
    }
    cout << rev << endl;
    bool win=0;
    int left = longer / 10;             //54321
    int right = longer % 10;            // 65432
    int middle = (longer /10)%10;       // 5432
    int middle1;
    int middle2;
    int trueorfalse = 0;
    while (left > 0 && right > 0 && middle1 > 0 && middle2 >0)
    {
        left = longer / 10;        //4321   //321
        right = longer % 10;       //6543   //654
        middle1 = middle%10;        //543
        middle2= middle/10;         //432
        if (rev == left || rev == right || rev == middle1 || rev == middle2 || rev == middle)
        {
            win = true;
        }
        else
        {
            win = false;
        }
    }
    return win;
}


int main ()
{
    int longer;
    int shorter;
    int winorno;
    cout << "Please enter two numbers, first of which is longer: ";
    cin >> longer;
    cin >> shorter;
    winorno = check(longer,shorter);
    if (winorno==true)
    {
        cout << "It works.";
    }
    else
    {
        cout << "It doesn't work.";
    }

    return 0;
}

The more you overthink the plumbing, the easier it is to stop up the drain. -- Scotty, Star Trek III.

如果将此任务分为两部分,这会变得容易得多:

  1. 反转整数中的数字。

  2. 在第二个整数中搜索第一部分计算出的逆整数

对于第一部分,假设 n 包含要反转的数字。

int modulo=1;
int reversed_n=0;

do
{
     reversed_n = reversed_n * 10 + (n % 10);
     modulo *= 10;
} while ( (n /= 10) != 0);

最终结果是,如果 n 包含 345,reversed_n 将以 543 结束,而 modulo 将是 1000。我们需要 modulo第二部分.

以这种方式构造循环的原因是有意的。如果原来的数字是0,我们希望reversed_n也是0,modulo也是10。

而现在,我们可以采用类似的方法来搜索第二个数字,称为search,是否包含reversed_n

for (;;)
{
    if ((search % modulo) == reversed_n)
    {
        std::cout << "Yes" << std::endl;
        return 0;
    }

    if (search < modulo)
        break;

    search /= 10;
}

std::cout << "No" << std::endl;

完整的程序:

#include <iostream>

int main()
{
    int search=654321;
    int n=345;

    int modulo=1;
    int reversed_n=0;

    do
    {
        reversed_n = reversed_n * 10 + (n % 10);
        modulo *= 10;
    } while ( (n /= 10) != 0);


    for (;;)
    {
        if ((search % modulo) == reversed_n)
        {
            std::cout << "Yes" << std::endl;
            return 0;
        }

        if (search < modulo)
            break;
        search /= 10;
    }

    std::cout << "No" << std::endl;
    return 0;
}
#include <iostream>
#include <cmath>
using namespace std;

int calculateNumLength(int num){
    int length = 0;
    while (num > 0) {
        num = num / 10;
        length++;
    }
    return length;
}

bool check(int longer, int shorter){

    int reversed = 0;
    int digit;
    int shortLength = calculateNumLength(shorter);
    int longLength = calculateNumLength(longer);
    int diffrence = longLength - shortLength;
    int possibleValues = diffrence + 1;
    int possibleNums[possibleValues];

    while ( shorter > 0 ) {
        digit = shorter % 10;
        rev = ( rev * 10 ) + digit;
        shorter = shorter / 10;
    }

    int backstrip = pow(10, diffrence);
    int frontstrip = pow(10, longLength-1);
    int arrayCounter = 0;
    while ( longer > 0 ){
        possibleNums[arrayCounter++] = longer/backstrip;
        if ( backstrip >= 10 ){
            backstrip = backstrip / 10;
        }else{
            break;
        }
        longer = longer % frontstrip;
        frontstrip = frontstrip / 10;
    }
    for (int i=0;i<possibleValues;i++){
         if (possibleNums[i] == rev ){
             return true;
         }
    }
    return false;
}

int main() {
    std::cout << check(654321,123) << std::endl;
    return 0;
}