java课堂练习——方法

在这节Java课上的PPT中,验证了一些方法

随机代码

public class suiji
{
private static final int N = 200;
private static final int LEFT = 40;
private static final int RIGHT = 10000;
private static long x0 = 1L;
private long a = 1103515245L;
private long c = 12345L;
private long m = 2147483648L;
private long rand ( long r )
{
 r = ( r * a + c ) % m;//Xn+1=(aXn + c)mod m
 return r;
 }
private long little ( int a, int b, long rand )
{
 return a + rand % ( b - a + 1 );
}
private void recursion ( int count, long rand )
{
 if (count >= N)
{
  return;
}
 rand = rand (rand);
 long r = little (LEFT, RIGHT, rand);
 System.out.print (r + "\n");
 recursion (++count, rand);
}
public static void main ( String[] args )
{
 sgg recur = new sgg ();
 recur.recursion (0, x0);
}
}

2.请看以下代码,你发现了有什么特殊之处吗?

相同函数名,数据类型不同与调用的函数对应。

3.

计算组合数

import java.util.*;
public class sgg {
 public static void main(String[] args) {// TODO Auto-generated method stub
 Scanner in = new Scanner(System.in);
 System.out.print("please input :");
 int n = in.nextInt();
 System.out.print("please input:");
 int k = in.nextInt();
 
 int C = sgg(n)/(sgg(k)*sgg(n - k));
 System.out.println("result is :"+C);
 }
 
 public static int sgg(int n)
 {
  int s = 0;
  if(n < 0)
   System.out.println("error!");
  else if(n == 1||n == 0)
   s = 1;
 
  else
   s = n * sgg(n -1);
  return s;
  }
 }

import java.util.Scanner;
public class sgg {
 public static void main(String[] args) {// TODO Auto-generated method stub
  
  Scanner in = new Scanner(System.in);
  System.out.print("please input:");
  int n = in.nextInt();
  System.out.print("please input :");
  int k = in.nextInt();

  int f = sgg(n,k);
  System.out.println(" reaselt is: "+f);
  }

 public static int sgg(int n,int k)
 {
  int f = 0;
  if(n == 1||k == 0||n == k)
   f = 1;
  else
   f = sgg(n - 1,k - 1) + sgg(n - 1,k);
  return f;
  }
 }

 

import java.util.Scanner;
public class ditui {
 public static void main(String[] args) {// TODO Auto-generated method stub
  Scanner in = new Scanner(System.in);
  System.out.print("please input:");
  int n = in.nextInt();
  System.out.print("please input :");
  int k = in.nextInt();
  System.out.println("reasult is:"+sgg(n,k));
  in.close();
  }
 public static int sgg(int m,int n)
 {
  if(m<0||n<0||m<n)
   return 0;
  if(m==n)
   return 1;
  if(n==1)
   return m;
  return sgg(m-1,n)+sgg(m-1,n-1);
  }
 }

 

汉诺塔用JAVA实现

public class TowersOfHanoi
{
   // recursively move disks between towers
   public static void solveTowers( int disks, int sourcePeg, 
      int destinationPeg, int tempPeg )
   {
      // base case -- only one disk to move
      if ( disks == 1 )
      {
         System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );
         return;
      } // end if

      // recursion step -- move (disk - 1) disks from sourcePeg
      // to tempPeg using destinationPeg
      solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );

      // move last disk from sourcePeg to destinationPeg
      System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );

      // move ( disks - 1 ) disks from tempPeg to destinationPeg
      solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
   } // end method solveTowers

   public static void main( String[] args )
   {
      int startPeg = 1; // value 1 used to indicate startPeg in output
      int endPeg = 3; // value 3 used to indicate endPeg in output
      int tempPeg = 2; // value 2 used to indicate tempPeg in output
      int totalDisks = 3; // number of disks
      
      // initial nonrecursive call: move all disks.
      solveTowers( totalDisks, startPeg, endPeg, tempPeg );
   } // end main
} // end class TowersOfHanoi

输出结果:

 判断字符串是否为回文数

import java.util.*;
public class sgg {
public static void main(String[] args) {
// TODO Auto-generated method stub
 String str="";
 System.out.println("请输入一个字符串:");
 Scanner in=new Scanner(System.in);
 str=in.nextLine();
 StringBuffer sb=new StringBuffer(str);
 sb.reverse();
 int n=0;
 for(int i=0;i<str.length();i++){
  if(str.charAt(i)==sb.charAt(i))
   n++;
  }
 
 if(n==str.length())
  System.out.println(str+"是回文!");
 else
  System.out.println(str+"不是回文!");
 }
}

 

posted @ 2016-10-16 10:16  20153143  阅读(176)  评论(0编辑  收藏  举报