1.2.4 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

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()
 6 {
 7     freopen("palsquare.in","r",stdin);
 8     freopen("palsquare.out","w",stdout);
 9     char m[111],c[111];
10     int n;
11     cin>>n;
12     long long p;
13     for(int i=1;i<=300;i++){
14         p=i*i;
15         int j=0;
16         int b=i;
17         int z=0;
18         while(b){
19             if(b%n<10) c[z++]=b%n+‘0‘;
20             else c[z++]=b%n-10+‘A‘;
21             b/=n;
22         }
23         while(p){
24             if(p%n<10) m[j++]=p%n+‘0‘;
25             else m[j++]=p%n-10+‘A‘;
26             p/=n;
27         }
28         int f=1;
29         for(int l=0,k=j-1;l<j;l++,k--){
30             if(m[l]!=m[k]){
31                 f=0;
32                 break;
33             }
34         }
35         if(f) {
36             for(int l=z-1;l>=0;l--) cout<<c[l];
37             cout<<" ";
38             for(int l=j-1;l>=0;l--) cout<<m[l];
39             cout<<endl;
40         }
41     }
42     return 0;
43 }
时间: 2024-10-14 12:33:48

1.2.4 Palindromic Squares的相关文章

USACO 1.2 Palindromic Squares

Palindromic SquaresRob 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

洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares

P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数.用’A’,’B’……表示10,11等等 输入输出格式 输入格式: 共

[Swust OJ 797]--Palindromic Squares(回文数水题)

题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000 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 1

洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares

题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数.用'A','B'--表示10,11等等 输入输出格式 输入格式: 共一行,一个单独的整数B(B用十进制表示). 输出格式: 每行两个B进制的符合要求的数字,第二个数是第一个数的平方,且第二个数是回文数. 输入输出样例 输入样例#1: 10 输出样例#1: 1 1 2 4 3

Palindromic Squares

链接 分析:求出b进制以后在判是否为回文 1 /* 2 ID:wanghan 3 PROB:palsquare 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 using namespace std; 11 int b; 12 bool judge(string s,int

【USACO 1.2】Palindromic Squares

进制转换,然后判断是否是回文 /******************************************* TASK: palsquare LANG: C++ Created Time: 2016年09月07日 星期三 21时18分46秒 *********************************/ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&

USACO Section1.2 Palindromic Squares 解题报告

palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 把1~300中,其平方在B进制下是回文数的数进行输出.每个数x输出一行,输出B进制下的x和x²,用空格隔开. 注意,10~

usaco Palindromic Squares

我会告诉你进制转换我都忘了,翻出了数字逻辑课本才想起来的. /* ID: modengd1 PROG: palsquare LANG: C++ */ #include <iostream> #include <stdio.h> #include <string.h> #include <stack> using namespace std; char leter[20]={'0','1','2','3','4','5','6','7','8','9','A'

Section 1.2.4 Palindromic Squares 大水

http://www.wzoi.org/usaco/12%5C501.asp patpat 学习了一个新的stl函数 eg. string poi = "poi"; reverse(poi.begin(), poi.end()); 就把poi变成了"iop" #include <bits/stdc++.h> using namespace std; int base; string trans(int temp){ int j, ch; string a