码迷,mamicode.com
首页 > 其他好文 > 详细

USACO Palindromic Squares 【STL__string_的应用】

时间:2015-01-20 21:45:29      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

 

使用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;
}

 

USACO Palindromic Squares 【STL__string_的应用】

标签:

原文地址:http://www.cnblogs.com/wushuaiyi/p/4237156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!