青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

技術,瞎侃,健康,休閑……

mahu@cppblog 人類的全部才能無非是時間和耐心的混合物
posts - 11, comments - 13, trackbacks - 0, articles - 12
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

The 3n + 1 problem

Posted on 2006-06-10 00:41 mahudu@cppblog 閱讀(1341) 評論(3)  編輯 收藏 引用 所屬分類: C/C++

Background

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

The Problem

Consider the following algorithm:

1.	input n

2. print n

3. if n = 1 then STOP

4. if n is odd then tex2html_wrap_inline44

5. else tex2html_wrap_inline46

6. GOTO 2

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

The Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.

The Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

Solution ?

#include <iostream>

using namespace std;

?

int cycle(intm)

{

?? int i = 1;

?? while (m != 1){

????? if(m%2)

??????? m = m*3 + 1;

????? else

??????? m /= 2;

????? i++;

?? }

?? return i;

}??

?

int main()

{

?? int m,n,max,temp;

?? int mOriginal,nOriginal;

?? int i;

?

?? while (cin >> m >> n){

????? mOriginal = m;

????? nOriginal = n;

????? if (m > n){

??????? temp = m;

??????? m = n;

??????? n = temp;

????? }

?

????? max = cycle(m);

????? for (i = m+1; i <= n; i++){

??????? temp = cycle(i);

??????? if (temp > max){

?????????? max = temp;

??????? }

????? }?

????? cout << mOriginal << " " << nOriginal << " " << max << endl;

?? }

?? return 0;

}

Feedback

# re: The 3n + 1 problem  回復  更多評論   

2007-11-14 20:34 by 無意中看到
你這個程序屬于很難通過的,基本上會碰到超時問題
輸入: 1 1000000
看你多牛的計算機3秒能搞出來
這是典型的dp問題,暴力是不好用的

# re: The 3n + 1 problem  回復  更多評論   

2008-10-18 17:17 by TaiwanNo.1
/*
這個可以以0.7 sec完成
*/
#include <stdio.h>

int compute(int a)
{
int cnt = 1;
while(a > 1)
{
a & 0x01 ? (a = (a<<1) + a + 1) : (a >>= 1);
++cnt;
}
return cnt;
}


int a, b, c;
int i, j;
int main(void)
{

while(0 < scanf("%d %d", &i, &j))
{
c = 0;
i < j ? (a = i, b = j) : (a = j, b = i);
while(a <= b)
{
int tmp = compute(a++);
if(tmp > c)
c = tmp;
}
printf("%d %d %d\n", i, j, c);
}
return 0;
}

# re: The 3n + 1 problem  回復  更多評論   

