关闭

Section 1.2 Dual Palindromes

标签: c语言
178人阅读 评论(0) 收藏 举报
分类:

数制转换,num2str,string方法
是上面一题(Palindromic Squares)的简单变形

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

#include<fstream>
#include<string>
using namespace std;

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

//ifstream fin("in.txt");
//ofstream fout("out.txt");

bool IsDualPal(int);
bool IsPal(int,int);
int main()
{
    int N,S;
    fin>>N >>S;
    while(N>0)
    {
        S++;
        if(IsDualPal(S))
        {
            fout<<S <<endl;
            N--;
        }
    }
    fin.close();
    fout.close();
    return 0;
}

bool IsDualPal(int n)
{
    /*
        To judge whether n is 
        a dual-palindrom or not.
    */
    int base;
    int pal_num = 0;
    for(base=2;base<=10;base++)
    {
        if(IsPal(n,base))
            pal_num++;
        if(pal_num == 2)
            break;
    }
    if(pal_num == 2)
        return true;
    else
        return false;
}

bool IsPal(int n,int base)
{
    /*
        To judge whether n is a 
        palindrom or not on the base given.
    */
    string s;
    int quotient,remainder;

    do
    {
        quotient = n / base;
        remainder = n % base;
        n = quotient;
        s.insert(s.begin(),remainder+'0');
    }
    while(quotient > 0);

    int i,len=s.length();
    bool result = true;
    for(i=0;i<len/2;i++)
    {
        if(s[i] != s[len-i-1])
        {
            result = false;
            break;
        }
    }
    return result;
}
0
0
查看评论

USCAO Section 1.2 Dual Palindromes

原题: Dual 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 p...
  • qq_34407173
  • qq_34407173
  • 2017-05-30 13:14
  • 80

USACO Section 1.2 Dual Palindromes

/* ID: lucien23 PROG: dualpal LANG: C++ */ #include #include #include #include using namespace std; string num2string(int num,int baseNum); int main(...
  • LucienDuan
  • LucienDuan
  • 2013-11-25 21:44
  • 698

USACO section 1.2 Dual Palindromes

这个就是简单的进制转换加回文判断,其他方面的逻辑自己控制一下就好。/* ID: 13913351 LANG: C TASK:dualpal */ #include<stdio.h> #include<string.h> char num[21]={"01234567...
  • nay_nix2
  • nay_nix2
  • 2017-05-26 15:39
  • 62

USCAO section 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 righ...
  • nealgavin
  • nealgavin
  • 2012-07-05 21:40
  • 362

[题目] Section 1.2 Dual Palindromes

[题目] Section 1.2 Dual Palindromes [题意]如果一个整数N在2~10进制下至少有两次是回文数,那么N就叫做双重回文数。给出两个整数N和S,找出从N开始的前S个双重回文数并输出。 [思路]和上一个题基本一样。。 [代码] #include #include...
  • u014293306
  • u014293306
  • 2014-09-01 20:17
  • 300

USACO - Chapter1 Section 1.2 - Dual Palindromes

Dual Palindromes
  • zhuo1ang
  • zhuo1ang
  • 2017-05-13 16:08
  • 82

USACO 1.2.5 Dual Palindromes双重回文数

题目描述如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”。例如,12321就是一个回文数,而77778就不是。当然,回文数的首和尾都应是非零的,因此0220就不是回文数。事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数。 编一个...
  • Nidhogg__
  • Nidhogg__
  • 2016-09-13 17:24
  • 279

USACO 1.2 Dual Palindromes

依旧是暴力判断,没啥好说的 #include #include #include #include using namespace std; int n, s; int t(0), p; int a[100]; bool check(int n, int k) { int t...
  • baidu_23081367
  • baidu_23081367
  • 2014-11-22 02:56
  • 336

1.2 Dual Palindromes

原题链接:http://ace.delos.com/usacoprob2?a=Wk8bBciEzIF&S=dualpal /* ID:wconvey PROG:dualpal LANG:C++ */ #include #include using namespace ...
  • wconvey
  • wconvey
  • 2012-01-11 20:38
  • 186

usaco 1.2 Dual 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 numb...
  • cfylove
  • cfylove
  • 2015-08-08 21:16
  • 312
    个人资料
    • 访问:3052次
    • 积分:173
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条