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

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

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

The 3n + 1 problem

Posted on 2006-06-10 00:41 mahudu@cppblog 閱讀(1345) 評論(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>
            国产精品国产馆在线真实露脸| 免费高清在线一区| 国产精品国产三级国产普通话蜜臀| 亚洲麻豆国产自偷在线| 最新国产精品拍自在线播放| 欧美国产一区二区在线观看| 99这里只有久久精品视频| 日韩视频―中文字幕| 国产精品成人av性教育| 欧美一级专区免费大片| 久久久久久电影| 日韩午夜电影av| 亚洲愉拍自拍另类高清精品| 国产主播喷水一区二区| 亚洲第一天堂无码专区| 欧美日韩在线另类| 久久九九免费视频| 欧美激情片在线观看| 亚洲欧美制服中文字幕| 久久精品一区中文字幕| 日韩午夜在线| 欧美在线观看一二区| 亚洲精品一二区| 午夜精品视频在线观看| 亚洲精品在线免费| 亚洲欧美高清| 亚洲美女中文字幕| 久久国产欧美精品| 一区二区欧美国产| 久久男人av资源网站| 亚洲一区精品电影| 久久综合999| 性xx色xx综合久久久xx| 欧美福利网址| 老鸭窝91久久精品色噜噜导演| 欧美日韩成人在线视频| 老巨人导航500精品| 国产精品理论片在线观看| 欧美高清视频一区| 国产欧美日本| 99亚洲一区二区| 亚洲青涩在线| 久久久国产精品亚洲一区| 亚洲欧美国产精品va在线观看| 美女脱光内衣内裤视频久久影院| 久久福利毛片| 欧美一区二视频在线免费观看| 欧美国产三区| 亚洲第一网站免费视频| 韩国一区电影| 欧美在线|欧美| 性做久久久久久久免费看| 欧美日韩国产片| 欧美夫妇交换俱乐部在线观看| 国产精品最新自拍| 这里只有精品丝袜| 一区二区三区四区精品| 欧美激情第二页| 亚洲成色www久久网站| 影音先锋亚洲一区| 久久精品亚洲一区二区| 久久久www成人免费毛片麻豆| 国产精品久在线观看| 亚洲新中文字幕| 亚洲资源av| 国产精品久久夜| 亚洲免费一在线| 欧美一区二区在线播放| 国产日本欧美一区二区| 性欧美大战久久久久久久免费观看| 亚洲欧美在线播放| 国产毛片久久| 久久久精品日韩| 欧美大片一区二区| 99这里只有精品| 欧美亚洲成人精品| 在线视频亚洲欧美| 久久精品国产一区二区三区| 国语精品中文字幕| 免费看黄裸体一级大秀欧美| 亚洲国产第一| 亚洲综合成人在线| 国产一本一道久久香蕉| 久久久久久网| 亚洲七七久久综合桃花剧情介绍| 这里只有精品在线播放| 国产麻豆91精品| 久久免费视频在线| 日韩视频中文字幕| 久久精品久久综合| 亚洲精品国产品国语在线app| 欧美日韩国产美女| 欧美在线国产| 亚洲人成高清| 久久国产婷婷国产香蕉| 亚洲激情成人| 国产精品三级久久久久久电影| 欧美制服丝袜第一页| 亚洲高清免费| 欧美在线免费观看| 亚洲精品欧美激情| 国产婷婷色一区二区三区四区| 美国成人毛片| 亚洲欧美日韩区| 亚洲国产视频直播| 久久精品国产免费看久久精品| 91久久精品网| 国产色综合天天综合网| 欧美女人交a| 久久免费少妇高潮久久精品99| 一二美女精品欧洲| 亚洲成人自拍视频| 久久久精品久久久久| 亚洲调教视频在线观看| 亚洲福利av| 国产午夜精品一区二区三区欧美| 欧美激情一区二区三区在线视频| 小辣椒精品导航| 最新日韩精品| 国产自产在线视频一区| 国产精品久久久久毛片大屁完整版 | 老鸭窝亚洲一区二区三区| 亚洲午夜性刺激影院| 在线国产精品播放| 国产日韩在线一区| 国产精品久久久久毛片大屁完整版| 麻豆精品视频在线| 久久久国产精彩视频美女艺术照福利 | 香蕉久久一区二区不卡无毒影院| 亚洲人成网站精品片在线观看 | 久久久亚洲国产美女国产盗摄| 亚洲一区二区黄| 亚洲美女区一区| 亚洲欧洲日韩女同| 欧美国产三区| 欧美成人一区二区三区| 久久亚洲综合| 麻豆成人综合网| 久久久久久久97| 久久久天天操| 老巨人导航500精品| 麻豆成人在线| 欧美高清视频www夜色资源网| 美女成人午夜| 欧美激情女人20p| 欧美激情精品久久久久久久变态| 欧美二区在线| 亚洲区中文字幕| 日韩一级黄色av| 亚洲午夜久久久| 午夜精品区一区二区三| 欧美在线播放高清精品| 久久久精品欧美丰满| 久色婷婷小香蕉久久| 蜜桃av噜噜一区二区三区| 欧美二区在线| 欧美日韩精品免费观看| 国产精品九九| 国产在线日韩| 亚洲精品裸体| 亚洲欧美国产一区二区三区| 午夜久久黄色| 欧美xx视频| av不卡在线看| 欧美在线观看视频| 免费久久99精品国产自| 欧美丝袜一区二区| 一区二区三区三区在线| 亚洲欧美日韩中文视频| 久久精品亚洲精品| 欧美久久久久| 国产欧美另类| 91久久精品国产91性色tv| 亚洲午夜视频在线观看| 久久婷婷综合激情| 亚洲精品一二区| 久久爱www.| 欧美日韩综合在线免费观看| 国内精品久久久久影院优| 日韩视频在线免费观看| 久久精品99无色码中文字幕| 欧美激情第8页| 午夜精品久久久| 欧美另类女人| 在线国产精品播放| 亚洲无线一线二线三线区别av| 久久久久久久综合| aⅴ色国产欧美| 裸体歌舞表演一区二区| 国产欧美日韩综合一区在线观看| 亚洲国产成人久久综合一区| 亚洲欧美日韩在线一区| 欧美激情亚洲自拍| 久久爱另类一区二区小说| 欧美特黄一级大片| 亚洲黄色毛片| 久热精品视频| 新片速递亚洲合集欧美合集| 欧美日韩一区二区免费视频| 91久久久精品|