usaco-1.2.5 Palindromic Squares

来源:互联网 发布:linux启动weblogic命令 编辑:程序博客网 时间:2024/05/22 03:52

         最近都复习不进去。考研的热情感觉忽然都没了。为了不让自己颓废下去,就让自己做几道题。今天做的这道题目很简单,其实就是进制转换。而且数据量不大,只需要穷举就好了。一开始我还想着转换了进制以后不是需要每个进制都写一个乘法么?还好很快就想到了先在十进制下乘好了再做进制转换就好了。

         题目链接:http://cerberus.delos.com:790/usacoprob2?a=H7jNbILHw91&S=palsquare

         题目的翻译还是自行百度  nocow。

         下面贴出自己的代码:

/*ID:sunexio2PROG:palsquareLANG:C++ */#include <iostream>#include <fstream>#include <map>#include <string>#include <cmath>using namespace std;ifstream fin("palsquare.in");ofstream fout("palsquare.out");bool judge(string &str){for(int i = 0, j = str.length() - 1; i < j; ++i, --j ){if(str[i] != str[j])return 0;}return 1;}int main(){int n;char s[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};while(fin >> n){string str;for(int i = 1; i < 300; ++i){int tmp = i * i;str = "";while(tmp){str += s[tmp % n];tmp /= n;}if(judge(str)){int ttmp = i;string tt;while(ttmp){tt += s[ttmp % n];ttmp /= n;}for(int j = tt.length() - 1; j >= 0; --j)fout << tt[j];fout << ' ';for(int j = str.length() - 1; j >= 0; --j)fout << str[j];fout << endl;}}}return 0;}


0 0
原创粉丝点击