USACO Palindromic Squares 【STL__string_的应用】

这里有个讲解 string 用法非常详细的博文:https://www.byvoid.com/zhs/blog/cpp-string

题目意思很简单啦,就是找回文

 

使用string可以高速A过

 

Source code:

/*
ID: wushuai2
PROG: palsquare
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))

using namespace std;
const int INF = 0x3f3f3f3f;

string solve(int base, int n){
    string ss;
    while(n){
        if(n % base > 9){
            ss.push_back(n % base - 10 + 'A');
        } else{
            ss.push_back(n % base + '0');
        }
        n /= base;
    }
    reverse(ss.begin(), ss.end());
    return ss;
}

bool judge(string ss){
    string pp = ss;
    reverse(ss.begin(), ss.end());
    if(ss.compare(pp) == 0){
        return true;
    }
    return false;
}

int main() {
    ofstream fout ("palsquare.out");
    ifstream fin ("palsquare.in");
    int base;
    fin >> base;
    for(int i = 1; i <= 300; ++i){
        if(judge(solve(base, i * i))){
            fout << solve(base, i) << ' ' << solve(base, i * i) << endl;
        }

    }

    return 0;
}

 

posted @ 2015-01-20 19:56  Jeremy Wu  阅读(206)  评论(0)    收藏  举报