USACO section1.2 Dual Palindromes

本题与上一题Palindromic Squares类似,主要涉及到进制的转换以及回文的判断;

原题

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

分析

输入为两个数字N和S,要求得到前N个大于S且满足在2~10进制表示下至少有两次是回文,逐行输出;

提交代码:

/*
ID: Aaron Cai
PROG: dualpal
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("dualpal.in");
	int N,S; 
	fin >> N >> S;


	ofstream fout("dualpal.out");
	
	int count=0;
	while(count != N)
	{
		S++;
		int n=0;
		for (int i=2;i<=10;i++)
		{
			if(isPalin(toBase(S,i)))
			{
				n++;
			}
			if(n>=2)
			{
				count++;
				fout << S << endl;
				break;
			}
		}
	}

	return 0;
}

这里直接采用了上一题中实现的进制转换和回文判断函数。

提交结果:

TASK: dualpal
LANG: C++

Compiling...
Compile: OK

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

All tests OK.

官方参考答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.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 "" */
char*
numbconv(char *s, int n, int b)
{
    int len;

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

    /* 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';
    return s;
}

/* is number n a dual palindrome? */
int
isdualpal(int n)
{
    int i, j, npal;
    char s[40];

    npal = 0;
    for(i=2; i<=10; i++)
	if(ispal(numbconv(s, n, i)))
	    npal++;

    return npal >= 2;
}

void
main(void)
{
    FILE *fin, *fout;
    int n, s;

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

    fscanf(fin, "%d %d", &n, &s);

    for(s++; n>0; s++) {
	if(isdualpal(s)) {
	    fprintf(fout, "%d\n", s);
	    n--;
	}
    }

    exit(0);
}

THE END


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值