usaco Palindromic Squares

本文介绍了一个编程问题,即在给定的数制下找出其平方为回文数的所有整数,并提供了两种不同的C++实现方案。

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

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

刚开始根本就没看清题,只考虑了10进制及以内的,后来发现是一直到20进制,之前在写时还在想怎么把数组和它的大小都传过来,真的好蠢!

改过后的代码(参考了一下网上的):

/*
ID:
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<fstream>

using namespace std;
int N,l,L,x[50],y[50];
//用此函数转化进制
void trans(int n){
    int m;
    m=n;
    l=0;
    while(m){x[++l]=m%N;m/=N;}
    m=n*n;
    L=0;
    while(m){y[++L]=m%N;m/=N;}
}
//用此函数判断是否为回文
int judge(){
    for(int i=1;i<L/2+1;i++){
        if(y[i]!=y[L-i+1])return 0;
    }
    return 1;
}

int main(){
    freopen("palsquare.in","r",stdin);
    freopen("palsquare.out","w",stdout);
    cin>>N;
    //对于10进制及以内进制的数,eg.x[4]=4;c[4]=4;(不变)
    //对于10进制以上的数,eg.x[4]=10;c[10]=A,可正确表示
    char c[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
    for(int i=1;i<=300;i++){
        trans(i);
        if(judge()){
            while(l){
                cout<<c[x[l--]];
            }
            cout<<" ";
            while(L){
                cout<<c[y[L--]];
            }
            cout<<endl;
        }
    }
    return 0;
}
之前的错代码(条理很不清):

/*
ID:
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<fstream>

using namespace std;
int N;

int judge(int n){
    int t=n*n;
    int s[100],str[100];
    int rem,i=0;
    //把t转化成N进制,并存到数组s中
    do{
        rem=t%N;
        t/=N;
        s[i++]=rem;
    }while(t!=0);
    int flag=1;
    for(int j=0;j<i/2;j++){
        if(s[j]!=s[i-j-1]){flag=0;break;}
    }
    int b,k=0;
    if(flag){
        //如果n方是一个回文那么把n转化成N进制来输出;
        do{
        b=n%N;
        n/=N;
        str[k++]=b;
        }while(n!=0);
        while(k){
            k--;
            cout<<str[k];
        }
        cout<<" ";
        while(i){
            i--;
            cout<<s[i];
        }
        cout<<endl;
    }
}

int main(){
    freopen("palsquare.in 

","r",stdin);
    freopen("palsquare.out","w",stdout);
    cin>>N;
    int rem,j=0;
    for(int i=1;i<=300;i++){
        judge(i);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值