USACO——Palindromic Squares 回文平方数
来源:互联网 发布:华为麦芒4 4g网络设置 编辑:程序博客网 时间:2024/06/09 14:48
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 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 69696
/*ID: youqihe1PROG: palsquareLANG: 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;}
同样的,还有这题
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)
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)
262728
也只要一个个判断即可/*ID: youqihe1PROG: dualpalLANG: 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;}
- USACO——Palindromic Squares 回文平方数
- USACO——Palindromic Squares 回文平方数
- USACO Palindromic Squares 回文平方数
- Usaco 1.2.4 回文平方数(Palindromic Squares)
- USACO 1.2.4 Palindromic Squares 回文平方数
- 【USACO题库】1.2.4 Palindromic Squares回文平方数
- 【USACO题库】1.2.4 Palindromic Squares回文平方数
- Palindromic Squares 回文平方数
- Palindromic Squares 回文平方数
- USACO1.2.4 Palindromic Squares(回文平方数)
- 10.9做题——USACO1.2/洛谷1206回文平方数(Palindromic Squares)
- USACO——回文平方数
- 【枚举判断】Palindromic Squares 回文平方数 (Usaco_Training 1.2)
- 【枚举】洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares
- USACO-Section1.2 Palindromic Squares [回文数][进制转换]
- usaco回文平方数
- #USACO TRAINING#回文平方数
- 【USACO TRAINING】回文平方数
- java中的静态变量、静态方法与静态代码块
- 小白学 c--2第一个程序 hello world
- 黑马程序员——Java之String类、基本数据类型对象包装类等
- EmguCV 在 Visual Studio 中的配置
- Music Player With Desktop LRC Scolling - Java
- USACO——Palindromic Squares 回文平方数
- 我是一滴千年泪,女娲造人所剩水。不羡九宵天长久,为缘人间几轮回。
- 平安科技移动开发二队技术周报(第七期)
- 斑斑驳驳。伤情时节,镌为别离的箫瑟
- 跳马问题
- LD_PRELOAD应用--基于libvirt审计(上)
- leetcode--Find Peak Element
- MySQL常用命令
- 挚爱身影渐隐。素绞无意世情狠