九度 oj 题目1038:Sum of Factorials
来源:互联网 发布:js判断两个日期大小 编辑:程序博客网 时间:2024/06/08 13:35
http://ac.jobdu.com/problem.php?pid=1038
这道题首先想到用组合数,如果要列出完整的组合内容应考虑到用所有组合, 所有排列的模板。如果只是要组合的和(或者积)就相当于二叉搜索,中途还可以剪枝。
code1:
#include <stdio.h>static int f[11];static int n;void make_f(){ f[0] = 1; for (int i = 1; i < 11 ; ++i) { f[i] = i * f[i-1]; } } bool dfs(int x, int l){ if(x==n) return true; if(l>10) return false; if(x>n) return false; bool b0 = dfs(x,l+1); if(b0) return true; x += f[l]; bool b1 = dfs(x,l+1); return b1;} int main(){ make_f(); while(scanf("%d",&n) !=EOF){ if(n==0) printf("NO\n"); else printf("%s\n",dfs(0,0)?"YES":"NO"); } }
后来看题解发现,其实这是一道可以用贪心算法求解的题。因为f[i] > sum(f[0],,,,f[i-1])。 如果n是sum of factorials,且n>=f[i], 那么n中一定包含f[i],否则后面的所有f[0]...f[i-1]的和加起来都小于n。
code2:
#include <stdio.h>static int f[11];void make_f(){ f[0] = 1; for (int i = 1; i < 11; ++i) { f[i] = i * f[i-1]; } } int main(){ make_f(); int n; while(scanf("%d",&n)!=EOF){ if(n==0){ printf("NO\n"); continue; } for (int i = 10; i >=0 ; --i) { if (n>=f[i]) { n -= f[i]; } } printf("%s\n",n==0?"YES":"NO"); } }
0 0
- 九度oj 题目1038:Sum of Factorials
- 九度OJ题目1038:Sum of Factorials
- 九度 oj 题目1038:Sum of Factorials
- 九度OJ 1038 Sum of Factorials
- 九度oj-1038-Sum of Factorials
- 题目1038:Sum of Factorials 九度
- 九度OJ 1038 Sum of factorials (模拟)
- 九度[1038]-Sum of Factorials
- 题目1038:Sum of Factorials
- 题目1038:Sum of Factorials
- 题目1038:Sum of Factorials
- 题目1038:Sum of Factorials
- 题目1038:Sum of Factorials
- 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)
- 贪心法_题目1038:Sum of Factorials
- light oj 1189 - Sum of Factorials (数学)
- LIGHT OJ 1189 - Sum of Factorials【贪心+递归】
- light oj 1189 - Sum of Factorials (阶乘+贪心)
- [硕.Love Python] Heap(堆)
- Android应用实现多语言切换
- 获取坐标的总结
- 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾
- 我的博客网站重写历程--1.引言:说在前面的话与暂定开发进程
- 九度 oj 题目1038:Sum of Factorials
- 机房那些事儿---配置ODBC问题集锦
- javaweb中mysql数据库连接方法
- SpringMVC数据验证——第七章 注解式控制器的数据验证、类型转换及格式化
- Python变长参数,参数解包
- android_app开发微信支付集成
- 文章标题
- 使用pull解析器解析和生成xml文件
- spring-boot整合websocket