本文为作者自己的练习成果,用于记录学习的历程。题目来源见下文,程序代码为原创。作者初学Java,水平较低,文章内容仅供参考。
题目来源:https://www.cnblogs.com/VellBibi/p/3500671.html
【程序1】RabbitMonthNum.java
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析:递归;m月份兔子对数 = (m-1)月份兔子对数 + (m-3)月份兔子对数
public class RabbitMonthNum {
private static int month;
//求某月兔子对数的方法
public static long RabbitCal(int m) {
if (m==1) {
return 1;
}
else if (m==2) {
return 1;
}
else if (m==3) {
return 2;
}
else {
return RabbitCal(m-1) + RabbitCal(m-3);
}
}
public static void main(String[] args) {
System.out.println("请输入你想知道的兔子数量的月份");
Scanner scanner = new Scanner(System.in);
month = scanner.nextInt();
System.out.println("月份:"+month+" 兔子数量:"+RabbitCal(month)+" 对");
scanner.close();
}
}
【程序2】FindPrimeNumber.java
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
public class FindPrimeNumber {
private static int n = 0;
public static void main(String[] args) {
for (int i = 101; i < 201; i++) {
for (int j = 2; j < i; j++) {
if (i % j != 0) {
if (j == (i - 1)) {
System.out.println(i+" ");
n++;
}
else {
continue;
}
}
else {
break;
}
}
}
System.out.println("101-200之间素数的个数为:"+n+" 个");
}
}
【程序3】FindDaffodilNumber.java
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
public class FindDaffodilNumber {
public static int cube(int m) {
return m*m*m;
}
public static void main(String[] args) {
System.out.println("100-999之间的“水仙花数”为:");
for (int i = 100; i < 1000; i++) {
int a = i % 100 % 10;//个位
int b = i % 100 / 10;//十位
int c = i / 100;//百位
int sum = cube(a)+cube(b)+cube(c);
if (sum == i) {
System.out.println(sum);
}
}
}
}
【程序4】PrimeFactorization.java
题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
public class PrimeFactorization {
public static void primefactory(int m) {
for (int i = 2; i < m; i++) {
if (m % i == 0) {
System.out.print(i+"*");
m = m / i;
i = 1;
continue;
}
else if (i == (m-1)) {
System.out.print(m);
}
else {
continue;
}
}
}
public static void main(String[] args) {
int k;
System.out.println("请输入需执行分解质因数操作的数:");
Scanner scanner = new Scanner(System.in);
k = scanner.nextInt();
PrimeFactorization pf = new PrimeFactorization();
System.out.print(k+" 分解质因数的结果为: ");
pf.primefactory(k);
scanner.close();
}
}
【程序5】ConditionOperator.java
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class ConditionOperator {
public static void main(String[] args) {
System.out.println("请输入你的分数");
Scanner scanner = new Scanner(System.in);
int point = scanner.nextInt();
//条件运算符
String level = point>89?"A":(point>=60?"B":"C");
System.out.println("您成绩的等第是: "+level);
scanner.close();
}
}
【程序6】GcdTest.java
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗转相除法,最大公因数gcd(a,b)=gcd(b,a mod b)。
求最大公因数(bigDivisor):
1.m,n中较大的数对较小的数求余。(假设m>n)
2.把n赋给m,把求余的结果赋给n。
3.重复上述操作直到求余的结果为0。此时上一次求余过程中被求余的值就是最大公因数。
求最小公倍数(muitiple):
最小公倍数满足 multiple=m * n / bigDivisor(此处m,n为初值)。
public class GcdTest {
public static void main(String[] args) {
System.out.println("请输入两个正整数:");
Scanner scanner = new Scanner(System.in);
int input1 = scanner.nextInt();
int input2 = scanner.nextInt();
int bigDivisor;
int multiple;
int temp;
//判断m,n大小
int m = (input1>input2)?input1:input2;
int n = (input1<input2)?input1:input2;
//求最大公因数
do{
temp = n;
n = m % n;
m = temp;
}while(n != 0);
bigDivisor = temp;
multiple = input1*input2/bigDivisor;
System.out.println("所输入的两个正整数的最大公因数为:"
+bigDivisor+"最小公倍数为:"+multiple);
scanner.close();
}
}
【程序7】StatisticChar.java
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:用一个数组存放字符串中的所有元素,对比数组中每个元素在ASK码的氛围,来确定它的符号类别。
char字符ASK码范围:
(1)数字0-9:48~57
(2)字母A-Z:65-90 字母a-z:97-122
(3)空格:32
public class StatisticChar {
public static void main(String[] args) {
System.out.println("请输入一行字符");
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();
int num = 0;//数字个数
int letter = 0;//字母个数
int space = 0;//空格个数
char[] arr = sentence.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i]>47&&arr[i]<58) {
num++;
}
else if ((arr[i]>64&&arr[i]<91)||(arr[i]>96&&arr[i]<123)) {
letter++;
}
else if (arr[i]==32 ) {
space++;
}
}
System.out.println("输入的字符中含有数字:"+num+"个"+" 字母:"
+letter+"个"+" 空格:"+space+"个");
scanner.close();
}
}
【程序8】AddTest.java
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246。
程序分析:把a+aa+aaa+aa…a看作一个第一项为a,第二项为aa,第n项为aa…(n个)a的数列,找出其中规律:An=2*10^(n-1)+A(n-1),前n项和Sn=A1+A2+A3+…+An。
用递归的方法求An,用for循环求Sn。
public class AddTest {
private static int a;
private static int b;
//求An的值
public static double showAn(int n) {
if (n>1) {
return a * (Math.pow(10, (n-1))) + showAn(n-1);
}
else {
return a;
}
}
//求前n项和Sn
public static double sumAn(int m) {
double sum = 0;
for (int i = 1; i <= m; i++) {
sum += showAn(i);
if (i<m) {
System.out.print(showAn(i)+"+");
}
else {
System.out.print(showAn(i));
}
}
System.out.print("=");
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要执行运算的一个数字");
a = scanner.nextInt();//运算数字
System.out.println("请输入有几个数相加");
b = scanner.nextInt();//运算次数
System.out.print("s=");
System.out.print(sumAn(b));
scanner.close();
}
}
【程序9】PerfectNumber.java
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数。
程序分析:完数的定义:除自身以外的因数的和等于自身。(1不是完数)
public class PerfectNumber {
//判断一个数是否为完数
public void SearchFactor(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
sum += n / i;
}
}
if (sum == n) {
System.out.println(n);
}
else {
sum = 0;
}
}
//查找1000以内的所有完数
public void AllFactor() {
for (int i = 2; i <= 1000; i++) {
SearchFactor(i);
}
}
public static void main(String[] args) {
PerfectNumber pn = new PerfectNumber();
System.out.println("1000以内的完数有:");
pn.AllFactor();
}
}
【程序10】BallTest.java
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
程序分析:第10次落地=反弹9次 第n次反弹高度=100/2^n 总距离=(前n次反弹高度和)*2+100
public class BallTest {
private static double height = 100;//起始高度
private static double jumph;//反弹高度
private static double distance = 100;//总经过距离
//第几次反弹多高
public static double BallJump(int n) {
jumph = height / Math.pow(2,n);
return jumph;
}
//求总经过距离
public static double BallDistance(int m) {
for (int i = 1; i <= m; i++) {
distance += 2 * BallJump(i);
}
return distance;
}
public static void main(String[] args) {
System.out.println("球第10次落地时,共经过 "+BallDistance(9)
+"米,"+"第10次反弹 "+BallJump(10)+"米高。");
}
}
【程序11】Threedigits.java
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:用3个for循环实现个位、十位、百位上数字的遍历,再排除其中有数字重复的数。
public class Threedigits {
public static void main(String[] args) {
int num = 0;
int sum = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
for (int k = 1; k < 5; k++) {
if (i != j && j !=k && i != k) {
num = i * 100 + j * 10 + k;
System.out.print(num + " ");
sum++;//计算个数
if (sum % 10 == 0) {
System.out.println();
}
}
}
}
}
System.out.println();
System.out.print("能组成"+sum+"个三位数.");
}
}
【程序12】
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
程序分析:利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
public class SalaryTest {
public static void main(String[] args) {
System.out.println("请输入月利润I:");
Scanner scanner = new Scanner(System.in);
int profit = scanner.nextInt();//单位:万元
double percentage = 0;//提成
if (profit <= 10) {
percentage = profit * 0.1;
}
else if (profit > 10 && profit < 20) {
percentage = 10 * 0.1 + (profit-10) * 0.075;
}
else if (profit >= 20 && profit < 40) {
percentage = (profit - 20) * 0.05 + 10 * 0.1 + 10 * 0.075;
}
else if (profit >= 40 && profit < 60) {
percentage = (profit - 40) * 0.03 + 20 * 0.05 + 10 * 0.1
+ 10 * 0.075;
}
else if (profit >= 60 && profit < 100) {
percentage = (profit - 60) * 0.015 + 20 * 0.03 + 20 * 0.05
+ 10 * 0.1 + 10 * 0.075;
}
else if (profit > 100) {
percentage = (profit - 100) * 0.01 + 40 * 0.015 + 20 * 0.03
+ 20 * 0.05 + 10 * 0.1 + 10 * 0.075;
}
System.out.println("应发的奖金总额为:"+percentage+"万元");
}
}
【程序13】FindNumber.java
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:a+100=x^2 a+100+168=y^2
public class FindNumber {
private static int a;
private static long range;//在多少范围内判断
public static void main(String[] args) {
System.out.println("请输入判断的范围:");
Scanner scanner = new Scanner(System.in);
range = scanner.nextLong();
System.out.print("在"+range+"范围内"+"a可以是:");
for (a = 0; a <= range; a++) {
for (int i = 10; i <= range; i++) {
if (i * i == a + 100) {
for (int j = i; j <= range; j++) {
if (j * j == a + 100 + 168) {
System.out.print(a+" ");
}
}
}
}
}
System.out.print("这些完全平方数");
}
}
【程序14】ShowWitchDay.java
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:闰年:可以被4整除但不能被100整除;可以被400整除.
public class ShowWitchDay {
private static int year;
private static int month;
private static int day;
private static int sumdays = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
year = scanner.nextInt();
System.out.println("请输入月份:");
month = scanner.nextInt();
System.out.println("请输入某日:");
day = scanner.nextInt();
for (int i = 1; i < month; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
sumdays += 31;
}
else if (i == 2 && ((year % 4 == 0 && year % 100 != 0)||year % 400 == 0)) {
sumdays += 29;
}
else if (i == 2 && (year % 4 != 0 || year % 400 != 0)) {
sumdays += 28;
}
else {
sumdays += 30;
}
}
sumdays += day;
System.out.println(year+"年"+month+"月 "+day+"日"+"是这一年的 第"+sumdays+"天");
}
}
【程序15】CompareSizes.java
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:比较大小后按大小交换两个数 ,保持z<y<x
public class CompareSizes {
private static int x;
private static int y;
private static int z;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数x:");
x = scanner.nextInt();
System.out.println("请输入整数y:");
y = scanner.nextInt();
System.out.println("请输入整数z:");
z = scanner.nextInt();
int temp;
if (x - y > 0) {
if (y - z > 0) {
}
else {
temp = y;
y = z;
z = temp;
if (x - y > 0) {
}
else {
temp = x;
x = y;
y = temp;
}
}
}
else {
temp = x;
x = y;
y = temp;
if (y - z > 0) {
}
else {
temp = y;
y = z;
z = temp;
if (x - y > 0) {
}
else {
temp = x;
x = y;
y = temp;
}
}
}
System .out.print(z+" "+y+" "+x);
}
}
【程序16】MultiplicationTable.java
题目:输出9*9口诀。