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

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

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

The 3n + 1 problem

Posted on 2006-06-10 00:41 mahudu@cppblog 閱讀(1344) 評論(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>
            久久综合中文字幕| 美乳少妇欧美精品| 欧美久久精品午夜青青大伊人| 在线精品国产成人综合| 蜜臀av一级做a爰片久久| 久久久99爱| 1024亚洲| 亚洲剧情一区二区| 国产精品国产馆在线真实露脸| 亚洲欧美bt| 欧美一区二区观看视频| 在线国产精品播放| 亚洲精品久久久久久下一站 | 欧美有码视频| 欧美一区二区三区在线免费观看| 国产最新精品精品你懂的| 牛夜精品久久久久久久99黑人| 麻豆成人精品| 亚洲一区欧美二区| 欧美一区免费| 亚洲精品在线电影| 亚洲欧美日韩综合国产aⅴ| 狠狠操狠狠色综合网| 欧美好吊妞视频| 国产精品久久二区| 免费不卡在线视频| 欧美午夜不卡| 另类尿喷潮videofree| 欧美日本国产视频| 欧美 日韩 国产在线| 亚洲欧美国产制服动漫| 久久久999国产| 亚洲一区二区精品视频| 久久久www成人免费无遮挡大片| 亚洲精品黄色| 欧美中文字幕在线观看| 夜夜爽99久久国产综合精品女不卡| 午夜精品久久久久久久99水蜜桃 | 亚洲高清视频一区二区| 亚洲精品乱码久久久久久日本蜜臀 | 久久久午夜电影| 亚洲小说欧美另类婷婷| 久久久av水蜜桃| 午夜精品久久久久久久| 欧美mv日韩mv国产网站| 久久久久久久性| 国产精品成av人在线视午夜片| 麻豆精品在线视频| 国产日韩一区在线| 中文高清一区| 一本一道久久综合狠狠老精东影业| 欧美中在线观看| 欧美一级视频一区二区| 欧美日韩国产免费| 亚洲高清毛片| 亚洲国产婷婷| 久久久久国产成人精品亚洲午夜| 午夜综合激情| 国产精品h在线观看| 91久久午夜| 亚洲国产欧美一区| 久久中文字幕一区| 免费亚洲网站| **性色生活片久久毛片| 久久不见久久见免费视频1| 午夜国产不卡在线观看视频| 欧美午夜精品理论片a级按摩| 亚洲人成7777| 日韩一级网站| 欧美日韩中文字幕日韩欧美| 日韩视频精品| 亚洲欧美日韩系列| 国产精品免费一区豆花| 亚洲欧美三级在线| 欧美一区二区三区四区在线观看地址 | 一区二区免费在线视频| 亚洲一区二区视频| 国产精品毛片a∨一区二区三区| 一区二区三区视频在线播放| 亚洲欧美电影在线观看| 国产农村妇女精品一区二区| 欧美一区午夜视频在线观看| 久久久高清一区二区三区| 黑人一区二区| 免费成人高清在线视频| 亚洲精品久久久久久一区二区| 日韩亚洲精品电影| 欧美亚男人的天堂| 欧美一级午夜免费电影| 模特精品裸拍一区| av不卡在线| 国产精品中文字幕在线观看| 久久er99精品| 欧美成人性生活| 中文亚洲字幕| 国产日韩一区二区三区在线| 久久久久综合| 日韩亚洲欧美成人| 久久国产天堂福利天堂| 亚洲精品国产视频| 国产精品久久久久影院亚瑟| 亚洲亚洲精品在线观看| 国产精品你懂的| 免费成人av| 亚洲免费影视| 亚洲第一偷拍| 欧美制服第一页| 亚洲精品免费电影| 国产亚洲综合在线| 欧美日韩在线精品一区二区三区| 午夜精彩国产免费不卡不顿大片| 欧美激情网友自拍| 欧美一级大片在线免费观看| 亚洲精品日韩在线观看| 国产一区二区在线观看免费| 欧美日本精品一区二区三区| 久久爱www久久做| 一区二区高清在线观看| 亚洲电影激情视频网站| 久久精品一区四区| 亚洲自拍电影| 99re热这里只有精品免费视频| 国产一区二区在线观看免费| 欧美先锋影音| 欧美国产日本高清在线| 久久久久久夜精品精品免费| 亚洲一区二区不卡免费| 亚洲美女中出| 亚洲人成人一区二区三区| 男人天堂欧美日韩| 麻豆成人综合网| 久久国产精品一区二区| 亚洲欧美视频在线观看视频| 亚洲理论在线观看| 亚洲精品乱码视频| 在线精品亚洲| 在线观看日韩国产| 精品91在线| 经典三级久久| 一区二区三区在线免费观看| 国产亚洲成av人在线观看导航| 国产精品国产三级国产a| 欧美日韩1区2区| 欧美极品一区| 欧美精品一区二区三区在线看午夜| 毛片基地黄久久久久久天堂| 老司机aⅴ在线精品导航| 久久九九有精品国产23| 久久久久久亚洲精品不卡4k岛国| 欧美在线观看一二区| 欧美一区二区三区在| 久久久.com| 玖玖精品视频| 欧美激情亚洲视频| 欧美精品一区三区在线观看| 欧美喷水视频| 欧美亚男人的天堂| 国产乱码精品一区二区三区忘忧草| 国产美女精品人人做人人爽| 国产日韩精品一区二区三区在线| 国产偷久久久精品专区| 国内精品久久久久影院优| 在线欧美一区| 亚洲精品一区二区在线观看| 日韩一级二级三级| 亚洲淫性视频| 久久精品人人做人人综合| 麻豆九一精品爱看视频在线观看免费| 欧美fxxxxxx另类| 亚洲精品乱码久久久久久久久| 日韩特黄影片| 欧美一二三视频| 看欧美日韩国产| 欧美色欧美亚洲高清在线视频| 国产精品看片资源| 在线欧美福利| 中文在线不卡| 久久人人爽国产| 亚洲精品国产系列| 午夜精品亚洲| 欧美成人亚洲成人| 国产伦精品一区二区三区照片91| 激情综合在线| 亚洲午夜精品久久久久久app| 久久久久久亚洲精品不卡4k岛国| 亚洲国产精品高清久久久| 亚洲自拍电影| 欧美成人福利视频| 国产欧美在线观看| 99在线热播精品免费| 欧美中文字幕在线| 亚洲精品国产精品乱码不99| 午夜国产精品影院在线观看| 欧美精品aa| 精品av久久久久电影| 亚洲专区一区二区三区| 欧美激情一区二区三区在线视频| 亚洲一区二区免费看| 欧美日韩ab片| 亚洲电影在线观看|