英文:
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 <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;
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论