首页 > 其他 > 详细

poj3181 背包+大数

时间:2017-11-03 13:41:31      阅读:271      评论:0      收藏:0      [点我收藏+]

http://poj.org/problem?id=3181

Dollar Dayz
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7997   Accepted: 2992

Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:

        1 @ US$3 + 1 @ US$2

1 @ US$3 + 2 @ US$1
1 @ US$2 + 3 @ US$1
2 @ US$2 + 1 @ US$1
5 @ US$1
Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

Input

A single line with two space-separated integers: N and K.

Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

Sample Input

5 3

Sample Output

5

Source

   大数加法写成shi我也是很无奈- -,ans的位数写的不对导致部分数据能过让我检查半天。
  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 struct Bign
 6 {
 7     int a[105];
 8     void out()
 9     {
10         for(int i=a[0];i>=1;--i)
11             printf("%d",a[i]);puts("");
12     }
13 }f[1015];
14 void add(Bign &e,Bign &x){
15         int limit=max(e.a[0],x.a[0]);
16         e.a[0]=limit;
17         for(int i=1;i<=limit;++i)
18             e.a[i]+=x.a[i];
19         for(int i=1;i<=e.a[0];++i)
20         {
21             if(e.a[i]>9){
22                 e.a[i+1]++;
23                 e.a[i]%=10;
24             }
25             if(e.a[e.a[0]+1]) e.a[0]++;
26         }
27     }
28 int main()
29 {
30     int N,K,i,j;
31     while(cin>>N>>K){
32         for(i=0;i<=N;++i){
33                 memset(f[i].a,0,sizeof(f[i].a));
34                 f[i].a[0]=1;
35         }
36         f[0].a[1]=1;
37         for(i=1;i<=K;++i)
38             for(j=i;j<=N;++j){
39             add(f[j],f[j-i]);
40             }
41         f[N].out();
42     }
43     return 0;
44 }

 

poj3181 背包+大数

原文:http://www.cnblogs.com/zzqc/p/7777664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!