24h購物| | PChome| 登入
2009-04-30 18:47:08| 人氣566| 回應0 | 上一篇 | 下一篇

ACM 574 Sum It Up

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

作法:(1)14個for(2)DFS(我所提供的解法)
網友若想提供14個for的暴力解 可以貼上來
想法:1.請計算同樣數字的個數
     2.請了解題目 才看看我的作法 不知道要怎麼說明

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

#include<stdio.h>
#include<stdlib.h>
int num[14][2]={0},top=1,find=0;
int anstemp[14]={0};
int n,m;
void make(int sum,int now)
{
 int a,b,c;
 for(a=num[now][1];a>=0;a--)
   {
     if(sum+a*num[now][0]<n&&now+1<top)
       {
        anstemp[now]=a;
        make(sum+a*num[now][0],now+1);
       }
     if(sum+a*num[now][0]==n)
       {
        anstemp[now]=a;
        int ans[14]={0},anstop=0;
        for(b=1;b<now+1;b++)
         {
          if(anstemp[b]!=0)
           for(c=0;c<anstemp[b];c++,anstop++)
            ans[anstop]=num[b][0];
         }
         for(b=0;b<anstop-1;b++)
          printf("%d+",ans[b]);
          printf("%d",ans[b]);
         printf("\n");
         find=1;
       }
   }
}
main()
{
 while(scanf("%d %d",&n,&m)==2)
   {
    int a,b,c,temp;
    if(n==0&&m==0) break;
    top=1;find=0;num[0][0]=-1;
    for(a=0;a<m;a++)
     {
      scanf("%d",&temp);
      if(temp==num[top-1][0]) num[top-1][1]++;
      else {num[top][0]=temp;num[top][1]=1;top++;}
     }
     printf("Sums of %d:\n",n);
     make(0,1);
     if(find==0) printf("NONE\n");
     for(a=0;a<=13;a++) {num[a][0]=0;num[a][1]=0;}
   }
 return 0;
}

台長: 來源不明
人氣(566) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: ACM |
此分類下一篇:ACM 11032 11032 - Function Overloading
此分類上一篇:ACM 11572 11572 - Unique Snowflakes

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