我在Codeforces上解决了Quasi-Binary问题(无所谓),这是我的submission。这是我产生的代码:在不同编译器上产生不同输出的代码
#include <iostream>
#include <cmath>
using namespace std;
int quasi_binary(int num, int tens)
{
int res,digit;
if(num == 0)
{
return 0;
}
digit = num%10;
num = num/10;
res = quasi_binary(num, tens+1);
if(digit)
{
cout << 1;
return ((digit-1)*pow(10,tens)+res);
}
else
{
cout << 0;
return res;
}
}
int main()
{
int n,k=-1,temp,digit;
cin >> n;
//this loop calculates the value of k,as it needs to be printed first
temp=n;
while(temp)
{
digit = temp%10;
temp = temp/10;
if(digit>k)
k=digit;
}
cout << k << endl;
//print those k quasi-numbers
while(n)
{
n = quasi_binary(n,0);
cout << " ";
}
return 0;
}
我没有看到任何可能在不同编译器上产生未定义行为的语句。我在适当的地方使用了适当的括号,以避免模棱两可。仍然存在未定义的行为。任何人都可以请帮忙找到生成未定义行为的语句/指令。
输入
415
输出(在线评测) - 不正确
5
111 101 101 11 11 11 11 11 11 11 11 11
输出(我的64位PC与海湾合作委员会上) - 正确
5
111 101 101 101 1
'POW(10,十位)' - 如果你有整数指数不要使用'pow'。 [不保证pow会给你正确的结果](http://stackoverflow.com/questions/25678481/why-pown-2-return-24-when-n-5/25678721#25678721)。 – PaulMcKenzie
我不认为它与架构或编译器有关。测试数字是否大于零时,请使用完整的条件。即使用'if(num> 0)'而不是'if(num)'。不确定这是否是问题 – smac89
*我没有看到任何可以在不同编译器上产生未定义行为的语句* - 但是您确实有产生浮点值的语句('pow()'),因此您的程序不是保证产生相同的结果。 – PaulMcKenzie