usaco 1.24

1.2的第4第5题都考察了10进制转X进制的问题,用以前的模板套一下就好了

/*
ID: zhangw31
PROG: palsquare
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;

int base;

string tentox (int num, int syst)
{
	string returnstring;
	string digt = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	stack<char>number;
	while (num != 0)
	{
		number.push(digt[num % syst]);
		num /= syst;
	}
	while (!number.empty())
	{
		returnstring += number.top();
		number.pop();
	}
	return returnstring;
}

int xtoten (string num, int syst)
{
	int xtotenn = 0;
	int length = num.length();
	for (int i = 0; i < length; ++i)
	{
		if (num[i] >= '0' && num[i] <= '9')	xtotenn = xtotenn * syst + (num[i] - '0');
		if (num[i] >= 'A' && num[i] <= 'Z') xtotenn = xtotenn * syst + (num[i] - 'A' + 10);
	}
	return xtotenn;
}

bool testPalindromic(string a) {
	int len = a.length();
	for (int i = 0; i < len / 2; ++i) {
		if (a[i] != a[len-1-i]) return false;
	}
	return true;
}
	

int main() {
	ifstream fin("palsquare.in");
	ofstream fout("palsquare.out");
	fin >> base;
	for (int i = 1; i <= 300; ++i) {
		string teststring = tentox(i*i, base);
		if (testPalindromic(teststring)) {
			fout << tentox(i, base) << " " << teststring << endl;
		}
	}
	fin.close();
	fout.close();
}

	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值