动手动脑及课后作业02

1.SquareIntTest.java中不用static如何还能使用Square函数

解决方法:

2、

代码:

import java.util.Scanner;

public class Suiji {

	public static void main(String[] args) {
		System.out.print("请输入要输出的随机数个数:");
		Scanner s = new Scanner(System.in);
		int num = s.nextInt();
		
		//生成随机数
		int seed = (int)(Math.random() * 1000);
		int a = (int)(Math.random() * 1000);
		int c = (int)(Math.random() * 1000);
		int m = (int)(Math.random() * 1000);
		
		for(int i = 0;i < num;i++)
		{	
			seed = (a * seed + c) % m;
			System.out.print(seed + " ");
		}
	}

}

 结果:

 

3.动手动脑

方法的重载,方法名相同,但引用的参数类型不同,调用的方法就会不同。

4.课后作业1

(1)代码:

import java.util.Scanner;

public class Zuhe1 {

	public static void main(String[] args) {
		System.out.print("请输入组合数的上标k,下标n:");
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();

		if(k > n)
			System.out.println("输入有误!");
		else
		{
			int result = Cal(n)/(Cal(k) * Cal(n - k));
			System.out.println("结果为:" + result);
		}
	}
	
	public static int Cal(int x)
	{
		if(x == 0 || x == 1)
			return 1;
		else
			return x * Cal(x - 1);
	}

}

  运行结果截图:

(2)代码:

import java.util.Scanner;

public class Zuhe2 {

	public static void main(String[] args) {
		System.out.print("请输入组合数的上标k,下标n:");
		Scanner s = new Scanner(System.in);
		int k = s.nextInt();
		int n = s.nextInt();

		if(k > n)
			System.out.println("输入有误!");
		else
		{
			int result = Cal(n, k);
			System.out.println("结果为:" + result);
		}
	}
	
	public static int Cal(int x,int y)
	{
		if(y == 0 || x == y)
			return 1;
		else
		{
			int k = 1,j = 1,i;  
			for(i = x;i > x - y;i--)  
			{   
				k = k * (i) / j;
				j++;  
	        }  
			return k;  	
		}
	}

}

结果截图:

5.课后作业2:递归编程解决汉诺塔问题。用Java实现

代码:

package T3;
import java.util.Scanner;

public class Hanoi {

	public static void main(String[] args) {
		System.out.println("汉诺塔有三个座A、B、C,初始状态为A上有一些盘子,要将盘子移动到C上。");
		System.out.print("请输入盘子数:");
		Scanner N = new Scanner(System.in);
		int n = N.nextInt();
		
		System.out.print("移动方法为:");
		solve('A','B','C',n);
	}
	
	public static void solve(char start,char temp,char end,int num)
	{
		if(num == 1)
			System.out.printf("%c --> %c  ",start,end);
		else
		{
			solve(start,end,temp,num - 1);
			System.out.printf("%c --> %c  ",start,end);
			solve(temp,start,end,num - 1);	
		}
	}

}

  结果:

6.课后作业3:使用递归方式判断某个字串是否是回文( palindrome )

代码:

package demo;

import java.util.Scanner;

public class IsHuiwen {

	public static void main(String[] args) {
		System.out.print("请输入字符串:");
		Scanner S = new Scanner(System.in);
		String s = S.nextLine();
		
		if(Huiwen(s) == 1)
			System.out.println(s + "是回文数!");
		else
			System.out.println(s + "不是回文数!");
	}
	
	public static int Huiwen(String str)
	{
		if(str.length() == 0||str.length() == 1)
			return 1;
		else
		{
			char First = str.charAt(0);
			char End = str.charAt(str.length() - 1);
			if(First != End)
				return 0;
		}
		return Huiwen(str.substring(1,str.length() - 1));
	}

}

结果截图:

转载于:https://www.cnblogs.com/fylove/p/5966024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值