Java 期中作业解析

Java 期中作业解析

0、一些示例

获取终端用户的一行输入并原样输出

// file name: demo1.java
import java.util.Scanner;

public class demo1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String s = input.nextLine();
		System.out.println(s);
		input.close();
	}
}

将字符串分解为字符数组

// file name: demo2.java

public class demo2 {
	public static void main(String[] args) {
		String s = "Hello,Human.";
		char[] c = s.toCharArray();
	}
}

判断整数与字符串是否对称

// 函数方法

static boolean 整数对称(int n) {
	int len = 0;
	while (n/Math.pow(10,++len)>=1);

	boolean is = true;
	for (int i=0;i<len/2;i++) {
		if (((n/(int)Math.pow(10,i))%10)!=((n/(int)Math.pow(10,(len-i-1)))%10)) {
			is = false;
		}
	}

	return is;
}
static boolean 字符串对称(String s) {
	int len = s.length();

	boolean is = true;
	for (int i=0;i<len/2;i++) {
		if (s.charAt(i)!=s.charAt(len-i-1)) {
			is = false;
		}
	}

	return is;
}

1、编程实现:根据以下函数关系,对输入的x值计算输出对应的y值。

x的值 对应y的值
x<0 0
0<=x<10 x
10<=x<20 0.5*x+18
x>=20 100
// file name: firstSubject.java
import java.util.Scanner;

public class firstSubject {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double x, y;
		
		x = input.nextInt();
		if (x<0) {
			y = 0;
		} else if (x<10) {
			y = x;
		} else if (x<20) {
			y = 0.5*x+18;
		} else {
			y = 100;
		}
		System.out.println(y);
		input.close();
	}
}

2、编写程序计算1!+2!+3!+...+n!,并输出计算结果。

(要求:n从键盘输入,0<=n<=50,如果输入的n值不在此范围,提示再次输入)

// file name: secondSubject.java
import java.util.Scanner;

public class secondSubject {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while(true) {
			int n = input.nextInt();
			if (n<0|n>50) {
				System.out.println("n值不在范围[0,50]内,请重新输入。");
			} else {
				int p = 1;
				long s = 0;
				for (int i = 1;i<=n;i++) {
					p *= i;
					s += p;
				}
				System.out.println(s);
				break; // 退出循环
			}
		}
		input.close();
	}
}

3、已知圆周率PIE的计算公式为 \(\frac{\pi}{4}=\frac{1}{1}-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+...\) 。要求计算圆周率PIE值(精度为1e-6)。

PS:精度是最后一项的绝对值小于1e-6即abs(1/n)<1e-6。

// file name: thirdSubject.java

public class thirdSubject {
	public static void main(String[] args) {
		double s = 0.0;
		int sign = 1;
		double n = 1.0;
		while(!(Math.abs(1/n)<1e-6)) {
			s += 1/n*sign;
			n += 2;
			sign *= -1;
		}
		System.out.println(s*4);
	}
}

4、从键盘输入一行字符串(以换行符结束),要求分别统计里面英文字符的总个数和数字的总个数,并分别输出。

// file name: fourthSubject.java

import java.util.Scanner;

public class fourthSubject {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		char[] arr_c = input.nextLine().toCharArray();
		int str_n = arr_c.length;
		int word_n = 0, num_n = 0;
		
		while (str_n-->0) {
			char c=arr_c[arr_c.length-str_n-1];
			if (c>='a'&c<='z'|c>='A'&c<='Z') {
				word_n++;
			} else if (c>='0'&c<='9') {
				num_n++;
			}
		}
		System.out.println("英文字符的个数:"+word_n);
		System.out.println("数字字符的个数:"+num_n);
		input.close();
	}
}

5、查询水果价格

给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。
首先在屏幕上显示以下菜单:

[1] apple
[2] pear
[3] orange
[4] grape
[0] exit

用户可以输入编号1~4查询对应水果的单价,用户输入0即退出;输入其他编号,显示此水果没有出售。

// file name: fifthSubject.java

import java.util.Scanner;

