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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章