65,186
社区成员




Description
集合的前N个元素:编一个程序,按递增次序生成集合M的最小的N个数,M的定义如下:
(1)数1属于M;
(2)如果X属于M,则Y=2*X+1和Z=3*x+1也属于M;
(3)此外再没有别的数属于M。
Input
n(b.in) (1≤n≤100)
Output
生成集合M的最小的N个数(b.out)
#include <set>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
set<int> s;
int n,i;
cin>>n;
s.insert(1);
int p1,p2;
set<int>::iterator ite=s.begin();
for(i=1; i<=n; i+=2){
s.insert( 2 * (*ite) + 1);
s.insert( 3 * (*ite) + 1);
ite++;
}
for( i=1, ite = s.begin(); ite!=s.end() && i <= n; ite++, i++){
cout<<*ite<<' ';
}
cout<<endl;
return 0;
}
#include <iostream>
using namespace std;
int a[1000]={0}, b[1000]={0}, head_a = 1, head_b = 1, rear = 1, x = 1, n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cout << x << " ";
a[rear] = 2 * x + 1;
b[rear] = 3 * x + 1;
// cout << a[rear] << " " << b[rear] << " " << rear << endl;
rear++;
if (a[head_a] > b[head_b]) x = b[head_b], head_b++;
else if (a[head_a] < b[head_b]) x = a[head_a], head_a++;
else if (a[head_a] == b[head_b]) {
x = a[head_a];
head_a++;
head_b++;
}
}
return 0;
}