进制转换,num2str问题
/*
ID: alexyua2
PROG: palsquare
LANG: C++
*/
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
ifstream fin("palsquare.in");
ofstream fout("palsquare.out");
//ifstream fin("in.txt");
//ofstream fout("out.txt");
string num2str(int,int);
bool IsPalindromic(string);
int main()
{
int base;
fin>>base;
int i;
for(i=1;i<=300;i++)
{
string square = num2str(pow(i,2), base);
if(IsPalindromic(square))
fout<<num2str(i,base) <<' ' <<square <<endl;
}
}
string num2str(int num, int base)
{
/*
To transform one number in base 10
into a string in base given
*/
string result;
int quotient,remain;
while(1)
{
quotient = num / base;
remain = num % base;
num = quotient;
if(remain<10)
result.insert(result.begin(),'0'+remain);
else
result.insert(result.begin(),'A'+remain-10);
if(num == 0)
return result;
}
}
bool IsPalindromic(string square)
{
int i;
int len = square.length();
bool result = true;
for(i=0;i<len/2;i++)
{
if(square[i] != square[len-i-1])
result = false;
}
return result;
}
本文介绍了一个使用C++实现的程序,该程序能够将十进制数转换为指定进制的字符串形式,并判断该字符串是否为回文数。通过对平方数进行进制转换并检查其回文特性,程序输出了指定范围内所有符合条件的数字及其对应的回文平方数。
1263

被折叠的 条评论
为什么被折叠?



