我正在解决代码中的this问题,并且我用C++编写了代码。这是快速的(但坏)解决方案:C++程序在不同的机器上产生不同的输出
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
int main()
{
int n,r,c,temp,len,i;
char str[100];
char rem;
string res;
cin >> n;
while(n--)
{
r = c = -1;
res = "";
scanf("%s",str);
sscanf(str, "R%dC%d",&r,&c);
if(r != -1 && c != -1)
{
/* RC type */
temp = c;
if(c%26 == 0)
temp--;
while(temp)
{
rem = 'A' + (temp%26 - 1);
res = res + rem;
temp = temp/26;
}
if(c%26 == 0)
res.at(0) = res.at(0) + 1;
reverse(res.begin(), res.end());
cout << res << r << endl;
}
else
{
/* normal type */
len = strlen(str);
r = 0;
c = 0;
temp = 0;
for(i=len-1;i>=0;i--)
{
if(str[i] >= '0' && str[i] <= '9')
{
r = r + pow(10,len-i-1) * (str[i] - '0');
}
else
{
c = c + pow(26,temp)*(str[i] - 'A' + 1);
temp++;
}
}
cout << "R" << r << "C" << c << endl;
}
}
return 0;
}
如果这是输入:
2
R23C55
BC23
我的Linux 64位GCC给出了这样的输出:
BC23
R23C55
但网上法官给出这样的输出:
BC23
R23C54
我已经使用了正确的括号,没有无限增量/减量运算符,以确保在两台机器上的事物完全相同的顺序od评估,但仍然存在导致未定义评估的问题。任何人都可以请帮助什么声明有未定义的行为。 AFAIK,解决方案没有这样的说法。请帮忙。
编辑 我用ceil()
的pow()
各地,并通过了测试案例。虽然,我现在很害怕。我现在担心如何确定从pow()返回的值,因为there is a good reason of not implementing pow to return int type.
它不是'C/C++',它只是'C++'。 – pzaenger
你应该做一些调试。 –
@OliverCharlesworth:过去1个小时我一直在挠头,但无论是我忽略了某些事情还是仅仅是错误... –