zoukankan      html  css  js  c++  java
  • USACA section 1.2.3 Palindromic Squares

    1. 进制转换+回文判断+int与char之间相互转换;

    以下是代码:

    /*
    ID: dollar4
    PROG: palsquare
    LANG: C++
    */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    int base;
    bool checkp(string str)
    {
        int len = str.size();
        for (int i = 0; i < len; i++)
        {
            if (str[i] != str[len-1-i])
                return false;
        }
        return true;
    }
    string trans(int a)
    {
        string str1 = "";
        int tmp;
        while (a)
        {
            tmp = a % base;
            if (tmp < 10)
                str1 += tmp + '0';
            else
                str1 += tmp - 10 + 'A';
            a /= base;
        }
        return str1;
    }
    string change(string str)
    {
        string rlt = str;
        int len = str.size();
        for (int i = 0; i < len; i++)
            rlt[i] = str[len-1-i];
        return rlt;
    }
    int main()
    {
        ofstream fout ("palsquare.out");
        ifstream fin ("palsquare.in");
        int i;
        string str1, str2;
        fin >> base;
        for (i = 1; i <= 300; i++)
        {
            str1 = trans(i * i);
            if (checkp(str1))
            {
                str2 = change(trans(i));
                fout << str2 << " " << str1 << endl;
            }
        }
        return 0;
    }
    

    以下是官方参考代码


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <ctype.h>
    #include <math.h>
    
    /* is string s a palindrome? */
    int
    ispal(char *s)
    {
        char *t;
    
        t = s+strlen(s)-1;
        for(t=s+strlen(s)-1; s<t; s++, t--)
        	if(*s != *t)
    	    return 0;
    
        return 1;
    }
    
    /* put the base b representation of n into s: 0 is represented by "" */
    
    void
    numbconv(char *s, int n, int b)
    {
        int len;
    
        if(n == 0) {
    	strcpy(s, "");
    	return;
        }
    
        /* figure out first n-1 digits */
        numbconv(s, n/b, b);
    
        /* add last digit */
        len = strlen(s);
        s[len] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n%b];
        s[len+1] = '\0';
    }
    
    void
    main(void)
    {
        char s[20];
        char t[20];
        int i, base;
        FILE *fin, *fout;
    
        fin = fopen("palsquare.in", "r");
        fout = fopen("palsquare.out", "w");
        assert(fin != NULL && fout != NULL);
    
        fscanf(fin, "%d", &base);
        for(i=1; i <= 300; i++) {
    	numbconv(s, i*i, base);
    	if(ispal(s)) {
    	    numbconv(t, i, base);
    	    fprintf(fout, "%s %s\n", t, s);
    	}
        }
        exit(0);
    }


  • 相关阅读:
    Node.js入门学习笔记
    Memcached服务器安装、配置、使用详解
    基于Dubbo框架构建分布式服务
    Apache Beam:一个开源的统一的分布式数据处理编程库
    Spring Cloud Netflix构建微服务入门实践
    内部排序算法:快速排序
    内部排序算法:冒泡排序
    内部排序算法:基数排序
    Java常见面试题
    svn+ssh方式svn服务器和客户端的配置[转载]
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188921.html
Copyright © 2011-2022 走看看