记录编号 69431 评测结果 AAAAAAAAAA
题目名称 [金陵中学2007] 吉祥数 最终得分 100
用户昵称 Gravatar赵寒烨 是否通过 通过
代码语言 C++ 运行时间 0.003 s
提交时间 2013-09-15 15:14:05 内存使用 0.32 MiB
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <memory.h>
#include <iostream>
#include <ctime>
using namespace std;

const int weisqr[10][9]=
{
	{0,0,0,0,0,0,0,0,0},
	{1,1,1,1,1,1,1,1,1},
	{1,2,4,8,16,32,64,128,256},
	{1,3,9,27,81,243,729,2187,6561},
	{1,4,16,64,256,1024,4096,16384,65536},
	{1,5,25,125,625,3125,15625,78125,390625},
	{1,6,36,216,1296,7776,46656,279936,1679616},
	{1,7,49,343,2401,16807,117649,823543,5764801},
	{1,8,64,512,4096,32768,262144,2097152,16777216},
	{1,9,81,729,6561,59049,531441,4782969,43046721}
};
int cal(int num,int level)
{
	int total=0,temp;
	while (num)
	{
		temp=num%10;
		total+=weisqr[temp][level];
		num/=10;
	}
	return(total);
}
void swapi(int& a,int& b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
}
void swapb(bool& a,bool& b)
{
	if (a==b) return;
	else
	{
		a=!a;
		b=!b;
	}
}
void qsort(int* a,int l,int h)
{
	int i=l,j=h,m=a[(i+j)/2];
	while (i<=j)
	{
		while (a[i]<m) i++;
		while (a[j]>m) j--;
		if (i<=j) swapi(a[i++],a[j--]);
	}
	if (i<h) qsort(a,i,h);
	if (j>l) qsort(a,l,j);
}
int main(void)
{
	freopen("ghillie.in","r",stdin);
	freopen("ghillie.out","w",stdout);
	int i,j,n=1,k,m,a[201]={0},num;
	bool b[201]={false},c[201]={false};
	scanf("%d\n",&m);
	while (scanf("%d",&a[n])==1) n++;
	n--;
	for (k=1;k<=m;k++)
	{
		memset(b,0,sizeof(b));
		for (i=1;i<=n;i++)
		{
			if (!c[i]) num=cal(a[i],k+1);
			for (j=1;j<=n;j++)
				if (num==a[j]) b[j]=true;
		}
		for (i=1;i<=n;i++)
			if (b[i]) c[i]=true;
	}
	for (i=1,j=n;i<=j;)
	{
		while (!c[i]) i++;
		while (c[j]) j--;
		if (i<=j)
		{
			swapb(c[i],c[j]);
			swapi(a[i++],a[j--]);
		}
	}
	qsort(a,1,j);
	for (i=1;i<=j;i++) printf("%d ",a[i]);
	printf("\n");
	return 0;
}