Java经典算法(二)
说明:共有四十题。本部分为第二部分,共十题。
11、题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
package com.remoa.algorithm.day06;
/**
* 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
* @author Remoa
*
*/
public class NoRepetitionNumber {
public static void algorithm(){
int result;
System.out.println("能组成" + 4 * 3 * 2 + "个互不相同且无重复数字的三位数,分别是:");
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
for(int k = 1; k <= 4; k++){
if(i != j && j != k && k != i){
result = i * 100 + j * 10 + k;
System.out.print(result + " ");
}
}
}
}
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
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,求应发放奖金总数?
package com.remoa.algorithm.day06;
import java.util.Scanner;
/**
* 题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?
* @author Remoa
*
*/
public class Bonus {
public static double algorithm(double profit){
double bonus = 0.0;
if(profit <= 10){
bonus = profit * 0.1;
}else if(profit <=20){
bonus = (profit - 10) * 0.075 + 10 * 0.1;
}else if(profit <= 40){
bonus = (profit - 20) * 0.05 + 10 * 0.1 + 10 * 0.075;
}else if(profit <= 60){
bonus = (profit - 40) * 0.03 + 10 * 0.1 + 10 * 0.075 + 20 * 0.05;
}else if(profit <= 100){
bonus = (profit - 60) * 0.015 + 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03;
}else{
bonus = (profit - 100) * 0.01 + 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015;
}
return bonus * 10000;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入利润金额:");
double profit = scanner.nextDouble();
double bonus = algorithm(profit / 10000);
System.out.println("奖金为:" + bonus);
scanner.close();
}
}
运行结果:
13、题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
package com.remoa.algorithm.day07;
/**
* 题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
* @author Remoa
*
*/
public class AnInteger {
public static void algorithm(){
for(int i = -100; i <= 10000; i++){
if(Math.sqrt(i + 100) % 1 == 0 && Math.sqrt(i + 168) % 1 == 0){
System.out.println(i);
}
}
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
14、题目:输入某年某月某日,判断这一天是这一年的第几天?
package com.remoa.algorithm.day07;
import java.util.Scanner;
/**
* 题目:输入某年某月某日,判断这一天是这一年的第几天?
* @author Remoa
*
*/
public class WhichDay {
public static int algorithm(int year, int month, int day){
int[] arrayMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int count = 0;
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
arrayMonth[1]++;
}
for(int i = 0; i < month - 1; i++){
count += arrayMonth[i];
}
return count + day;
}
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年月日");
int year = scanner.nextInt();
int month = scanner.nextInt();
int day = scanner.nextInt();
System.out.println("这一天是这一年的第" + algorithm(year, month, day) + "天");
scanner.close();
}
}
运行结果:
15、题目:输入三个整数x,y,z,请把这三个数由小到大输出。
package com.remoa.algorithm.day08;
import java.util.Scanner;
/**
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
* @author Remoa
*
*/
public class MaxToMin {
public static void algorithm(int x, int y, int z){
if(x > y){
if(y > z){
System.out.println(x + " " + y + " " + z);
}else{
System.out.println(x + " " + z + " " + y);
}
}else{
if(x > z){
System.out.println(y + " " + x + " " + z);
}else{
System.out.println(y + " " + z + " " + x);
}
}
}
public static void main(String []args){
System.out.println("请输入三个整数x,y,z:");
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
algorithm(x, y, z);
scanner.close();
}
}
运行结果:
16、题目:输出9*9口诀。
package com.remoa.algorithm.day08;
/**
* 题目:输出9*9口诀。
* @author Remoa
*
*/
public class MultiplicationTable {
public static void algorithm(){
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
System.out.print(i + " * " + j + " = " + i * j + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
图16.1 乘法口诀表运行结果
17、题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
package com.remoa.algorithm.day09;
/**
* 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
* @author Remoa
*
*/
public class Peaches {
public static void algorithm(){
int count = 1;
for(int i = 1; i < 10; i++){
count = (count + 1) * 2;
}
System.out.println("第一天共摘了有" + count + "个桃子");
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
图17.1 猴子偷桃问题运行结果
18、题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
package com.remoa.algorithm.day09;
/**
* 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
* @author Remoa
*
*/
public class TableTennis {
public static void algorithm(){
for(char i = 'x'; i <= 'z'; i++){
for(char j = 'x'; j <= 'z'; j++){
if(i != j){
for(char k = 'x'; k <= 'z'; k++){
if(i != k && j != k){
if(i != 'x' && k != 'x' && k != 'z'){
System.out.println("a, b, c的对手分别为:" + i + ", " + j + ", " + k);
}
}
}
}
}
}
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
19、题目:打印出如下图案(菱形)
*
***
*****
*******
*****
***
*
package com.remoa.algorithm.day10;
/**
* 题目:打印出如下图案(菱形)
*
***
*****
*******
*****
***
*
* @author Remoa
*
*/
public class PrintDiamond {
public static void algorithm(){
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4 - i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 2 * i - 1; k++){
System.out.print("*");
}
System.out.println();
}
for(int i = 4; i > 1; i--){
for(int j = 1; j <= 5 - i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 2 * i - 3; k++){
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
algorithm();
}
}
运行结果:
图19.1 菱形打印运行结果
package com.remoa.algorithm.day10;
import java.text.DecimalFormat;
/**
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
* @author Remoa
*
*/
public class Sum {
public static double algorithm(){
double a1 = 2, a2 = 3;
double b1 = 1, b2 = 2;
double sum = 2.0 / 1 + 3.0 / 2;
for(int i = 3; i <= 20; i++){
double a3 = a1 + a2;
a1 = a2;
a2 = a3;
double b3 = b1 + b2;
b1 = b2;
b2 = b3;
sum += a3 / b3;
}
return sum;
}
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(algorithm()));
}
}
运行结果: