zoukankan      html  css  js  c++  java
  • 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 


    /*
        ID:qhn9992
        PROG:dualpal
        LANG:C++
    */

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <cstring>

    using namespace std;

    char cuan[100];

    void fun(int t,char cuan[],int base)
    {
        unsigned long long int res=0;
        char ts[10000];
        int cnt=0;
        while(t)
        {
            ts[cnt++]=(t%base)+'0';
            t/=base;
        }
        int cnt2=0;
        for(int i=cnt-1;i>=0;i--)
            cuan[cnt2++]=ts;
        cuan[cnt2]='';
    }

    bool ispali(int s,int d)
    {
        for(int i=s,j=d;i<j;i++,j--)
        {
            if(cuan==cuan[j]) ;
            else return false;
        }
        return true;
    }

    int main()
    {
        freopen("dualpal.in","r",stdin);
        freopen("dualpal.out","w",stdout);
        int n,s,t;
        scanf("%d%d",&n,&s);
        vector<int> v;
        t=s+1;
        while(n)
        {
            int cnt=0;
            for(int i=2;i<=10;i++)
            {
                fun(t,cuan,i);
                if(ispali(0,strlen(cuan)-1))
                {
                    cnt++;
                }
                if(cnt>=2)
                {
                    v.push_back(t);
                    n--;
                    break;
                }
            }
            t++;
        }
        for(int i=0;i<v.size();i++)
        {
            printf("%d ",v);
        }
        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )
  • 相关阅读:
    第四次实验报告
    第三次实验报告
    循环结构课后反思
    第二次实验报告
    第一次实验报告1
    第一次作业
    第二次实验报告 总结
    第九章实验报告
    第八章实验报告
    第六次实验报告
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350870.html
Copyright © 2011-2022 走看看