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

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

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

The 3n + 1 problem

Posted on 2006-06-10 00:41 mahudu@cppblog 閱讀(1328) 評論(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>
            久久综合网络一区二区| 午夜影院日韩| 亚洲日本欧美天堂| 欧美激情一区二区三区在线| 亚洲乱码国产乱码精品精98午夜| 亚洲激情电影在线| 国产精品毛片高清在线完整版| 欧美亚洲网站| 久久中文字幕一区二区三区| 亚洲精品中文字幕女同| 亚洲线精品一区二区三区八戒| 国产一区二区成人久久免费影院| 欧美xx视频| 国产精品久久二区| 美女视频黄免费的久久| 欧美日韩综合在线| 老司机亚洲精品| 欧美日韩三区四区| 久久久久久成人| 欧美日韩国产精品 | 国产精品高潮呻吟久久| 久久久久一区| 欧美日本一区二区高清播放视频| 欧美一区二视频| 欧美激情精品| 六月丁香综合| 国产精品视频1区| 亚洲成人资源| 国产一级揄自揄精品视频| 亚洲精品美女在线观看播放| 国产在线精品成人一区二区三区 | 欧美高清视频在线 | 久久伊人精品天天| 欧美诱惑福利视频| 欧美日韩一区二区免费在线观看 | 久久永久免费| 国产麻豆91精品| 亚洲卡通欧美制服中文| 亚洲第一精品夜夜躁人人爽| 亚洲欧美国产高清| 99这里只有精品| 欧美mv日韩mv国产网站| 久久综合成人精品亚洲另类欧美| 国产精品久久久久久一区二区三区| 亚洲福利视频二区| 怡红院精品视频在线观看极品| 亚洲自拍偷拍一区| 亚洲自拍高清| 国产精品第十页| 日韩亚洲一区二区| aa级大片欧美三级| 欧美久久久久久久| 最新69国产成人精品视频免费| 一色屋精品视频免费看| 久久精品国产999大香线蕉| 亚洲欧美国产一区二区三区| 欧美日韩一区在线观看视频| 99成人在线| 亚洲综合好骚| 国产精品久久久久久久一区探花| 在线一区观看| 先锋a资源在线看亚洲| 国产精品一区二区黑丝| 亚洲欧美日本另类| 久久久久国色av免费观看性色| 国产中文一区二区三区| 久久久久国产精品一区| 欧美成人免费全部| 亚洲欧洲另类| 欧美日韩国产一级| 正在播放日韩| 久久精品网址| 在线看成人片| 欧美另类亚洲| 亚洲视频综合| 久久久久久尹人网香蕉| 亚洲国产精品久久久久久女王| 欧美大片18| 这里只有精品视频| 久久久国产精品一区二区三区| 国内精品久久久久影院色 | 欧美96在线丨欧| 亚洲久久一区二区| 欧美亚洲综合久久| 一区二区三区在线观看国产| 欧美国产视频在线观看| 亚洲一区二区三| 久久天天躁狠狠躁夜夜爽蜜月| 91久久国产综合久久91精品网站| 欧美金8天国| 午夜欧美精品| 亚洲精品久久| 久久精品在线| 日韩性生活视频| 国产色产综合色产在线视频| 欧美成人精品1314www| 亚洲天堂免费观看| 欧美国产视频日韩| 欧美一区二区免费视频| 亚洲精品乱码| 国产午夜精品在线| 欧美日韩美女在线观看| 久久国产精品毛片| 中文精品99久久国产香蕉| 免费精品99久久国产综合精品| 亚洲综合精品一区二区| 亚洲第一区在线观看| 国产农村妇女精品一二区| 欧美大片专区| 久久网站热最新地址| 亚洲一区二区不卡免费| 最新国产成人av网站网址麻豆| 久久xxxx精品视频| 亚洲视频狠狠| 最近中文字幕mv在线一区二区三区四区| 国产精品久久久久婷婷| 欧美二区不卡| 久久手机免费观看| 欧美一区二区私人影院日本 | 久久久噜噜噜久久中文字免| 亚洲午夜电影| 亚洲精品视频在线看| 国产尤物精品| 国产一区二区日韩精品| 国产精品黄视频| 欧美性久久久| 欧美午夜三级| 欧美色另类天堂2015| 欧美日韩国产精品| 欧美日韩精品三区| 欧美人与禽性xxxxx杂性| 欧美大片在线影院| 欧美精品成人| 欧美区在线播放| 欧美日本精品| 欧美日韩视频| 国产精品久久久久影院色老大| 欧美日韩国产首页| 欧美日韩一区二区三区免费看| 欧美精品色网| 欧美日韩在线精品一区二区三区| 欧美日本久久| 国产精品99免视看9| 国产精品视频99| 国产美女精品| 亚洲国产精品国自产拍av秋霞| 伊人久久综合| 欧美日韩国产123| 欧美四级在线| 国产精品性做久久久久久| 国产精品一区二区久久国产| 国产精品一区在线播放| 国产欧美精品一区aⅴ影院| 国产亚洲精品一区二区| 在线成人黄色| 亚洲毛片一区| 午夜久久99| 蜜臀久久久99精品久久久久久| 欧美黑人多人双交| 亚洲人成在线播放| 亚洲自拍都市欧美小说| 久久久91精品国产一区二区三区| 久久亚洲精品中文字幕冲田杏梨 | 最近看过的日韩成人| 99riav久久精品riav| 欧美一区二区三区的| 免费永久网站黄欧美| 欧美性色视频在线| 国产一区成人| 一区电影在线观看| 久久人人97超碰精品888| 亚洲三级视频在线观看| 亚洲欧美综合另类中字| 欧美www视频| 国产欧美在线视频| 日韩五码在线| 久久免费精品视频| 一本久久a久久精品亚洲| 久久精品国产第一区二区三区最新章节 | 欧美片在线播放| 国产午夜精品福利| 一区二区三区 在线观看视| 久久久久久久一区二区三区| 最新国产の精品合集bt伙计| 先锋影音久久| 国产精品jizz在线观看美国| 亚洲高清二区| 久久精品免费电影| 一区二区国产精品| 免费观看不卡av| 国产日韩精品在线播放| 在线视频你懂得一区| 免费高清在线视频一区·| 亚洲图片在区色| 欧美激情免费观看| 影音先锋亚洲电影| 欧美在线视频免费| 亚洲一区二区成人在线观看| 欧美日韩国产123| 亚洲精品一区二区三区av|