【水题】USACO Palindromic Squares

[size=small]进入USACO要注册才能看题:[/size] [url]http://train.usaco.org/usacogate[/url]

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

[color=blue]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[/color] //结果中2个数都要以所给进制输出
[/size]

[size=medium]水题……但是由于细节问题没能一次A……无奈[/size]


/*
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值