USACO Training 1.3 Duel Palindromes

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.

比如12321反过来读还是12321(所以是回文数),但77778就不是。

Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

但是0在首位或者个位都不算,所以0220不是一个回文数。

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).

给一个10进制数下的21,他不是一个回文数。但是2进制下的21(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.

一共N航,每一行包含一个10进制数,至少两个进制们需要从小到大打印出来。

SAMPLE OUTPUT (file dualpal.out)

26
27
28

/*
ID: choiyin1
PROG: dualpal
LANG: C++
*/
#include <bits/stdc++.h> 
 
using namespace std;
char leter[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
void changetheBase(int x,int base,char out[60])
{
    stack<char> S;
    int i=0;
    while(x>0)
    {
        S.push(leter[x%base]);
        x/=base;
    }
    for(i=0;!S.empty();i++)
    {
        out[i]=S.top();
        S.pop();
    }
    out[i]=0;
} 
bool isPalindromic(char input[60])
{
    int temp[60];
    int len=strlen(input);
    for(int i=0;i<len;i++)
    {
        temp[len-i-1]=input[i];
    }
    for(int i=0;i<len;i++)
    {
        if(input[i]!=temp[i])
            return false;
    }
    return true;
}
bool islegal(int x)
{
    char out[60];
    int ans=0;
 
    for(int i=2;i<=10;i++)
    {
        changetheBase(x,i,out);
        if(isPalindromic(out))
        {
            ans++;
        }
        if(ans>=2)
            return true;
    }
    return false;
}
int main()
{
    int N,S,ans;
    freopen("dualpal.in","r",stdin);
    freopen("dualpal.out","w",stdout);
    scanf("%d%d",&N,&S);
    ans=0;
    for(int i=S+1;ans<N;i++)
    {
        if(islegal(i))
        {
            cout<<i<<endl;
            ans++;
        }
    } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值