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

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

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

The 3n + 1 problem

Posted on 2006-06-10 00:41 mahudu@cppblog 閱讀(1330) 評論(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国产精品久久久| 欧美日韩一区二区三区在线 | 久久精品视频免费| 中文日韩在线| 国产精品久久久91| 午夜精品一区二区三区在线播放| 亚洲最新视频在线播放| 国产精品chinese| 欧美一区二区三区四区高清 | 激情婷婷久久| 国产精品自在线| 欧美一二三视频| 亚洲一区精品电影| 国产精品一区二区三区久久| 一区二区三区日韩欧美| 一区二区三区免费观看| 国产精品羞羞答答| 久久久青草青青国产亚洲免观| 亚洲视频精品| 亚洲视频一区二区在线观看| 国产精品乱子久久久久| 欧美专区亚洲专区| 久久久蜜桃精品| 日韩亚洲欧美一区二区三区| 亚洲免费大片| 国内视频一区| 最新日韩欧美| 国产精品一区视频| 欧美成人国产一区二区| 欧美/亚洲一区| 这里只有精品电影| 午夜免费日韩视频| 亚洲二区视频在线| av成人国产| 国模私拍视频一区| 亚洲精品视频一区二区三区| 国产精品永久免费视频| 欧美福利视频| 国产精品毛片va一区二区三区 | 亚洲欧美日韩专区| 尤物在线精品| 99re6热只有精品免费观看| 国产精品一区免费视频| 亚洲第一免费播放区| 欧美午夜在线| 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲人成人一区二区三区| 国产精品家庭影院| 欧美黄在线观看| 国产亚洲精品久久久久久| 亚洲高清一区二区三区| 国产欧美精品一区二区色综合 | 亚洲精品日韩激情在线电影| 国产欧美一区二区视频| 亚洲人午夜精品免费| 精品51国产黑色丝袜高跟鞋| 一区二区三区免费网站| 亚洲精品日韩久久| 久久成人在线| 羞羞视频在线观看欧美| 欧美精品久久一区二区| 免费看黄裸体一级大秀欧美| 国产精品尤物| 欧美一区二区久久久| 欧美在线短视频| 国产精品乱码一区二区三区| 久久精品盗摄| 欧美激情一区二区三区成人| 亚洲一区二区在线免费观看| 国产精品亚洲欧美| 美女在线一区二区| 一区二区精品在线| 免费亚洲电影在线| 性色av一区二区三区红粉影视| 狠狠干成人综合网| 亚洲国产老妈| 欧美日韩在线播放三区四区| 久久精视频免费在线久久完整在线看| 亚洲高清网站| 欧美一级电影久久| 欧美中文日韩| 中国亚洲黄色| 亚洲国产精品传媒在线观看| 久久成人羞羞网站| 久久精品综合一区| 性高湖久久久久久久久| 亚洲第一伊人| 欧美体内谢she精2性欧美| 亚洲黄色小视频| 欧美激情精品久久久久久免费印度| aa亚洲婷婷| 欧美www视频在线观看| 免费久久99精品国产自| 亚洲高清视频的网址| 久久一二三区| 欧美黄在线观看| 一区二区三区中文在线观看| 亚洲国产高清aⅴ视频| 亚洲区中文字幕| 一本久久知道综合久久| 亚洲视频你懂的| 欧美一区二区免费视频| 久久精品伊人| 亚洲激情二区| 美日韩在线观看| 日韩视频一区二区三区| 亚洲综合日韩| 欧美综合激情网| 国产精品自拍三区| 伊人成人开心激情综合网| 亚洲精品免费网站| 亚洲精品影视在线观看| 亚洲资源av| 亚洲一区不卡| 久久色在线播放| 亚洲手机成人高清视频| 亚洲人成7777| 久久一区中文字幕| 国产人成精品一区二区三| 欧美不卡在线视频| 久久综合色婷婷| 国产欧美激情| 免费不卡在线观看| 欧美一区二区三区四区在线| 你懂的国产精品| 国产欧美日韩高清| 亚洲国产成人91精品| 在线国产亚洲欧美| 欧美午夜精品理论片a级按摩| 亚洲欧美激情一区二区| 一区二区三区导航| 国产真实乱偷精品视频免| 欧美激情在线狂野欧美精品| 亚洲欧美在线播放| 免费成人高清| 亚洲毛片视频| 亚洲人午夜精品| 欧美成人亚洲| 欧美一区二区三区四区在线观看地址| 亚洲精品三级| 国产热re99久久6国产精品| 欧美mv日韩mv亚洲| 欧美亚洲一区二区在线| 99ri日韩精品视频| 亚洲国产第一| 欧美成人午夜免费视在线看片| 午夜欧美不卡精品aaaaa| 99香蕉国产精品偷在线观看| 精品不卡一区| 国产亚洲欧美一区在线观看| 欧美色欧美亚洲高清在线视频| 玖玖玖免费嫩草在线影院一区| 亚洲尤物视频网| 午夜精品久久久久久久久久久久久| 久久蜜臀精品av| 亚洲免费影视第一页| 亚洲精品婷婷| 亚洲第一精品福利| 一区在线观看| 狠狠爱综合网| 韩国福利一区| 久久亚洲色图| 国产女优一区| 久久久国际精品| 久久免费视频网| 欧美日韩一区免费| 亚洲激情啪啪| 亚洲日本成人女熟在线观看| 欧美在线免费播放| 久久超碰97人人做人人爱| 欧美国产日产韩国视频| 亚洲国内自拍| 欧美专区在线观看| 亚洲人永久免费| 欧美视频手机在线| 亚洲欧美日韩综合国产aⅴ| 久久婷婷色综合| 亚洲精品视频免费在线观看| 性色av香蕉一区二区| 另类激情亚洲| 日韩午夜电影在线观看| 性色一区二区| 欧美成人精品高清在线播放| 欧美激情中文字幕乱码免费| 欧美成人一二三| 欧美日韩一区二区高清| 欧美中文字幕在线| 欧美v国产在线一区二区三区| 欧美激情精品久久久久久免费印度|