hdoj 1258 SUM IT UP
来源:互联网 发布:gx48b控制软件 编辑:程序博客网 时间:2024/06/06 14:28
程序的思想是:输入数据是,先使用快排对其从大到小进行排序,然后记录相同数据的个数,比如4 3 3 2 2 1 1,最后的数据变成4 3 2 1 ,并且同时数据的个数f[]变成1 2 2 2
然后就是遍历,相同的数据如果不能得到最后的结果,下一次就不会遍历。
//剪枝有这几个
首先:从大到小排序,剪枝1
再者:如果当前的sum比要遍历的数据小,则跳过这个数据
利用一个vector来记录结果
#include <iostream>//#include <fstream>#include <vector>#include <algorithm>using namespace std;/*0MS344K*///functionint cmp(const void *a,const void *b);void dfs(int index,int sum);//varint a[13]; //输入数据 int b[13]; //记录从大到小的数据,相同数据个数记录在f[]中 int l; //b[]数据的长度 int f[13]; //记录当前数据b[]的个数vector<int> c; //结果 bool flag;//fstream fin;int main(){ //fin.open("1258.txt",ios::in); int t,n; while(cin>>t>>n&&n!=0) { flag=false; for(int i=0;i<n;i++) { cin>>a[i]; } memset(f,0,sizeof(f)); qsort(a,n,sizeof(a[0]),cmp); //把相同的数据合并在一起 int cur=a[0]; //当前数据是多少 int j=0; b[j]=cur; for(int i=0;i<n;i++) { if(a[i]==cur) f[j]++; else { cur=a[i]; b[++j]=cur; i--; } } l=++j; cout<<"Sums of "<<t<<":"<<endl; dfs(0,t); if(!flag) cout<<"NONE"<<endl; } system("pause"); return 0;}void dfs(int index,int sum){ if(sum==0) { flag=true; int length=c.size(); for(int i=0;i<length-1;i++) cout<<c[i]<<"+"; cout<<c[length-1]<<endl; } else { for(int i=index;i<l;i++) { if(b[i]>sum) continue;//剪枝 else { c.push_back(b[i]); f[i]--; if(f[i]==0) dfs(i+1,sum-b[i]); else if(f[i]>0) dfs(i,sum-b[i]); f[i]++; c.pop_back(); } } }}int cmp(const void *a,const void *b){ return ((*(int *)b)-(*(int *)a));}
- 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
- The Star Zoo——1、You must marry me
- invoke DragQueryFile,@hDrop,-1,NULL,0 笔记
- Android NDK r8e __gmsl 错误
- 求四个数中的最大的数
- A note about how to write test plan
- hdoj 1258 SUM IT UP
- 数据库系统体系结构(一)概述
- 数据库系统体系结构(二)并行数据库
- 数据库系统体系结构(三)分布式数据库
- Dynamic Web Project 的学习笔记
- Embedded Linux Primer----嵌入式Linux基础教程--前言一
- yiiframework 简介 六
- (tomcat中级)应用Tomcat的WebappClassLoader加载指定目录的jar文件
- 菜单(Menu)