Hdoj 1258 Sum it up
来源:互联网 发布:淘宝 破冰仪式 编辑:程序博客网 时间:2024/05/18 01:38
题目请点击这里
题意:给定一个数sum,和n个数,问sum可以由这里面哪些数来表示?(等式不能重复,典型dfs)
ac代码如下:
(开始把sums写成sum,wa了4发,太粗心了我)
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int sum,n,flag;int a[20],ans[20];void dfs(int x,int cnt,int m)///m 控制等式的和,cnt控制等式因子个数,x为当前等式的第一个数{ int i,last; ///if(m>sum) ///return ; if(m==sum) { flag=1; for(i=1;i<cnt;i++) { if(i==cnt-1) printf("%d\n",ans[i]); else printf("%d+",ans[i]); } return ; } last=-1; for(i=x;i<=n;i++) { if(last!=a[i]) { ans[cnt]=a[i]; last=a[i];///保证了所有等式不会有重复的 dfs(i+1,cnt+1,m+a[i]); } } return ;}int main(){ while(scanf("%d%d",&sum,&n),n||sum) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); flag=0; printf("Sums of %d:\n",sum); dfs(1,1,0); if(!flag) printf("NONE\n"); } return 0;}
0 0
- hdoj 1258 SUM IT UP
- Hdoj 1258 Sum it up
- hdoj-1258 Sum It Up
- hdoj 1258 Sum It Up (递归回溯)
- hdoj 1258 Sum It Up(DFS)
- hdoj 1258 Sum It Up【DFS】
- hdoj--1258--Sum It Up(dfs)
- HDOJ(HDU).1258 Sum It Up (DFS)
- HDOJ 1258 Sum It Up(DFS)
- HDOJ 题目1258 Sum It Up(DFS)
- hdu 1258 Sum It Up
- HDU 1258 sum it up
- HDU-1258-Sum It Up
- HDU 1258 Sum It Up
- hdu 1258 sum it up
- HDU 1258 - Sum It Up
- hdu 1258 Sum It Up
- hdu 1258 Sum It Up
- 苹果开发者账号的类型
- JavaSE----API之集合(Collection、List及其子类、Set及其子类、JDK1.5新特性)
- 配置Log4j
- 黑马程序员 单例设计模式
- android paopao窗口+gridview 的实现底部菜单
- Hdoj 1258 Sum it up
- 模板函数 为 void maxn(T *,int n)
- LeetCode-Lowest Common Ancestor of a Binary Tree 深度解析
- 定义常量的三种方法
- 浅谈new与delete
- GitHub上整理的一些工具,求补充
- mybatis if test 相等的情况如何动态拼接sql
- 设计模式总结(二)
- ehcache集群详解二(集群的最小配置)