public class fifthSubject {
	public static void main(String[] args) {
		boolean isLoop = true;
		Scanner input = new Scanner(System.in);
		
		System.out.println("[1] apple");
		System.out.println("[2] pear");
		System.out.println("[3] orange");
		System.out.println("[4] grape");
		System.out.println("[0] exit");
		
		while (isLoop) {
			int n = input.nextInt();
			switch (n) {
				case 0:
					isLoop = false;
					break;
				case 1: 
					System.out.println("price="+"3.00元/公斤");
					break;
				case 2: 
					System.out.println("price="+"2.50元/公斤");
					break;
				case 3: 
					System.out.println("price="+"4.10元/公斤");
					break;
				case 4: 
					System.out.println("price="+"10.20元/公斤");
					break;
				default:
					System.out.println("没有出售此种水果");
			}
		}
		input.close();
	}
}

6、输出1-9999之间所有的完数(原题质因数是错的),并统计总数。

完数:一个数等于它所有因子数的和。例如:6=1+2+3,且6能被1、2、3整除。

解题思路:对于数n,用所有的k(1<=k<=n-1)去除n,将能整除的k至累加起来与n比较,如果相等则n是完数,输出该数,统计总数加1。

// file name: sixthSubject.java

public class sixthSubject {
	public static void main(String[] args) {
		int total = 0; // 完数总数
		System.out.print("1-9999之间所有的完数: ");
		for (int n=1;n<=9999;n++) {
			int sum_k = 0;
			for (int k=1;k<=n-1;k++) {
				if (n%k==0) {
					sum_k += k;
				}
			}
			if (n==sum_k) {
				System.out.print(n+",");
				total++;
			}
		}
		System.out.println("\n数量: "+total);
	}
}

7、排序:键盘输入一个数字n表示需要排序的总个数,然后输入n个数字,输出排序结果。

(经典排序方法有:冒泡排序、选择排序、插入排序等)

