【发布时间】:2014-12-28 10:50:44
【问题描述】:
我有一个程序,我应该将 int 转换为 char 到它里面
但我不能使用 itoa() 因为网站的法官不支持它
所以我写了这个:
xt[i]= rmn+'0';
但我得到这个错误:
Runtime error: Illegal file open (/dev/tty)
我应该如何转换这个?
我的代码是这样的:(针对 USACO 的 palsquare 问题)
/*
ID: sa.13781
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("palsquare.out");
int tool(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
p++;
return p;
}
void prt(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
{
fout << xt[p];
p++;
}
}
void mabna(int a, char xt[], int mab)
{
int ex = 1, tavan = 0, rmn, n;
for (; ex <= a; ex *= mab)
tavan++;
for (int i = tavan - 1; a != 0; i--, a /= mab)
{
rmn = a % mab;
switch (rmn)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
xt[i] = rmn + '0';
break;
case 10:
xt[i] = 'A';
break;
case 11:
xt[i] = 'B';
break;
case 12:
xt[i] = 'C';
break;
case 13:
xt[i] = 'D';
break;
case 14:
xt[i] = 'E';
break;
case 15:
xt[i] = 'F';
break;
case 16:
xt[i] = 'G';
break;
case 17:
xt[i] = 'H';
break;
case 18:
xt[i] = 'I';
break;
case 19:
xt[i] = 'J';
break;
}
}
}
bool mirror(char *xt)//CORRECT
{
int p = 0;
int n = tool(xt);
for (int i = 0; i < (n / 2); i++)
if (xt[i] == xt[n - i - 1])
p++;
if (p == (n / 2))
return true;
return false;
}
void calc(int mab) //CORRECT
{
for (int i = 1; i <= 300; i++)
{
char p[10] = {0}, p2[10] = {0};
mabna(i * i, p2, mab);
if ( mirror(p2) == true )
{
mabna(i, p, mab);
prt(p);
fout << " ";
prt(p2);
fout << "\n";
}
}
}
int main()
{
ifstream fin("palsquare.in");
int mab;
fin >> mab;
calc(mab);
return 0;
}
【问题讨论】:
-
欢迎来到 Stack Overflow。如果您遵循此处的建议,您的问题将更成功地吸引答案:stackoverflow.com/help/mcve。就目前而言,它被否决了,因为它不符合该页面的指导方针,更普遍的是这里stackoverflow.com/help。
-
iota是一个非标准函数。我并不惊讶该网站不支持它。 -
无论如何,您收到的“运行时错误”意味着您的程序正在崩溃(并且网站没有告诉您)。了解如何调试您的代码,我们无法为您完成。