USACO: Palsquare

用递归的方法实现进制转换

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char ch[20] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
char a[1000];
char m[1000];
int c = 0;
int b = 0;
void change(int t, int n){
     b = t%n;
     t = t / n;
     if(t >= 1){
      if(b <= 9){
       a[c]=b + '0';
      }
      else{
        a[c] =ch[b];
      }
    c++;
    change(t,n);

    }else{
      if(b <= 9){
       a[c]=b + '0';
      }
      else{
        a[c] =ch[b];
      }
      c++;
    }
}
void convert(int n){
    for(int i =0; i<1000;i++){
        m[i] = NULL;
    }
    for(int i = 0; i < n; i++){
        m[i] = a[n-1-i];
    }
}
bool qalsquare(char *m,int n){
    for(int i = 0; i < n; i++){
        if(m[i] != m[n-1-i]){
            return false;
        }
    }
    return true;
}

int main()
{
    ifstream fin;
    ofstream fout;
    fin.open("palsquare.in");
    fout.open("palsquare.out");
    int n;
    int s;
    fin >> n;
    for(int i = 1; i<= 300;i++){
        for(int i = 0; i< 1000; i++){
            a[i] = NULL;
        }
       c= 0;
       s= i*i;
       change(s,n);
       convert(c);
       if(qalsquare(a,c)){
        for(int i = 0; i< 1000; i++){
            a[i] = NULL;
        }
        c= 0;
        change(i,n);
        convert(c);
        fout<<m<<" ";

        for(int i = 0; i< 1000; i++){
            a[i] = NULL;
        }
        c= 0;
        change(s,n);
        convert(c);
        fout<<m<<endl;

       }
    }

}
原文地址:https://www.cnblogs.com/superjn/p/5532884.html