[UVA][JAVA][Sanner] 371 - Ackermann Functions@Morris' Blog|PChome Online 人新台
2012-03-05 18:59:37| 人717| 回0 | 上一篇 | 下一篇

[UVA][JAVA][Sanner] 371 - Ackermann Functions

0 收藏 0 0 站台

Ackermann Functions 

An Ackermann function has the characteristic that the length of the sequence of numbers generated by the function cannot be computed directly from the input value. One particular integer Ackermann function is the following:

displaymath32

This Ackermann has the characteristic that it eventually converges on 1. A few examples follow in which the starting value is shown in square brackets followed by the sequence of values that are generated, followed by the length of the sequence in curly braces:

 [10] 5 16 8 4 2 1 {6} [13] 40 20 10 5 16 8 4 2 1 {9} [14] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 {17} [19] 58 29 88 44 22 ... 2 1 {20} [32] 16 8 4 2 1 {5} [1] 4 2 1 {3}

Input and Output

Your program is to read in a series of pairs of values that represent the first and last numbers in a closed sequence. For each closed sequence pair determine which value generates the longest series of values before it converges to 1. The largest value in the sequence will not be larger than can be accomodated in a 32-bit Pascal LongInt or C long. The last pair of values will be 0, 0. The output from your program should be as follows:

Between L and H, V generates the longest sequence of S values.

Where:

L = the lower boundary value in the sequence

H = the upper boundary value in the sequence

V = the first value that generates the longest sequence, (if two or more values generate the longest sequence then only show the lower value) S = the length of the generated sequence.

In the event that two numbers in the interval should both produce equally long sequences, report the first.

Sample Input

 1 20 35 55 0 0

Sample Output

Between 1 and 20, 18 generates the longest sequence of 20 values. Between 35 and 55, 54 generates the longest sequence of 112 values.


import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int L, H, i, tmp;
while(keyboard.hasNext()) {
L = keyboard.nextInt();
H = keyboard.nextInt();
if(L > H) {
tmp = L;
L = H;
H = tmp;
}
if(L == 0 && H == 0)
break;
int lOngest= 0, v = 0;
for(i = L; i <= H; i++) {
tmp = function((long)i);
if(tmp > longest) {
lOngest= tmp;
v = i;
}
}
System.out.printf("Between %d and %d, %d generates the longest sequence of %d values.\n",
L, H, v, longest);
}
}
public static int function(long n) {
int length = 0;
do {
if((n&1) != 0)
n = n*3+1;
else
n /= 2;
length++;
} while(n != 1);
return length;
}
}

台: Morris
人(717) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][JAVA][System.in.read] 371 - Ackermann Functions
此分上一篇:[UVA][JAVA][Biginteger] 619 - Numerically Speaking

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86