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

      看一下n2的base进制是不是回文串, 是的话输出来即可, 直接暴力枚举:

    /*
        ID: m1500293
        LANG: C++
        PROG: palsquare
    */
    
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    int B;
    
    int a[100], len;
    
    bool check(int n)
    {
        len = 0;
        int tp = n*n;
        while(tp > 0)
        {
            a[len++] = tp%B;
            tp /= B;
        }
        bool flog = true;
        for(int i=0; i<len/2; i++)
        {
            if(a[i] != a[len-1-i])
            {
                flog = false;
                break;
            }
        }
        return flog;
    }
    
    int b[100], len_b;
    
    void trans(int n)
    {
        len_b = 0;
        while(n > 0)
        {
            b[len_b++] = n%B;
            n /= B;
        }
        for(int i=len_b-1; i>=0; i--)
            if(b[i] < 10) printf("%d", b[i]);
            else printf("%c", 'A'+b[i]-10);
    }
    
    int main()
    {
        freopen("palsquare.in", "r", stdin);
        freopen("palsquare.out", "w", stdout);
        while(scanf("%d", &B) == 1)
        {
            for(int n=1; n<=300; n++)
            {
                if(check(n)) 
                {
                    trans(n);
                    printf(" ");
                    for(int i=0; i<len; i++)
                    {
                        if(a[i] < 10) printf("%d", a[i]);
                        else printf("%c", 'A'+a[i]-10);
                    }
                    printf("
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    python bif 如何自学
    python萌新应知应会
    Animation
    响应式布局
    浏览器兼容
    HTML基础
    SublimeText 3 Emmet Hot Keys
    Web大前端环境搭建
    Sublime Text 运行js
    bash脚本编程基础
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5055449.html
Copyright © 2011-2022 走看看