`

【水题】USACO Palindromic Squares

阅读更多
进入USACO要注册才能看题: http://train.usaco.org/usacogate

题目:【翻译版、是别处的网站】http://www.wzoi.org/usaco/12%5C501.asp

SAMPLE INPUT (file palsquare.in)
10
SAMPLE OUTPUT (file palsquare.out)
1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696
    //结果中2个数都要以所给进制输出


水题……但是由于细节问题没能一次A……无奈

/*
ID: 1006100071
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <iomanip>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

char num[] = {"0123456789ABCDEFGHIJKLMNO"};

inline bool ispalind (char *s, int len)   //判断s是否回文
{
	int i;
	for (i = 0; i < len / 2; i++)
		if (s[i] != s[len-1-i])
			return false;
	return true;
}
void my_itoa (int a, char *s, int b)//把a转成b进制放到s中,n为0时失效,根据题意没有0的情况!
{
	int k = 0, i;
	char p[30];
	while (a)
	{
		p[k++] = num[a % b];
		a /= b;
	}
	p[k] = 0;
	for (i = 0; i < k; i++)    //反转字符串
		s[i] = p[k-1-i];
	s[k] = 0;
	//cout << s << endl;
}
int main()
{
	/*freopen ("palsquare.in", "r", stdin);
	freopen ("palsquare.out", "w", stdout);*/
	char s1[30], s[30];
	int n, len, b;
	while (scanf ("%d", &b) != EOF)
	{
		for (n = 1; n <= 300; n++)
		{
			my_itoa (n * n, s, b);
			len = strlen(s);
			if (ispalind (s, len))
			{
				my_itoa (n, s1, b);
				printf ("%s %s\n", s1, s);
			}
		}
	}
	return 0;
}
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics