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


  • 相关阅读:
    1.Android 视图及View绘制分析笔记之setContentView
    Android 6.0
    include、merge 、ViewStub
    Vitamio视频播放器
    EventBus 二
    EventBus 一
    ZJOI2002 昂贵的聘礼
    [POI2009]WIE-Hexer
    UVA 11440 Help Tomisu
    洛谷 2448 无尽的生命
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6352040.html
Copyright © 2011-2022 走看看