Title Link: http://acm.swust.edu.cn/problem/797/
Time limit (ms): $ Memory Limit (KB): 10000
Description
Palindromes is numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.
Given a number base B (2 <= b <= base), print all the integers n (1 <= n <= base) such that the S Quare of N is palindromic if expressed in base B; Also print the value of that palindromic square. Use the letters ' A ', ' B ', and so on to represent the digits, one, 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 the integers represented in base B. The first integer is the number whose square is palindromic; The second integer is the square itself.
Sample Input
Sample Output
1 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 69696 |
Hint
The main idea : An n represents the binary (n<=20), the output of the decimal number 300 or less of all this number of squares is the palindrome number of all the number and its square
problem-solving ideas: direct conversion judgment is, the interpretation of palindrome can use Palindrome number properties, in the conversion into the array of direct judgment, specific look at the codeThe code is as follows:
1#include <iostream>2 using namespacestd;3 intN;4 CharStr[] ="0123456789ABCDEFGHIJ";5 void out(intx) {6 inta[ -], I =0;7 while(x) {8A[++i] = x percentN;9X/=N;Ten } One for(; i; cout << str[a[i--]]); A } - voidFindintx) { - inta[ -], I =0, S = x*x, T; the while(s) { -A[++i] = s%N; -S/=N; - } +t =i; - for(; I&&a[i] = = A[t-i +1]; --i); { + if(!i) { A out(x), cout <<' '; at out(x*x), cout <<Endl; - } - } - } - intMain () - { inCIN >>N; - for(inti =1; I <= -; i++) to find (i); + return 0; -}
View Code
[Swust OJ 797]--palindromic Squares (palindrome number water problem)