1.2 Dual Palindromes

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且转换成其他进制 有两种以上是回文数,输出他。
/*
ID: cxq_xia1
PROG: dualpal
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int N,S,cntNumPal,cntLen;
char trans[50];
bool isPalsquare(char a[])
{
    for(int i=0;i<cntLen;i++)
    {
        if(i==0&&a[i]==0)
            return false;
        if(trans[i]!=trans[cntLen-1-i])
            return false;
    }
    return true;
}

int main()
{
    freopen("dualpal.in","r",stdin);
    freopen("dualpal.out","w",stdout);

    cin >> N >> S;
    int cnt=1;
    for(int i=1;cnt<=N;i++)
    {
        cntNumPal=0;
        for(int base=2;base<=10;base++)
        {
            int tmp=S+i;
            memset(trans,0,sizeof(trans));
            cntLen=0;
            while(tmp!=0)
            {
                trans[cntLen++]=tmp%base;
                tmp/=base;
            }
            if(isPalsquare(trans))
            {
                cntNumPal++;
            }

            if(cntNumPal>=2)
                break;
        }
        if(cntNumPal>=2)
        {
            cout << S+i <<endl;
            cnt++;
            continue;
        }
    }

    return 0;
}

  

时间: 2024-10-04 19:25:30

1.2 Dual Palindromes的相关文章

洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数. 编

洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes

题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做"回文数".例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数. 编一个程序,从文件读入两个十进制数N (1 <= N <= 15)S (0 < S < 10000)然后找出前N个满足大于S且在两种或两种以上进制(二进制至十进制)上是

USACO 1.2 Dual Palindromes (回文,进制转换)

废话不哆嗦,贴代码: /* ID:twd30651 PROG:dualpal LANG:C++ */ #include<iostream> #include<fstream> #include<stdlib.h> #include<string.h> using namespace std; char s[100]; int N,S; void gs(int num,int BASE) { int index=0; while(num/BASE) { s[i

usaco Dual Palindromes

/* ID: modengd1 PROG: dualpal LANG: C++ */ #include <iostream> #include <stack> #include <stdio.h> #include <string.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'

USACO Section1.2 Dual Palindromes 解题报告

dualpal解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给出N和S,找出大于S的前N个双回文数. 双回文数定义:在二进制至十进制中的两种(或两种以上)进制下是回文数.[数据范围] 1

Section 1.2.5 Dual Palindromes 水

http://www.wzoi.org/usaco/11%5C302.asp pat 没太水了没什么好说的. 然而感觉如果不计代价的话,先把string reverse再比较是否相同来判断回文,好方便pat #include <bits/stdc++.h> using namespace std; bool gao(int base, int n){ int i, j; string ans = ""; while(n){ ans += n % base + '0'; n

Dual Palindromes

链接 分析:直接暴力求解,傻逼题 1 /* 2 ID:wanghan 3 PROB:dualpal 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 using namespace std; 11 string Rev(int num,int b){ 12 string t=

USACO Chapter 1 Section 1.2

不知道为什么感觉1.2比1.1反而简单不少 1 /* 2 ID:xiekeyi1 3 PROG:milk2 4 LANG:C++ 5 */ 6 #include<bits/stdc++.h> 7 using namespace std ; 8 const int MAXN = 5010; 9 struct point 10 { 11 int begin , end ; 12 } a[MAXN] ; 13 14 15 bool cmp( struct point a , struct point

USACO Chapter 1 解题总结

1.1.1 Your Ride Is Here 基本字符串操作,无压力. 1.1.2 Greedy Gift Givers 基础模拟题,弄明白题意,不怕麻烦,就OK了. 1.1.3 Friday the Thirteenth 自己的做法:三维数组代表年月日,400的数据范围不大,模拟走一下时间的流逝过程即可.时间复杂度O(N*12*31),多好玩. 官方标程好像用到了一个神奇的公式,好像是什么蔡勒公式. 1.1.4 Broken Necklace 2*N的拆环,枚举断点找即可.在找的过程中先要解