2011-01-16 13:39 by UDHeart
@TaiwanNo.1
...我就是這樣寫的,沒有這么快
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            黑人中文字幕一区二区三区| 国产精品国产亚洲精品看不卡15 | 午夜精品免费| 欧美国产专区| 久久一二三国产| 国产综合色一区二区三区| 先锋a资源在线看亚洲| 亚洲视频免费看| 久久久精品久久久久| 一区二区欧美精品| 国产精品久久久久aaaa樱花| 亚洲一区二区精品| 一区二区激情| 国产欧美日韩91| 久久久久免费| 卡通动漫国产精品| 亚洲免费成人av电影| 亚洲欧洲另类国产综合| 噜噜噜噜噜久久久久久91| 一本大道久久精品懂色aⅴ| 久久久久久电影| 在线日韩中文| 亚洲欧美清纯在线制服| 亚洲视频欧洲视频| 国产精品看片你懂得| 亚洲综合丁香| 亚洲欧美成人精品| 一区二区视频免费在线观看| 欧美粗暴jizz性欧美20| 欧美精品在欧美一区二区少妇| 一区二区三区久久网| 亚洲午夜三级在线| 国产一区二区欧美日韩| 欧美丰满少妇xxxbbb| 欧美精品一区二区三| 午夜一区不卡| 免费成人黄色片| 亚洲午夜精品一区二区| 欧美一级黄色录像| 亚洲精品视频啊美女在线直播| 一本色道久久综合亚洲精品不卡| 久久精品国产亚洲5555| 一区电影在线观看| 国产精品九九| 久久久99国产精品免费| 免费高清在线视频一区·| 亚洲视频欧美在线| 久久精品免费看| 亚洲夜晚福利在线观看| 欧美中文在线字幕| 亚洲午夜视频| 噜噜噜噜噜久久久久久91| 亚洲一区二区三区久久| 久久综合五月| 欧美综合二区| 欧美视频在线观看 亚洲欧| 美女黄网久久| 久久久久久高潮国产精品视| 欧美久久九九| 老妇喷水一区二区三区| 国产精品剧情在线亚洲| 亚洲国产毛片完整版| 国产欧美一区在线| 一级日韩一区在线观看| 日韩一级免费| 久久一区视频| 久久蜜桃av一区精品变态类天堂| 欧美日韩一区二区视频在线| 女人香蕉久久**毛片精品| 国产欧美欧洲在线观看| 99re国产精品| 一本色道久久88综合亚洲精品ⅰ| 久久艳片www.17c.com| 久久福利影视| 国产欧美精品久久| 亚洲一区二区av电影| 亚洲一区二区欧美| 国产精品成人国产乱一区| 亚洲精品久久久久中文字幕欢迎你 | 亚洲一区在线免费观看| 久久一区激情| 久久婷婷丁香| 国产在线观看精品一区二区三区| 国模精品一区二区三区色天香| 久久亚洲精品一区二区| 国产伦精品一区二区三区高清| 日韩一级在线| 亚洲视频在线观看网站| 欧美理论视频| 亚洲国产日韩欧美一区二区三区| 在线观看视频亚洲| 老鸭窝91久久精品色噜噜导演| 裸体女人亚洲精品一区| 在线精品国精品国产尤物884a| 欧美在线亚洲在线| 久久精品91| 国产在线精品成人一区二区三区| 欧美一区免费视频| 国产精品一区二区在线观看网站 | 欧美搞黄网站| 亚洲精品女人| 国产精品成人国产乱一区| 亚洲伊人久久综合| 欧美诱惑福利视频| 国外成人免费视频| 麻豆成人av| 亚洲剧情一区二区| 欧美一级专区| 在线不卡中文字幕播放| 欧美大片免费久久精品三p | 久久亚洲二区| 亚洲精品国产精品国自产在线| 欧美成人午夜激情| 日韩一级在线观看| 欧美一区二区三区婷婷月色 | 国产精品日韩精品欧美精品| 香蕉久久夜色| 亚洲高清不卡在线观看| 99国产精品久久久久老师| 欧美日韩一区二区三区在线观看免| 亚洲一区美女视频在线观看免费| 久久在线免费观看| 99v久久综合狠狠综合久久| 国产精品麻豆va在线播放| 欧美专区日韩专区| 99av国产精品欲麻豆| 久久久久在线| 亚洲素人在线| 亚洲福利视频三区| 国产精品一区二区久久精品| 久久一区激情| 亚洲一区免费看| 91久久国产综合久久| 亚洲国产精品久久91精品| 亚洲国产精品嫩草影院| 亚洲欧美三级伦理| 亚洲欧洲在线一区| 国产一区二区三区在线观看网站| 欧美国产91| 久久se精品一区二区| 亚洲精品在线观看免费| 免费观看久久久4p| 久久精品人人做人人爽电影蜜月 | 亚洲激情一区二区| 久久青草久久| 欧美视频一区二区| 欧美激情国产日韩| 亚洲综合三区| 亚洲区中文字幕| 国产日韩欧美一区二区三区四区| 浪潮色综合久久天堂| 亚洲综合视频在线| 日韩视频免费看| 亚洲电影中文字幕| 美日韩精品视频免费看| 午夜视频久久久久久| 一区二区三区视频在线观看| 在线观看av不卡| 国产亚洲精品美女| 国产精品久久久久婷婷| 欧美精选一区| 免费高清在线一区| 卡通动漫国产精品| 久久久精品一区| 欧美在线播放一区二区| 亚洲制服少妇| 一区二区三区精品视频在线观看| 亚洲人成人一区二区在线观看| 亚洲精品久久久久久久久久久久| 欧美精品综合| 欧美激情偷拍| 欧美不卡高清| 农夫在线精品视频免费观看| 欧美专区在线观看| 久久成人精品一区二区三区| 香蕉久久夜色精品国产| 午夜精品视频在线观看一区二区| 亚洲视频在线看| 亚洲主播在线| 欧美伊人久久大香线蕉综合69| 欧美一区日本一区韩国一区| 亚洲小说春色综合另类电影| 亚洲午夜av| 欧美亚洲综合在线| 久久精品国产91精品亚洲| 久久激情网站| 免费欧美在线视频| 欧美日韩精品一区二区在线播放| 欧美视频在线免费| 国产日韩精品一区| 在线观看欧美激情| 日韩视频一区二区在线观看 | 欧美三级黄美女| 欧美精品在线一区二区| 欧美性猛交xxxx乱大交蜜桃| 国产精品国产福利国产秒拍| 国产亚洲福利| 亚洲三级毛片| 亚洲男同1069视频| 久久中文字幕导航|