´问题描述:
给定一个自然数 n,由 n 开始可以依次产生半数集 set(n)中的数如下。
(1) n∈set(n);
(2) 在 n 的左边加上一个自然数,但该自然数不能超过最近添加的数的一半;
(3) 按此规则进行处理,直到不能再添加自然数为止。
例如,set(6)={6,16,26,126,36,136}。半数集 set(6)中有 6 个元素。
注意半数集是多重集。
´编程任务:
对于给定的自然数 n,编程计算半数集 set(n)中的元素个数。
代码:
#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
int a[1000];
int comp(int n);
int comp(int n){
int ans=1;
if(a[n]>0){
return a[n];
}
for(int i=1;i<=n/2;i++){
ans+=comp(i);
}
a[n]=ans;
return ans;
}
int main(){
int n;
cin>>n;
cout<<comp(n);
}
文件读写输出
#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
int a[1000];
int comp(int n);
int comp(int n){
int ans=1;
if(a[n]>0){
return a[n];
}
for(int i=1;i<=n/2;i++){
ans+=comp(i);
}
a[n]=ans;
return ans;
}
int main(){
int n,x;
fstream infile("input.txt",ios::in);
ofstream outf;
outf.open("output.txt");
while(!infile.eof()){
infile>>x;
outf<<comp(x)<<endl;
}
outf.close();
infile.close();
}