USACO section 1.2 Palindromic Squares

本文介绍了一种算法,用于找出特定进制下平方后形成回文数的所有整数,并提供了C++实现代码及评测结果。

原文

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.

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

分析

本题内容非常简单,给定一个进制(2进制到20进制)之间,要求算出300(10进制)以内所有求平方结果在给定进制下是回文的数字,以给定的进制输出。例如给定10进制的话,得到的结果是
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进制下121表示 1*10^2+2*10^1+1*10^0,其他进制同理。

提交代码

/*
ID: Aaron Cai
PROG: palsquare
LANG: C++
*/
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iostream>
using namespace std;


string toBase(int a,int base)
{
	int length = log(double(a))/log(double(base))+1;
	string out="";
	int b = a;
	for (int i=0;i!=length;i++)
	{
		int w = b / pow(base*1.0,double(length-i-1));
		b = b - w * pow(base*1.0,double(length-i-1));

		if(w<10)
			out += char('0'+w);
		else
		{
			out += char('A'+w-10);
		}
	}

	return out;
}

bool isPalin(string str)
{
	int half = str.length()/2;
	bool same = true;
	for (int i=0;i!=half;i++)
	{
		same = same && str[i] == str[str.length()-i-1];
		if(!same)
			return false;
	}
	if(same)
		return true;
}

int main()
{
	ifstream fin("palsquare.in");
	int base; 
	fin >> base;


	ofstream fout("palsquare.out");
	
	//cout << toBase(2,2)<<endl;
	
	for (int i=1;i<=300;i++)
	{
		if (isPalin(toBase(i*i,base)))
		{
			fout << toBase(i,base) << " " << toBase(i*i,base) << endl;
		}
	}
	return 0;
}


提交结果

TASK: palsquare
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 3496 KB]
   Test 2: TEST OK [0.003 secs, 3496 KB]
   Test 3: TEST OK [0.005 secs, 3496 KB]
   Test 4: TEST OK [0.005 secs, 3496 KB]
   Test 5: TEST OK [0.005 secs, 3496 KB]
   Test 6: TEST OK [0.005 secs, 3496 KB]
   Test 7: TEST OK [0.011 secs, 3496 KB]
   Test 8: TEST OK [0.008 secs, 3496 KB]

All tests OK.

官方参考答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>

/* is string s a palindrome? */
int
ispal(char *s)
{
    char *t;

    t = s+strlen(s)-1;
    for(t=s+strlen(s)-1; s<t; s++, t--)
    	if(*s != *t)
	    return 0;

    return 1;
}

/* put the base b representation of n into s: 0 is represented by "" */

void
numbconv(char *s, int n, int b)
{
    int len;

    if(n == 0) {
	strcpy(s, "");
	return;
    }

    /* figure out first n-1 digits */
    numbconv(s, n/b, b);

    /* add last digit */
    len = strlen(s);
    s[len] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n%b];
    s[len+1] = '\0';
}

void
main(void)
{
    char s[20];
    char t[20];
    int i, base;
    FILE *fin, *fout;

    fin = fopen("palsquare.in", "r");
    fout = fopen("palsquare.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d", &base);
    for(i=1; i <= 300; i++) {
	numbconv(s, i*i, base);
	if(ispal(s)) {
	    numbconv(t, i, base);
	    fprintf(fout, "%s %s\n", t, s);
	}
    }
    exit(0);
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值