zoukankan      html  css  js  c++  java
  • USACO 1.2-Palindromic Squares

    /*
    ID: m1590291
    PROG: palsquare
    LANG: C++
    */
    #include <fstream>
    #include <string>
    using namespace std;
    
    char NUMBER[20] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
    
    string convert(int, int);
    bool is_plalindrome(string);
    
    int main()
    {
        ifstream fin("palsquare.in.txt");
        ofstream fout("palsquare.out.txt");
    
        int b;
        fin >> b;
    
        string num, square;
        for (int i = 1; i <= 300; ++i) {
            num = convert(i, b);
            square = convert(i*i, b);
            if (is_plalindrome(square))
                fout << num << " " << square << "
    ";
        }
    
        return 0;
    }
    
    string convert(int n, int b)
    {
        string number;
        while (n) {
            number.insert(number.begin(), NUMBER[n%b]);
            n /= b;
        }
        return number;
    }
    
    bool is_plalindrome(string s)
    {
    
        for (string::iterator back = s.begin(), forward = s.end()-1;
             back < forward; ++back, --forward) {
            if (*back != *forward)
                return false;
        }
        return true;
    }
    


  • 相关阅读:
    Kendo
    过河
    数组分组(简单dp)
    Codeforces Round #604 (Div. 2)(A-E)
    HDU1253
    HDU1026
    linux常用命令(二) --目录操作
    linux常用命令(一)--ls
    hdu 1072
    Codeforces Round #597 (Div. 2)(A-D)
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6352040.html
Copyright © 2011-2022 走看看