USACO 1.2 Dual Palindromes

想不到更有效的方法,数据量不大,直接暴力

#include <iostream>
#include 
<fstream>

using namespace std;

ifstream fin(
"dualpal.in");
ofstream fout(
"dualpal.out");

#ifdef _DEBUG
#define out cout
#define in cin
#else
#define out fout
#define in fin
#endif

int buf[33];

bool isPal(int num, int base)
{
    
int cnt = 0;
    
while(num!=0){
        buf[cnt
++= num%base;
        num
/=base;
    }

    
int max = (cnt-1)/2;

    
for(int i=0;i<=max;++i){
        
if(buf[i]!=buf[cnt-1-i])
            
return false;
    }

    
return true;
}

bool duaPal(int num)
{
    
int cnt = 0;
    
for(int base=2;base<=10;++base){
        
if( isPal(num,base) ){
            cnt
++;
            
if(cnt==2)
                
return true;
        }
    }

    
return false;
}

void solve()
{
    
int n,s;
    
in>>n>>s;

    
while(n--){
       
while(!duaPal(++s));
       
out<<s<<endl;
    }        
}

int main(int argc,char *argv[])
{
  solve(); 
  
return 0;
}





附原题:
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


posted on 2009-06-05 15:09 YZY 阅读(1048) 评论(0)  编辑 收藏 引用 所属分类: AlgorithmUSACO


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

积分与排名

最新评论

阅读排行榜