USACO——Palindromic Squares 回文平方数

这篇博客介绍了USACO中的两个问题,涉及回文数的概念。第一个问题是寻找在特定基数B(2<=B<=20)下,1到300之间的平方数形成的回文数。第二个问题要求找出在基10下,1到15之间的数N,其在基数S(0<S<10000)下形成的数是回文的。每个问题都详细说明了输入输出格式并提供了样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值