Palindromic Squares
Rob Kolstad
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.
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
/*
ID:qhn9992
PROG:palsquare
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int base;
char str[1000],show[1000];
char idx(int x)
{
if(x>=0&&x<=9) return x+'0';
else return x-10+'A';
}
void change(int x)
{
int e=base,cnt=0;
while(x)
{
int t=x%e;
x/=e;
str[cnt++]=idx(t);
}
str[cnt]=0;
}
void Show(int x)
{
int e=base,cnt=0;
while(x)
{
int t=x%e;
x/=e;
show[cnt++]=idx(t);
}
show[cnt]=0;
for(int i=0,j=cnt-1;i<j;i++,j--)
{
swap(show[i],show[j]);
}
}
bool ck()
{
int t=strlen(str);
for(int i=0,j=t-1;i<j;j--,i++)
{
if(str[i]!=str[j]) return false;
}
return true;
}
int main()
{
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
cin>>base;
for(int i=1;i<=300;i++)
{
change(i*i);
if(ck())
{
Show(i);
cout<<show<<" "<<str<<endl;
}
}
return 0;
}
分享到:
相关推荐
USACO题目Palindromic Squares(回文平方数)及代码解析 在计算机科学和信息学中,回文数(Palindromic Number)是一种数字,它从左向右念和从右向左念都一样。例如,12321是一个典型的回文数。给定一个进制B(2,...
python-3.回文平方数 Palindromic Squares——升级版la.py
8 [1.2] 回文平方数 Palindromic Squares 9 [1.2] 双重回文数 Dual Palindromes 10 [1.3] 混合牛奶 Mixing Milk 11 [1.3] 修理牛棚 Barn Repair 12 [1.3] 牛式 Prime Cryptarithm 13 [1.3] 虫洞 wormhole 14 [1.3] ...
1.2.4 "Palindromic Squares" 和 "Dual Palindromes" 强调了对回文数的理解和生成算法。 1.3.1 "Mixing Milk" 和 "Barn Repair" 可能涉及到更复杂的数学模型和动态规划。 1.4.1 "Packing Rectangles" 可能需要理解二...
接着,1.2节重点是完整搜索,如"Milking Cows"中运用离散化技术,"Transformations"和"Name That Number"通过枚举解决,而"Palindromic Squares"和"Dual Palindromes"进一步强化了枚举法的应用。 1.3节围绕贪心算法...
7. **回文数检查**:"Palindromic Squares"涉及回文数的概念,即正读反读都相同的数字。我们需要列出所有可能的平方数,然后检查它们是否是回文数。对于大于10的基数,数字可以表示为A, B, C等字母。 8. **算法优化...
#### Palindromic Squares 题目要求找出所有的回文平方数。此类问题可以通过遍历所有可能的数字,并检查它们的平方是否为回文数来解决。为了提高效率,可以预先计算出一定范围内的所有回文数,并使用哈希表进行快速...
**1.2.4 Palindromic Squares** - **问题描述**:寻找特定范围内的回文数平方。 - **算法思想**:可以使用回文数的性质结合数学公式来进行优化搜索。 **1.2.5 Dual Palindromes** - **问题描述**:题目要求寻找同时...
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长...
zoj 3661 Palindromic Substring.md
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. ...
PAT甲级 1024 Palindromic Number A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit...
题目名称:**Palindromic Substrings** 题目描述: 给定一个字符串 `s`,你需要找到并输出该字符串中所有长度为偶数的回文子串的数量。 输入: 输入包含一行,为一个字符串 `s`(只包含小写字母,长度不超过 `10^5...
c c语言_leetcode 0005_longest_palindromic_substring.zip
java入门 java_leetcode题解之005_Longest_Palindromic_Substring
c语言入门 c语言_leetcode题解之0516_longest_palindromic_subsequence
c语言入门 c语言_leetcode题解05-longest-palindromic-substring.c
js js_leetcode题解之5-longest-palindromic-substring.js
原创:PAT 1019 General Palindromic Number (20 分)A number that will be the same when