4117:简单的整数划分问题 很难理解,绕来绕去绕不明白
#include <bits/stdc++.h>
using namespace std;
int takeapart(int n,int i){
if(n==0)return 1;
if(i==1)return 1;
if(n>=i)return takeapart(n-i,i)+takeapart(n,i-1);
if(n<i) return takeapart(n,n);
}
int main(){
int n;
while(cin>>n){
cout<<takeapart(n,n)<<endl;
}
return 0;
}
如下是之前分解因数的题,些许类似 1751:分解因数
#include <bits/stdc++.h>
int sum=0;
using namespace std;
void takeapart(int begin,int n){
for(int i=begin;i<=n;i++){
if(n%i==0&&n/i>=i){
sum++;
takeapart(i,n/i);
}
else
break;
}
}
int main(){
int n;
while(cin>>n){
sum=1;
takeapart(2,n);
cout<<sum<<endl;
}
return 0;
}
还有下面这段毫无厘头没有结果的 QWQ
#include <bits/stdc++.h>
int sum=0;
using namespace std;
void takeapart(int sub,int n){
if(n==1||sub==1)sum++;
for(int i=sub;i>=0;i--){
if(n>i)takeapart(i,n-i);
if(n<i)takeapart(n,n);
if(n==i)sum++;
}
}
int main(){
int n;
while(cin>>n){
takeapart(n,n);
cout<<sum<<endl;
}
return 0;
}
希望学完动态规划之后能够清晰起来,I’ll come back!
|