USACO Palindromic Squares 源码

这题很容易的,依然是暴力搜索,好吧,这一章都叫complete search

要点就两个,一个是进制转换,一个是ASCII码转换(当然你要想用数组来搞也是没什么问题的)

难道就是在告诉你,暴力搜索才是你最需要care的么?!

下面上我的代码:

/*
ID: bbsunch2
PROG: palsquare
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 ("palsquare.out");
    ifstream fin ("palsquare.in");

    int baseNum = 0;
    fin >> baseNum;
    for(int i = 1; i <= 300; i++)
    {
        int square = i * i;
        string result = changeBase(square, baseNum);
        bool pals = checkPals(result);
        if(pals)
        {
            fout << changeBase(i, baseNum) << " " << result << endl;
        }
    }

    return 0;
}

 

你可能感兴趣的:(Algorithm,USACO)