96高市資訊學科能力競賽 第六題:吸血鬼數(Vampire Number)
作法:建表
想法:建表完 再開始比對所有可能 但是可能會有重複 在最後作輸出的時候處理
輸出前要排序.
/**********************************************************/
#include<stdio.h>
#include<stdlib.h>
char num[10000][10]={0};
main()
{
int a,b,c,n;
for(a=11;a<10000;a++)
{
n=a;
while(n)
{
num[a][n%10]++;
n=n/10;
}
}
while(scanf("%d",&n)==1)
{
int ans[10000]={0},top=0;
if(n==4)
{
for(a=10;a<100;a++)
for(b=a;b<100;b++)
{
if((a*b)%2!=0||(a%10==0&&b%10==0)) continue;
int temp[10]={0},tempn=a*b;
while(tempn)
{
temp[tempn%10]++;
tempn=tempn/10;
}
for(c=0;c<10;c++)
if(temp[c]!=num[a][c]+num[b][c]) break;
if(c==10)
{
ans[top]=a*b;
top++;
}
}
}
else if(n==6)
{
for(a=100;a<1000;a++)
for(b=a;b<1000;b++)
{
if((a*b)%2!=0||(a%10==0&&b%10==0)) continue;
int temp[10]={0},tempn=a*b;
while(tempn)
{
temp[tempn%10]++;
tempn=tempn/10;
}
for(c=0;c<10;c++)
if(temp[c]!=num[a][c]+num[b][c]) break;
if(c==10)
{
ans[top]=a*b;
top++;
}
}
}
else
{
for(a=1000;a<10000;a++)
for(b=a;b<10000;b++)
{
if((a*b)%2!=0||(a%10==0&&b%10==0)) continue;
int temp[10]={0},tempn=a*b;
while(tempn)
{
temp[tempn%10]++;
tempn=tempn/10;
}
for(c=0;c<10;c++)
if(temp[c]!=num[a][c]+num[b][c]) break;
if(c==10)
{
ans[top]=a*b;
top++;
}
}
}
for(a=0;a<top;a++) /*排序好輸出*/
{
c=a;
for(b=a+1;b<top;b++)
if(ans[b]<ans[c]) c=b;
if(c!=a)
{
int temp;
temp=ans[c];
ans[c]=ans[a];
ans[a]=temp;
}
if(ans[a]==ans[a-1]&&a!=0) continue;
printf("%d\n",ans[a]);
}
}
return 0;
}