hdu1028 Ignatius and the Princess III

本文解析了HDU ACM题目1028的解决方案,通过递归方法实现了整数分割问题,并提供了两种不同的代码实现方式。一种为直接递归计算,但存在超时风险;另一种预先计算所有可能的结果并存储在一个数组中,以提高运行效率。

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1028

递归能跑。。。

但是我交了直接超时了,然后我就把数据跑出来了再交的,等的时间有点长(怪不得超时)。

过的有点猥琐,看别人代码用母函数过的。

 1 #include<iostream>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<stdio.h>
 6 using namespace std;
 7 int b[130];
 8 const int M=120;
 9 int divide(int n,int m)
10 {
11     if(n==1 || m==1)
12     return 1;
13     if(n==m)
14     return (divide(n,m-1)+1);
15     if(n>m)
16     return (divide(n,m-1)+divide(n-m,m));
17     if(n<m)
18     return divide(n,n);
19 }
20 void table()
21 {
22     memset(b,0,sizeof(b));
23     for(int i=1;i<=M;i++)
24     b[i]=divide(i,i);
25 }
26 
27 int main()
28 {
29     freopen("out2.txt","w",stdout);
30     int n;
31     table();
32     for(int i=1;i<=M;i++)
33     {
34         printf("%d,",b[i]);
35         if(i%10==0)
36         printf("\n");
37     }
38     return 0;
39 }

 

 

 1 #include<iostream>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<stdio.h>
 6 using namespace std;
 7 int b[130]={1,1,2,3,5,7,11,15,22,30,42,
 8 56,77,101,135,176,231,297,385,490,627,
 9 792,1002,1255,1575,1958,2436,3010,3718,4565,5604,
10 6842,8349,10143,12310,14883,17977,21637,26015,31185,37338,
11 44583,53174,63261,75175,89134,105558,124754,147273,173525,204226,
12 239943,281589,329931,386155,451276,526823,614154,715220,831820,966467,
13 1121505,1300156,1505499,1741630,2012558,2323520,2679689,3087735,3554345,
14 4087968,4697205,5392783,6185689,7089500,8118264,9289091,10619863,12132164,
15 13848650,15796476,18004327,20506255,23338469,26543660,30167357,34262962,
16 38887673,44108109,49995925,56634173,64112359,72533807,82010177,92669720,
17 104651419,118114304,133230930,150198136,169229875,190569292,
18 214481126,241265379,271248950,304801365,342325709,384276336,431149389,
19 483502844,541946240,607163746,679903203,761002156,851376628,952050665,
20 1064144451,1188908248,1327710076,1482074143,1653668665,1844349560};
21 int main()
22 {
23     int n;
24     while(~scanf("%d",&n))
25     {
26         printf("%d\n",b[n]);
27     }
28     return 0;
29 }

 

转载于:https://www.cnblogs.com/xuesen1995/p/4438060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值