频道栏目
首页 > 资讯 > 其他 > 正文

usaco Palindromic Squares

17-10-07        来源:[db:作者]  
收藏   我要投稿

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
#include

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>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<之前的错代码(条理很不清):

 

 

/*
ID:
PROG: palsquare
LANG: C++
*/
#include
#include

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>N;
    int rem,j=0;
    for(int i=1;i<=300;i++){
        judge(i);
    }
    return 0;
}



 

相关TAG标签
上一篇:z-index的用法和注意点
下一篇:winhex处理磁盘格式化
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站