书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

经典进制转换——USACO Palindromic Squares

Description

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.

Input

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

Output

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.

Sample Input

10

Sample Output

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696



这里简单说一下base的意思和用法,
base 10,就是十进制的意思

#include "iostream"
#include "string"
#include "cstring"
using namespace std;
int bit[20]={0};
char chbit[20]={'0'};
char forbit[20]={'0'};
int pos, TenBase, pos1;

void ChTen(int base, int num) //将十进制的数 转化成相应的进制 数组形式的
{
	int i;
	for(i=0; num>0; i++)
	{
		bit[i] = num%base;
		num /= base;		
	}
	pos = i-1; //统计字符串长度
}

bool Judge() //判断字符数组是否为回文
{
	pos1 = pos;
	for(int i=0; i<=pos; i++)
	{
		if(bit[i]<10)
			chbit[i] = bit[i]+48;
		else
			chbit[i] = bit[i] - 10 + 'A';
	}
	for(int i=0, j=pos1; i<=j; i++, j--)
		if(chbit[i]!=chbit[j])
			return false;
	return true;
}
int main()
{
	int base, TenNum;
	cin>>base;
	for(int m=1; m<=300; m++)
	{
		TenNum = m*m;
		ChTen(base, TenNum);
		if(Judge())
		{
			ChTen(base, m);
			for(int i=0; i<=pos; i++)
			{
				if(bit[i]<10)
					forbit[i] = bit[i]+48;
				else
					forbit[i] = bit[i] - 10 + 'A';
			}
			for(int i=pos; i>=0; i--)
				cout<<forbit[i];
			cout<<" ";
			for(int i=pos1; i>=0; i--)
				cout<<chbit[i];
			cout<<endl;
		}
	}
}

posted on   More study needed.  阅读(235)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示