| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:不需要费力气想,用背包背出来In Reply To:Re:不需要费力气想,用背包背出来 Posted by:zoujing1978 at 2018-12-17 11:20:55 > 用背包做的,能贴个代码出来么?我学习一下,谢谢!
#include <iostream>
#include <algorithm>
int main() {
double x[1001];int fa[1001],r[1001],cntr=0,N;
scanf("%d",&N);
memset(x,0,sizeof(x));x[0]=1;
for(int i=1;i<=N;i++)
for(int j=N;j>=0;j--)//01背包,逆序遍历
if(j>=i&&x[j-i])
if(x[j-i]*i>x[j]){
x[j]=x[j-i]*i;
fa[j]=j-i;
}
int tmp=N;
while(tmp){
r[cntr++]=tmp-fa[tmp];
tmp=fa[tmp];
}
std::sort(r,r+cntr);
for(int i=0;i<cntr;i++) printf("%d ",r[i]);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator