Home > Old Blog Posts > USACO 1.2.4 Palindromic Squares

USACO 1.2.4 Palindromic Squares


初学者就当启蒙一下进位制的知识了……

注意10进制以上要用字母

代码:

/*
ID: dementr1
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin(“palsquare.in”);
ofstream fout(“palsquare.out”);
bool judge(int n,int base)
{
int tmp=n,t[40],num=0,i;
while(tmp>0)
{
t[num++]=tmp%base;
tmp=tmp/base;
}
for(i=0;i<=(int)num/2;i++) if(t[num-i-1]!=t[i]) return false;
return true;
}
void change(int n, int base)
{
int tmp=n,t[40],num=0,i;
while(tmp>0)
{
t[num++]=tmp%base;
tmp=tmp/base;
}
for(i=num-1;i>=0;i–)
{
if(0<=t[i]&&t[i]<10) fout<<t[i];
else fout<<char(‘A’+t[i]-10);
}
}
int main()
{
int base,i;
fin>>base;
for(i=1;i<=300;i++)
{
if(judge(i*i,base))
{
change(i,base);
fout<<” “;
change(i*i,base);
fout<<endl;
}
}
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: