usaco-1.2.5 Palindromic Squares

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

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

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

         下面贴出自己的代码:

/*
ID:sunexio2
PROG:palsquare
LANG: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值