![]() |
#2
rjsp2013-11-15 10:55
|

#include<iostream>//代码实现的目标是求质因子只包含2,3或5的数,规定1也在这些数中,所以前十项数为1,2,3,4,5,6,8,9,10,12,
#include<queue>
using namespace std;
typedef pair<unsigned long,int> node_type;
int main()
{
unsigned long result[1500];
priority_queue<node_type,vector<node_type>,greater<node_type> >Q;
Q.push(make_pair(1,2));
for(int i=0;i<1500;i++)
{
node_type node=Q.top();
Q.pop();
switch(node.second)//在switch这里我就卡住了,代码的结果是对的,但是我想不通result[]数组里面的值是排序好了的
{
case 2:Q.push(make_pair(node.first*2,2));//第一次循环时node.first==1,之后入队列,node.first也应该==2,依次类推,之后node.first的值应该是4
case 3:Q.push(make_pair(node.first*3,3));//8,16才对啊,求大神批评。。。
case 5:Q.push(make_pair(node.first*5,5));
}
result[i]=node.first;
}
int n;
cin>>n;
while(n>0)
{
cout<<result[n-1]<<endl;
cin>>n;
}
return 0;
}
#include<queue>
using namespace std;
typedef pair<unsigned long,int> node_type;
int main()
{
unsigned long result[1500];
priority_queue<node_type,vector<node_type>,greater<node_type> >Q;
Q.push(make_pair(1,2));
for(int i=0;i<1500;i++)
{
node_type node=Q.top();
Q.pop();
switch(node.second)//在switch这里我就卡住了,代码的结果是对的,但是我想不通result[]数组里面的值是排序好了的
{
case 2:Q.push(make_pair(node.first*2,2));//第一次循环时node.first==1,之后入队列,node.first也应该==2,依次类推,之后node.first的值应该是4
case 3:Q.push(make_pair(node.first*3,3));//8,16才对啊,求大神批评。。。
case 5:Q.push(make_pair(node.first*5,5));
}
result[i]=node.first;
}
int n;
cin>>n;
while(n>0)
{
cout<<result[n-1]<<endl;
cin>>n;
}
return 0;
}