如何修复我的排列数问题?

huangapple go评论66阅读模式
英文:

how can I fix the problem to my permutation number problem?

问题

我需要创建一个函数,以确定数字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()
{
    int a, b;
    cin >> a >> b;
    cout << circular(a, b);
    return 0;
}

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

英文:

I need to create a function that determines the minimum steps of permutations to the right of number a so that it is equal to number b. My code gets this error: warning: non-void function does not return a value in all
control paths [-Wreturn-type]

#include &lt;iostream&gt;
#include &lt;cmath&gt;

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 &amp;&amp; contor &lt;= 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 &gt;&gt; a &gt;&gt; b;
    cout &lt;&lt; circular(a, b);
    return 0;
}

I tried creating two additional functions one to determine how long is the number so I can use it to determine the power to 10, and another one for the reverse number, so that I am able to permute the digits of the number.

答案1

得分: 1

如果circular()中的while循环从未进入,则控制流会在不返回值的情况下流出该函数。这就是未定义行为

英文:

If the while loop in circular() is never entered, then control flows out of the function without returning a value. That's undefined behaviour.

huangapple
  • 本文由 发表于 2023年5月28日 19:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定