I was solving this problem on codeforces, and I wrote the code in C++. This is the quick(but bad) solution :
#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;
}
If this is the input :
2
R23C55
BC23
my Linux 64-bit gcc gives this output :
BC23
R23C55
But the online judge is giving this output :
BC23
R23C54
I have used proper brackets, no indefinite increment/decrement operator to ensure exact same order od evaluation of things on both the machines, but still something is there that is resulting in undefined evaluation. Can anyone please help what statement has undefined behaviour. AFAIK, the solution has no such statement. Please help.
EDIT
I used ceil()
around the pow()
and passed the test case. Although, I am scared now. I am now worried how to be sure of the value returned from pow(), since there is a good reason of not implementing pow to return int type.