码迷,mamicode.com
首页 > 其他好文 > 详细

队列2--集合

时间:2017-07-09 16:06:45      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:src   com   turn   img   clu   image   name   head   --   

队列2--集合

一、心得

 

二、题目及分析

 求1, 2x+1和3x+1队列的第100个数 

 

三、代码及结果

 

 1 //求1, 2x+1和3x+1队列的第100个数 
 2 //依次把队列中的2x+1和3x+1都取了 
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int q[200]; 
 7 
 8 int main(){
 9     int head=1;
10     q[head]=1;
11     int two=1,three=1;
12     //求第100个数 
13     for(int i=1;i<=100;i++){
14         int x2=2*q[two]+1;
15         int x3=3*q[three]+1; 
16         if(x2==x3){
17             q[++head]=x2;
18             two++,three++;
19         }
20         else if(x2<x3){
21             q[++head]=x2;
22             two++;
23         }
24         else{
25             q[++head]=x3;
26             three++;
27         }
28     } 
29     cout<<q[100]<<endl;
30     
31         
32     return 0;
33 } 

技术分享

前十项

技术分享

队列2--集合

标签:src   com   turn   img   clu   image   name   head   --   

原文地址:http://www.cnblogs.com/Renyi-Fan/p/7141427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!