c++ 我怎样才能把这个问题解决成我的排列数问题呢?

jhiyze9q  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(121)

我需要创建一个函数,它确定数字a右边的排列的最小步骤,以便它等于数字b。我的代码得到这个错误:警告:非void函数在所有控制路径中不返回值[-Wreturn-type]

#include <iostream>
#include <cmath>

using namespace std;

int nrCif(int a)
{
    int nr = 0;
    while(a != 0){
        nr ++;
        a /= 10;
    }
    return nr;
}

int inversa(int a)
{
    int o = 0;
    while(a != 0){
        o = o * 10 + a % 10;
        a /= 10;
    }
    return o;
}

int circular(int a, int b)
{
    int ca = a, contor = 0;
    while(ca != b && contor <= nrCif(a)){
        int aux = ca % 10;
        if(aux == 0){
            ca = ca / 10;
            aux = ca % 10;
            contor ++;
        }
        else
            {
                ca = aux * pow(10, nrCif(a)) + inversa(inversa(ca) / pow(10, nrCif(a)));
                contor ++;
            }
        if(contor != nrCif(a))
            return - 1;
        return contor;
    }
}

int main()
{

    ///1

    int a, b;
    cin >> a >> b;
    cout << circular(a, b);
    return 0;
}

我尝试创建两个额外的函数,一个用于确定数字的长度,以便我可以使用它来确定10的幂,另一个用于反向数字,以便我能够排列数字的数字。

3vpjnl9f

3vpjnl9f1#

如果从未进入circular()中的while循环,则控制流出函数而不返回值。undefined behaviour

相关问题