1019. General Palindromic Number (20)

考察回文以及任意进制数的转化

#include<iostream>
#include<vector>

void GetReverseNumberBaseD(int N, int D, std::vector<int>& a)
{
	do
	{
		int temp = N%D;
		a.push_back(temp);
		N /= D;
	}while(N != 0);
}
bool IsPalindromic(const std::vector<int>& a)
{
	int len = (int)a.size();
	for(int i = 0; i < len; ++i)
	{
		if(a[i] != a[len-1-i])
			return false;
	}
	return true;
}
void ReverseOutput(const std::vector<int>& a)
{
	int len = (int)a.size();
	for(int i = len-1; i >= 0; --i)
	{
		if(i != 0)
			printf("%d ",a[i]);
		else
			printf("%d\n",a[i]);
	}
}
int main()
{
	int N, D;
	while(scanf("%d%d",&N,&D) != EOF)
	{
		std::vector<int> reverseNumBaseD;
		GetReverseNumberBaseD(N, D, reverseNumBaseD);
		
		if( IsPalindromic(reverseNumBaseD) )
		{
			printf("Yes\n");
		}
		else printf("No\n");
		ReverseOutput(reverseNumBaseD);
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)