USACO1.2 Dual Palindromes(dualpal)

    从s的下一个数开始,依次检验是否在2~10进制的表示中,两种或两种以上表示形式是回文数,若是有两种就没必要继续检测,直接输出这个数字。直到找到n个这样的数字。值得记一下的是这个题是第一个我首次提交即经过所有测试例的题目。下面是返回的信息:YOUR PROGRAM ('dualpal') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.ios

 

/*
ID:jzzlee1
PROG:dualpal
LANG:C++
*/
#include <fstream>
#include<iostream>
#include<cstring>
using namespace std;
ifstream fin("dualpal.in");
ofstream fout("dualpal.out");
bool test(int b,int n)
{
	//n化为b进制,存进数组a[]
	int a[20];
	int i=0,sz=0;
	while(n)
	{
		a[sz++]=n%b;
		n/=b;
	}
	a[sz]=30;
	//判断n在b进制下是否是回文数
	bool flag=1;
	for(i=0;flag&&i<=sz/2;i++)
	{
		if(a[sz-1-i]!=a[i])
		flag=0;
	}
	return flag;
}
int main()
{
	int n,s;
	fin>>n>>s;
	int i=0,j=0,k=0;s++;
	while(i<n)
	{
		k=0;
		for(j=2;k<2&&j<=10;j++)
		{
			if(test(j,s))
				k++;
		}
		if(k==2)
		{
			fout<<s<<endl;
			i++;
		}
		s++;
	}

	return 0;

}
相关文章
相关标签/搜索