Usaco Training刷怪旅 第二层第四题:Palindromic Squares

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

PROGRAM NAME: palsquare

INPUT FORMAT

A single line with B, the base (specified in base 10).

SAMPLE INPUT (file palsquare.in)

10

OUTPUT FORMAT

Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!

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

 

这题如果翻译的比较奇葩,就去洛谷看一看翻译

就是在b进制下,300以内的数的平方如果是回文数就输出

这题两个重要点:

1.进制转换

2.判断回文

这两个可以结合到一个函数,最后输出再来一个函数

因为输出时需要有b进制,所以再来个函数转换

/*
ID:liusiyu3
TASK:palsquare
LANG:C++
*/
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
# define int long long
char a1[100];
char a[100];
int b;
int j=0;
char sum[100]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
void out(int a,int b) {
    int j=0;
    while(a){
        j++;
        a1[j]=a%b+'0';
        if(a1[j]>'9'){
       		a1[j]=a1[j]-'9'+'A'-1;
	    }
        a/=b;
    }
    for(int i=j;i>=1;i--){
		cout<<a1[i];
    }
    return ;
}
bool jz(int x,int n){
	j=0;
	memset(a,0,sizeof(a));
	while(x){
		j++;
		if (x%n>=10){
			a[j]=sum[x%n];
			//cout<<sum[x%n]<<" ";
		}else{
			a[j]=char(x%n+'0');
			//cout<<char(x%n+'0')<<"\n";
		}
		x/=n;
	}
	//cout<<j<<" ";
	for (int i=1;i<=j;i++){
		if (a[i]!=a[j-i+1]){
			return false;
		}
	}
	return true;
}
signed main(){
	freopen("palsquare.in","r",stdin);
	freopen("palsquare.out","w",stdout);
	scanf("%lld",&b);
	for (int i=1;i<=300;i++){
		if (jz(i*i,b)){
			//jz(i,b);
			out(i,b);
			printf(" ");
			out(i*i,b);
			printf("\n");
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值