USCAO Section 1.2 Palindromic Squares
来源:互联网 发布:不规则区域的填充算法 编辑:程序博客网 时间:2024/05/16 09:28
原题:
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.
题意:
输入一个进制n,求在十进制数1-300的平方数在n进制下为回文数的数并输出。
输出要求:原数字的n进制数和回文的n进制平方数。
题解:
水题,进制转换,要注意并没有要求原数字也在n进制下是回文数。
代码:
/*ID:newyear111PROG: palsquareLANG: C++*/#include <iostream>#include <fstream>#include <string>#include<algorithm>using namespace std;const int N=25;ifstream fin("palsquare.in");ofstream fout("palsquare.out");int n;char base[N];char s1[N*100],s2[N*100];//生成用于进制转换的对应表 void init(){ for(int i=0;i<10;i++){ base[i]=char(i+'0'); } for(int i=10;i<=20;i++){ base[i]=char('A'+i-10); }// cout<<base<<endl;}//回文数判定函数 bool isOK(char str[],int size){ for(int i=0;i<size/2;i++){ if(str[i]!=str[size-i-1]) return false; } return true;}//进制转换函数 int change(char s[],int t){ int r,num=0; while(t){ r=t%n; t/=n; s[num++]=base[r]; } s[num]=0; return num; }void solve(int x){ int xx=x*x; int sizeXX,sizeX; sizeX=change(s1,x); sizeXX=change(s2,xx); if(isOK(s2,sizeXX)){ for(int i=sizeX-1;i>=0;i--)//进制转换后,s1要逆序输出,不然原数字是反过来的 fout<<s1[i]; fout<<" "<<s2<<endl; }}int main(){ init(); fin>>n; for(int i=1;i<=300;i++){ solve(i); } fin.close(); fout.close(); return 0;}
阅读全文
0 0
- USCAO Section 1.2 Palindromic Squares
- Section 1.2 Palindromic Squares
- USACO Section 1.2 Palindromic Squares
- USACO Section 1.2 Palindromic Squares
- USACO section 1.2 Palindromic Squares
- [题目] Section 1.2 Palindromic Squares
- USACO Section 1.2 Palindromic Squares
- USACO-Section 1.2 Palindromic Squares[...]
- USACO Section 1.2 Palindromic Squares
- USCAO section1.2 Palindromic Squares
- USACA section 1.2.3 Palindromic Squares
- USACO Section 1.2.5 Palindromic Squares
- USACO Section 1.2.4 Palindromic Squares
- USACO-Section 1.2 Palindromic Squares【暴力枚举】
- USACO Training Section 1.2 Palindromic Squares 解题报告&AC代码
- USCAO section 1.2 Dual Palindromes
- USCAO Section 1.2 Dual Palindromes
- USACO 1.2-Palindromic Squares
- (贪心算法)HDU 2037 今年暑假不AC
- 罚款单,电信该被罚款4900亿人民币
- 通过串口打印GPS的信息------SF2820模块
- 策略模式与简单工厂模式
- mybatis关联查询2
- USCAO Section 1.2 Palindromic Squares
- HBase的三大组件
- 零基础学HTML+CSS [张熠编著][第三版]pdf
- redis操作命令
- linux下CJson使用
- 1007. 素数对猜想 (20)
- [Algorithm] 哈希
- 1012: Prime Number ?
- A+B Problem III -- ACM解决方法