Java Action Brain 2

Source: Internet
Author: User

1. How can I call a function without static in the Java class ?

If this function is static, the memory will be loaded directly during program initialization, and all method contents in this method must be static, otherwise the static method will be used to refer to the dynamic Variable. Method invocation:"class name. method"

If this function is dynamic, it is only actively loaded at the time of the call, and then the memory is Freed. Method invocation: You must instantiate a class first, and then call it by instantiating the class name + method.

2, observe the following code differences?

Indicates that the method is static, and that the method can be called with the class name in other Classes.

2, The combination of the source program:

(1) Recursive Combinatorial Number:

Import Java.util.scanner;public class Czuhe {public static void main (string[] Args) {System.out.print ("please Enter n:"); Suppresswarnings ("resource") Scanner sc=new Scanner (system.in); int n=sc.nextint (); System.out.print ("please Enter K:"), @SuppressWarnings ("resource") Scanner scan=new Scanner (system.in); int k=scan.nextint (); System.out.println (Cal (n,k));} public static int Cal (int n,int k) {if (k = = 0 | | N = = 0)      {          return 1;      }            If (N < K)      {          return 0;      }            if (N = = K)      {          return 1;      }            Return cal (n-1,k) + cal (n-1,k-1); }}

(2) using Yang Hui triangle to find the combination number:

Import java.util.Scanner; @SuppressWarnings ("unused") public class Czuhe {public static void main (string[] Args) { System.out.print ("please Enter N:"), @SuppressWarnings ("resource") Scanner sc=new Scanner (system.in); int n=sc.nextint (); System.out.print ("please Enter K:"), @SuppressWarnings ("resource") Scanner scan=new Scanner (system.in); int k=scan.nextint (); System.out.println (Cal (n,k));} public static int Cal (int n,int k) {if (k = = 0 | | N = = 0)      {          return 1;      }       int C=0;int a=1;  int S=math.min (K, n-k); For (int i=1;i<=s;i++) {c=a* (n-i+1)/(i); a=c;}          return c;     }}



(3) calculation using the combination number Formula:

Importjava.util.Scanner; @SuppressWarnings ("unused") public classCzuhe { public Static voidmain (string[] Args) {System.out.print ("please Enter n:"); @SuppressWarnings ("resource") Scanner SC=NewScanner (system.in); intn=Sc.nextint (); System.out.print ("please Enter k:"); @SuppressWarnings ("resource") Scanner Scan=NewScanner (system.in); intk=Scan.nextint (); System.out.println (jiecheng (N)/(jiecheng (K) *jiecheng (nK)); }     public Static intJiecheng (intN) {intS=1; if(N = = 1)              {                  return1; }                   Elses=jiecheng (N-1) *N; returns; }}

3, palindrome Number of procedures:

Import java.util.Scanner; @SuppressWarnings ("Unused") public classHuiwen {intn= -;  public Static voidmain (string[] Args) {System. out. Print ("Please enter a string:"); @SuppressWarnings ("Resource") Scanner SC=NewScanner (System.inch); String N=Sc.nextline (); if(fun (N) = =1) System. out. Print ("It's a palindrome string! "); ElseSystem. out. Print ("not a palindrome string! "); }             public Static  intfun (String Str) {intlen=Str.length (); {              if(len = =0|| Len = =1)                  return    1; CharFirst=str.charat (0); CharLast=str.charat (len-1); if(first!=last )return    0; returnFun (str.substring (1, len-1)); }      }}

4, linear with congruential generate random number

Importjava.util.Date; Importjava.util.concurrent.atomic.AtomicLong;  public classRandom {Private FinalAtomiclong seed; Private Final Static LongMultiplier = 0x5deece66dl; Private Final Static LongAddend = 0xBL; Private Final Static LongMask = (1L << 48)-1; Private Static volatile LongSeeduniquifier = 8682522807148012L;  public voidSrandseed (LongT) {           this. Seed.set (t); }             publicRandom () { this(++seeduniquifier +System.nanotime ());      System.out.println (seed.get ()); }             publicRandom (LongSeed) {           this. Seed =NewAtomiclong (0L);      Srandseed (seed); }            intRandomintBits) {          LongOldseed =Seed.get (); LongNextseed = (oldseed * multiplier + addend) &mask; return(int) (nextseed >>> (48-bits)); }             public Static voidmain (string[] Args) {Random R=NewRandom (); R.srandseed (NewDate (). getTime ()); System.out.println (r.random (32)); }  }  

5. Hanoi procedure

Import Java.util.scanner;public class Towersofhanoi {public        static void solvetowers (int disks, int sourcepeg,       i NT destinationpeg, int temppeg)   {            if (disks = = 1)      {         System.out.printf ("\n%d-to-%d", sourcepeg, des tinationpeg);         return;      }                  Solvetowers (disks-1, sourcepeg, temppeg, destinationpeg);            System.out.printf ("\n%d-to-%d", sourcepeg, destinationpeg);           Solvetowers (disks-1, temppeg, destinationpeg, sourcepeg);   }    public static void main (string[] Args)   {   System.out.print ("please Enter the number of plates to be moved:"); @SuppressWarnings ("resource") Scanner sc=new Scanner (system.in); @SuppressWarnings ("unused") int  n=sc.nextint ();     Solvetowers (N, 1, 2, 3);        } }

  

Java Action Brain 2

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.