zoukankan      html  css  js  c++  java
  • USACO section1.2 Palindromic Squares

    给出一个数 n(十进制),求出 1-300 范围内所有平方(n 进制)为回文串的数,并打印。

    /*
    PROG: palsquare
    LANG: C++
    */
    # include <cstdio>
    # include <cstring>
    
    # define N 300
    
    void strRev(char *s)
    {
        char ch;
        int len = strlen(s), mid = len / 2;
        for (int i = 0; i < mid; ++i)
            ch = s[i], s[i] = s[len-1-i], s[len-1-i] = ch;
    }
    
    void to(int base, int x, char *s)
    {
        int i = 0, t;
        while (x > 0)
        {
            t = x % base;
            if (t > 9) t += 'A'-10;
            else t += '0';
            s[i++] = t;
            x /= base;
        }
        s[i] = 0;
        strRev(s);
    }
    
    char isPal(char *s)
    {
        int len = strlen(s), mid = len / 2;
        for (int i = 0; i < mid; ++i)
            if (s[i] != s[len-i-1]) return 0;
        return 1;
    }
    
    int main()
    {
        int n;
    
        freopen("palsquare.in", "r", stdin);
        freopen("palsquare.out", "w", stdout);
    
        scanf("%d", &n);
        for (int i = 1; i <= N; ++i)
        {
            char a[10], b[20];
            to(n, i, a), to(n, i*i, b);
            if (isPal(b)) printf("%s %s\n", a, b);
        }
    
        fclose(stdin);
        fclose(stdout);
    
        return 0;
    }

    /**/

  • 相关阅读:
    Java-数据字符串进行四舍五入
    Git本地安装及汉化
    Navicat安装教程
    Jdk+maven安装
    系列文章
    @PathVariable
    feign组件
    Lombok
    常见Jar包的用途
    iOS archive(归档)的总结
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2595546.html
Copyright © 2011-2022 走看看