USACO——Palindromic Squares 回文平方数
2015-06-20 13:47
253 查看
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.
思路:做个判断回文的函数和进制转化的函数即可
同样的,还有这题
Dual Palindromes
Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither
leading nor trailing zeroes, so 0220 is not a palindrome.
The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).
Write a program that reads two numbers (expressed in base 10):
N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
也只要一个个判断即可
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. 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
思路:做个判断回文的函数和进制转化的函数即可
/* ID: youqihe1 PROG: palsquare LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include<string.h> using namespace std; int pal(char A[]) { int len=strlen(A),i,j; for(i=0;i<len/2;i++) if(A[i]!=A[len-i-1]) return 0; return 1; } void quare(int n,int b,char C[]) { int i=0; char B[25]; memset(B,'\0',sizeof(B)); while(n) { int k=n%b; if(k<10) B[i++]=k+'0'; else { if(k==10) B[i++]='A'; if(k==11) B[i++]='B'; if(k==12) B[i++]='C'; if(k==13) B[i++]='D'; if(k==14) B[i++]='E'; if(k==15) B[i++]='F'; if(k==16) B[i++]='G'; if(k==17) B[i++]='H'; if(k==18) B[i++]='I'; if(k==19) B[i++]='J'; } n/=b; } int t=0,j; for(j=i-1;j>=0;j--,t++) C[j]=B[t]; } int main() { FILE *fin = fopen ("palsquare.in", "r"); FILE *fout = fopen ("palsquare.out", "w"); int N; fscanf(fin,"%d",&N); int i,j; for(i=1;i<=300;i++) { char B[25],C[25]; memset(B,'\0',sizeof(B)); memset(C,'\0',sizeof(C)); quare(i*i,N,B); if(pal(B)) { quare(i,N,C); // printf("%d ",i); // printf("%s %s\n",C,B); fprintf(fout,"%s %s\n",C,B); } } return 0; }
同样的,还有这题
Dual Palindromes
Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither
leading nor trailing zeroes, so 0220 is not a palindrome.
The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).
Write a program that reads two numbers (expressed in base 10):
N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
PROGRAM NAME: dualpal
INPUT FORMAT
A single line with space separated integers N and S.SAMPLE INPUT (file dualpal.in)
3 25
OUTPUT FORMAT
N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.SAMPLE OUTPUT (file dualpal.out)
26 27 28
也只要一个个判断即可
/* ID: youqihe1 PROG: dualpal LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include<string.h> using namespace std; int pal(char A[]) { int len=strlen(A),i,j; for(i=0;i<len/2;i++) if(A[i]!=A[len-i-1]) return 0; return 1; } void quare(int n,int b,char C[]) { int i=0; char B[30]; memset(B,'\0',sizeof(B)); while(n) { int k=n%b; B[i++]=k+'0'; n/=b; } int t=0,j; for(j=i-1;j>=0;j--,t++) C[j]=B[t]; } int main() { FILE *fin = fopen ("dualpal.in", "r"); FILE *fout = fopen ("dualpal.out", "w"); int N,S; fscanf(fin,"%d %d",&N,&S); int i,j,k,num=0; for(i=S+1;num<N;i++) { int sum=0; for(k=2;k<=10;k++) { char B[30]; memset(B,'\0',sizeof(B)); quare(i,k,B); if(pal(B)) sum++; if(sum==2) { fprintf(fout,"%d\n",i); num++; break; } } } return 0; }
相关文章推荐
- 访问HTTP资源
- Uva - 11181 Probability|Given (条件概率)
- leetcode--Integer to Roman
- java中的静态变量、静态方法与静态代码块
- USACO——Name That Number 命名那个数字
- 百度地图定位功能的实现
- 产生随机数
- hdu 5261 蜀道难(deque 双端队列)
- POJ 2229 Sumsets
- HDOJ 5269 ZYB loves Xor I
- 围住浮动元素的三种方法
- 暂时不更新博客
- jxta-P2P学习
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
- CF E. Vanya and Brackets(添加一对括号使得表达式的值最大)
- Java串口通信详细解释
- java产生任意范围内不重复的随机数
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
- 黑马程序员--java学习笔记第七天
- VS中的活动debug和活动cpu