// file name: seventhSubject.java
import java.util.Scanner;
public class seventhSubject {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("n: ");
		int n = input.nextInt();
		int[] arr_n = new int[n];
		System.out.print("arr_n: ");
		while (n-- > 0) {
			arr_n[arr_n.length - n - 1] = input.nextInt();
		}
		n = 0;
		System.out.println("排序前: ");
		do {
			System.out.print(arr_n[n] + ",");
		} while (++n < arr_n.length);
		bubbleSort(arr_n);
		// selectSort(arr_n);
		// insertSort(arr_n);
		System.out.println("\n排序后: ");
		for (int i = 0; i < arr_n.length; i++) {
			System.out.print(arr_n[i] + ",");
		}
		input.close();
	}

	public static void bubbleSort(int[] a) {
		int len = a.length;
		for (int i = 0; i < len - 1; i++) {
			for (int j = 0; j < len - 1 - i; j++) {
				if (a[j] > a[j + 1]) {
					// 交换两数位置
					int temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}

	public static void selectSort(int[] a) {
		int len = a.length;
		for (int i = 0; i < len; i++) { // 循环次数
			int value = a[i];
			int position = i;
			for (int j = i + 1; j < len; j++) { // 找到最小的值和位置
				if (a[j] < value) {
					value = a[j];
					position = j;
				}
			}
			a[position] = a[i]; // 进行交换
			a[i] = value;
		}
	}

	public static void insertSort(int[] a) {
		int len = a.length; // 单独把数组长度拿出来,提高效率
		int insertNum;
		for (int i = 1; i < len; i++) {
			insertNum = a[i];
			int j = i - 1; // 序列元素个数
			// 从后往前循环,将大于insertNum的数向后移动
			while ((j >= 0) && (a[j] > insertNum)) {
				a[j + 1] = a[j]; // 元素向后移动
				j--;
			}
			a[j + 1] = insertNum; // 找到位置,插入当前元素
		}
	}
}

8、输出100-100000之间所有的回文数。例如:121 131 141 1221 2552 12321 23432都是回文数。

问题分析:所谓回文数,即左右对称的数字。本题中可以分为3位数字、4位数字、5位数字。
(1)3位数,只要个位和百位上数字相同即为回文数。
(2) 4位数,要个位和千位数字相同,并且十位数字和百位数字相同。
(3) 5位数,以百位数字为界限,左右两边数字相同,其他高位数字以此类推。

实现1——整数取位

// file name: eighthSubject.java

public class eighthSubject {
	public static void main(String[] args) {
		for (int i=100;i<=100000;i++) {
			int count = 0, n = i;
			while (n>0) {
				n /= 10;
				count++;
			}
			count = (count>0)?count:1;
			int[] arr_i = new int[count];

			for (int j=0;j<count;j++) {
				n = i;
				int w = j+1;
				while (w>1) {
					n /= 10;
					w--;
				}
				arr_i[count-j-1] = n%10;
			}
			boolean is = true;
			for (int j=0;j<arr_i.length/2;j++) {
				if(arr_i[j]!=arr_i[arr_i.length-j-1]) {
					is = false;
				}
			}
			if (is) {
				System.out.println(i);
			}
		}
	}
}

实现2.字符数组

// file name: eightSubjectX.java

public class eighthSubjectX {
	public static void main(String[] args) {
		for (int i=100;i<=100000;i++) {
			char[] arr_c = String.valueOf(i).toCharArray();
			boolean is = true;
			for (int j=0;j<arr_c.length/2;j++) {
				if(arr_c[j]!=arr_c[arr_c.length-j-1]) {
					is = false;
				}
			}
			if (is) {
				System.out.println(i);
			}
		}
	}
}

9、打印倒三角图案,要求从键盘输入整数n作为行数

n=8

***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

规律

总行数 当前行数(从1开始) 左边空格数 星号数
n m m-1 2*(n-m)+1
//file name: ninthSubject.java

import java.util.Scanner;

public class ninthSubject {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int m=1;m<=n;m++) {
			for (int j=1;j<=m-1;j++) {
				System.out.print(" ");
			}
			for (int j=1;j<=2*(n-m)+1;j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		input.close();
	}
}

类似的打印正三角也很简单,按规律写代码即可:

规律

总行数 当前行数(从1开始) 左边空格数 星号数
n m n-m 2*m-1
// file name: temp.java
import java.util.Scanner;

public class temp {
	public static void main(String[] args) {
		// n m:1 n-m 2*m-1
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int m = 1; m <= n; m++) {
			for (int j = 1; j <= n - m; j++) {
				System.out.print(" ");
			}
			for (int j = 1; j <= 2 * m - 1; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		input.close();
	}
}

10、36人搬72块砖。男一人搬4块,女一人搬3块,小孩两人搬一块,一次性搬完,问有多少种搬法?分别输出男、女、小孩的人数。

思路很简单,三个外层 for 循环枚举所有可能,最内层放个 if 判断是否符合条件即可。

//file name: tenthSubject.java
public class tenthSubject {
	public static void main(String[] args) {
		int total = 0;
		final int sum = 72, n = 36;
		for (int a=0;a<=n;a++) {
			for (int b=0;b<=n-a;b++) {
				for (int c=0;c<=n-a-b;c++) {
					if ((sum-a*4-b*3-c/2.0==0)&(a+b+c==n)) {
						System.out.println(a+","+b+","+c+";");
						total++;
					}
				}
			}
		}
		System.out.println(total);
	}
}

更高效:

//file name: tenthSubjectX.java
public class tenthSubjectX {
	public static void main(String[] args) {
		int total = 0;
		final int sum = 72, people_n = 36;
		for (int a=0;a<=(sum/Math.max(Math.max(4.0, 3.0), 0.5));a++) {
			for (int b=0;(b*3+a*4)<=sum;b++) {
				for (int c=0;(b*3+a*4+c/2.0)<=sum;c++) {
					if ((sum-a*4-b*3-c/2.0==0)
					   &(a+b+c==people_n)) {
						System.out.println(a+","+b+","+c+";");
						total++;
					}
				}
			}
		}
		System.out.println(total);
	}
}

帮某同学纠正后的代码:

//file name: MyT.java
public class MyT {
	public static void main(String[] args) {
		int b ,g, c;
		int sum = 36;
		int z = 72;
		for (b=0;b<=18;b++) {                   // 72/4=18
			for (g=0;g<=24;g++) {               // 72/3=24
				for (c=0;c<=36;c+=2) {          // max:36
					if (b+g+c==sum) {           // 总人数为36
						if (b*4+g*3+c/2*1==z) { // 总共72块砖
							System.out.println("b"+b+"g"+g+"c"+c);
						}
					}
				}
			}
		}
	}
}

粗暴简单 >_<

// 部分代码
int t = 0;
for (int a=1;a<=36;a++)
    for (int b=1;b<=36;b++)
        for (int c=1;c<=36/2;c++)
            if (a+b+c*2==36&&a*4+b*2+c==72) t++;
System.out.println(t);
posted @ 2018-11-21 11:26  林博士  阅读(890)  评论(0编辑  收藏  举报