24h購物| | PChome| 登入
2009-05-10 16:01:08| 人氣728| 回應0 | 上一篇 | 下一篇

96高市資訊學科能力競賽 第六題:吸血鬼數(Vampire Number)

推薦 0 收藏 0 轉貼0 訂閱站台

作法:建表
想法:建表完 再開始比對所有可能 但是可能會有重複 在最後作輸出的時候處理

輸出前要排序.

/**********************************************************/

#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;
}

台長: 來源不明
人氣(728) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: 資訊競賽 |
此分類下一篇:2006 NOIP 普及組 NOIP2006 3.Jam的計數法
此分類上一篇:2007 NOIP 普及組 NOIP2007 3.守望者的逃離

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文