火柴人java_Java的方法

动手动脑1:

package test;

public class sgg

{

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:以下程序有什么特殊之处?

在主函数下边还有两个static结构构成的成员方法。

72b8d68a6830568c3c3057109feffe1a.png

课后作业1:计算组合数

1)程序:package test;

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("reasult is:"+C);

}

public static int sgg(int n)//递归法计算阶乘

{

int s = 0;

if(n < 0)

System.out.println("错误!");

else if(n == 1||n == 0)

s = 1;

else

s = n * sgg(n -1);

return s;

}

}

76e13ebc81eb4d6cd5f256949b9df080.png

)程序:

package test;

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("请输n:");

int n = in.nextInt();

System.out.print("请输入k:");

int k = in.nextInt();

int f = sgg(n,k);

System.out.println(" 公式计算结果为: "+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;

}

}

结果输出:

8a3196c567792213e9cf4618a6d09624.png

3)程序:package test;

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("请输入n:");

int n = in.nextInt();

System.out.print("请输入k:");

int k = in.nextInt();

System.out.println("组合数结果为:"+sgg(n,k));

in.close();

}

public static int sgg(int m,int n)

{

if(m<0||n<0||m

return 0;

if(m==n)

return 1;

if(n==1)

return m;

return sgg(m-1,n)+sgg(m-1,n-1);

}

}

输出结果:

09a618ebe4301cac2658b493279982d5.png

课后作业2:汉诺塔用JAVA实现

程序:

// TowersOfHanoi.java

// Towers of Hanoi solution with a recursive method.

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

输出结果:

026d76c061e05a139149d953ff329ca1.png

课后作业3:判断字符串是否为回文数

程序:

package test;

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

if(str.charAt(i)==sb.charAt(i))

n++;

}

if(n==str.length())

System.out.println(str+"是回文!");

else

System.out.println(str+"不是回文!");

}

}

结果输出:

5fe6e3a973cdd1b15bd99fe59c8e5129.png

b6f5b9e2c1af705662757a2827f71247.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值