zoukankan      html  css  js  c++  java
  • usaco 1.2.5Dual 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
    View Code
     1 /*
    2 ID: qyxiang1
    3 PROG: dualpal
    4 LANG: C++
    5 */
    6
    7 #include <fstream>
    8 #include<string>
    9 #include<iostream>
    10 #include<memory.h>
    11 #include<algorithm>
    12 #include<vector>
    13 #include<string.h>
    14 using namespace std;
    15
    16 ifstream fin("dualpal.in");
    17 ofstream fout("dualpal.out");
    18
    19 #ifdef _DEBUG
    20 #define out cout
    21 #define in cin
    22 #else
    23 #define out fout
    24 #define in fin
    25 #endif
    26
    27 char map[] = {'0','1','2','3','4','5','6','7','8','9'};
    28 char target[33];
    29 bool trans(int initial)
    30 {
    31 int count = 0;
    32 for(int base = 2; base <= 10; ++base)
    33 {
    34 int init = initial;//就是这里,开始的时候形参是init,根本没有照顾到后面的变化
    35 int j = -1;
    36 int mod, div;
    37 memset(target,'\0',sizeof(target));
    38 bool flag = true;
    39
    40 do
    41 {
    42 mod = init%base;
    43 target[++j] = map[mod];
    44 init = init/base;
    45 }while(init != 0);
    46
    47 int k ;
    48 for(k = 0; k < j; ++k, --j)
    49 {
    50 if(target[j] != target[k])
    51 {
    52 flag = false;
    53 break;
    54 }
    55 }
    56 if(flag)++count;
    57 if(count > 1)return true;
    58 }
    59 return false;
    60 }
    61
    62 int main()
    63 {
    64 int n, s;
    65 in >> n >> s;
    66
    67 int i;
    68 for(i = s+1; n; ++i)
    69 {
    70 if(trans(i))
    71 {
    72 out << i << endl;
    73 n--;
    74 }
    75 }
    76 return 0;
    77 }

    28
    


    开始还以为是和前一题差不多的,但是发现函数调用过多出现超时现象,又合并成一个程序了。又出现一个小错误,太粗心了,开始的时候忘记把引入的参数赋给另一个变量了,错的好悲催啊。。。

  • 相关阅读:
    通过Form添加数据到数据库里
    如何取消服务器/主机空间目录脚本的执行权限
    一个人的网站开发
    3.1.2 新版视频教程震撼发布(30集)
    P类问题,NP,NPC,HPHard,coNP,NPI问题 的简单认识
    随机算法与近似算法
    python to exe
    ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
    anaconda + pyqt5 + pycharm 安装,测试
    C++ 结构体初始化
  • 原文地址:https://www.cnblogs.com/cosmoseeker/p/2140536.html
Copyright © 2011-2022 走看看