当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

USACO Dual Palindromes 题解

发表于: 2013-04-23   作者:bbsunchen   来源:转载   浏览次数:
摘要: 一天水了两题,呵呵,题目确实挺水的 做完上一题,这一题已经毫无难度可言了,只改了点代码 诸位凑合看: /* ID: bbsunch2 PROG: dualpal LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <vector&

一天水了两题,呵呵,题目确实挺水的

做完上一题,这一题已经毫无难度可言了,只改了点代码

诸位凑合看:

/*
ID: bbsunch2
PROG: dualpal
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

string changeBase(int formerNum, int baseNum)
{
    string latterNumString;
    while(formerNum)
    {
        char c;
        int num = formerNum % baseNum;
        if(num < 10)
        {
            c = (char)(num+48);
        }else
        {
            c = (char)(num+55);
        }
        latterNumString.insert(latterNumString.begin(),c);
        formerNum =  (int)(formerNum / baseNum);
    }
    return latterNumString;
}

bool checkPals(string input)
{
    bool pals = true;
    int inputLength = input.length();
    int halfLength = (int)(inputLength/2);
    for(int i = 0; i < halfLength; i++)
    {
        int k = inputLength - i - 1;
        if(input[i] != input[k])
        {
            pals = false;
            break;
        }
    }
    return pals;
}

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

    int N = 0;
    int S = 0;
    fin >> N >> S;
    int printNum = 0;

    while(printNum < N)
    {
        S ++;
        int palsNum = 0;
        bool dualpal = false;
        for(int baseNum = 2; baseNum <= 10; baseNum++)
        {
            string result = changeBase(S, baseNum);
            bool pals = checkPals(result);
            if(pals)
            {
                palsNum ++;
                if(palsNum >= 2)
                {
                    dualpal = true;
                    break;
                }
            }
        }
        if(dualpal)
        {
            fout << S << endl;
            printNum ++;
        }
    }
    return 0;
}

 

USACO Dual Palindromes 题解

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
【问题描述】 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出
题意分析: 给出4个长方形的高和长,以及给出6种基本布局,求合成无重叠的最小面积时的长和宽。 解题
select 11+22+22 ; select 11+22+22 from dual; select ab+ba; select ab+ba from dual; 我个人理解
A system switches between non-secure and secure modes by making processes, applications, and
In multiple-output power supplies in which a single supply powers circuitry of vastly differe
这道题目,给你一组时间范围,求最大的时间跨度和最大的时间段间隔。题目看似挺简单,但是如果不加
我知道写这东西挺二,可是我确实被USACO的提交折腾了很久…… 首先,它的 TEXT Submitting Solution
Batteries are the typical power sources for portable-system applications, and it is not unusu
This Design Idea stems from the limited availability of IC voltage regulators that can meet k
The AD736 true-rms-to-dcconverter is useful for many applications that require precise calcul
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号