"Java", "Find the Law" Gym-101243b-hanoi Tower

Source: Internet
Author: User

Test instructions: Give you a classic Hanoi recursive program and ask you at least a few steps to make the same number of plates on the three pillars. (Ensure that the initial plate number can be divisible by 3)

Law: ANS (n) =2^ (2*n/3-1) +t (N/3).

T (1) =0.

T (n) =

T (n-1) +1,n is even

T (n-1) *4+2,n is odd.

Java file read and write mainly the following two methods, the second, the output format more arbitrary, more practical:

import java.util.*;import java.io.*;import java.math.*;p ublic class Main{ public static void Main (string[] argc) {biginteger[] t=new biginteger[305]; Biginteger[] pw=new biginteger[305];t[1]=biginteger.zero;for (int i=2;i<=100;++i) {if (i%2==0) {T[i]=t[i-1].add ( Biginteger.one);} Else{t[i]=t[i-1].multiply (biginteger.valueof (4l)). Add (Biginteger.valueof (2l));}} pw[0]=biginteger.one;for (int i=1;i<=300;++i) {pw[i]=pw[i-1].multiply (biginteger.valueof (2l));} Scanner cin = new Scanner (system.in); try{cin=new Scanner (New FileInputStream ("Input.txt"));} catch (Exception e) {}int n=cin.nextint (); Cin.close ();/*pw[2*n/3-1].add (T[N/3]). toString () */file file=new File (" Output.txt "), Try{bufferedwriter bf=new bufferedwriter (new PrintWriter (file)); Bf.append (Pw[2*n/3-1].add (T[N/3)). ToString ()); Bf.close ();} catch (Exception e) {}}} 
Import java.util.*;import java.io.*;import java.math.*;p ublic class main{public static void Main (string[] argc) { Biginteger[] T=new biginteger[305]; Biginteger[] pw=new biginteger[305];t[1]=biginteger.zero;for (int i=2;i<=100;++i) {if (i%2==0) {T[i]=t[i-1].add ( Biginteger.one);} Else{t[i]=t[i-1].multiply (biginteger.valueof (4l)). Add (Biginteger.valueof (2l));}} pw[0]=biginteger.one;for (int i=1;i<=300;++i) {pw[i]=pw[i-1].multiply (biginteger.valueof (2l));} Scanner cin = new Scanner (system.in); try{cin=new Scanner (New FileInputStream ("Input.txt"));} catch (Exception e) {}int n=cin.nextint (); Cin.close ();/*pw[2*n/3-1].add (T[N/3]). toString () *///file file=new File (" Output.txt "); Try{filewriter fw = new FileWriter (" Output.txt ", true); PrintWriter cout = new PrintWriter (FW); Cout.println (Pw[2*n/3-1].add (T[N/3)); Cout.flush ();//bufferedwriter bf=new BufferedWriter (new PrintWriter),//bf.append (Pw[2*n/3-1].add (T[N/3]). toString ());//bf.close (); catch (Exception e) {}}}

"Java" "Find the Law" Gym-101243b-hanoi Tower

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.