Basic programming questions (11~12)

11. Permutation and combination of numbers

Question: There are four numbers 1, 2, 3, and 4. How many different three-digit numbers can be formed without repeated numbers? How many are they?

11.1 Program analysis

  • Implementation ideas
    1. Define variables as counters
    2. Use loops to realize the traversal of hundreds, tens, and single digits
    3. Use conditional statements to distinguish the traverse data
    4. Print the results

Violent solution

11.2 Code implementation

public class Test {
    
    

	public static void main(String[] args) {
    
    
		//1、计数器
		int count = 0;
		//2、循环遍历各位数据
		for (int i = 1; i <= 4; i++) {
    
    
			for (int j = 1; j <= 4; j++) {
    
    
				for (int k = 1; k <= 4; k++) {
    
    
					//3、数据判断(百、十、个位三个数不相同)
					if (k != i && k != j && i != j) {
    
    
						count++;
						//4、打印组合结果
						System.out.println(i+""+j+""+k);
						//或者System.out.println(i*100+j*10+k);
					}
				}
			}
		}
		System.out.println("1,2,3,4组合成三位数的个数为:"+count);	
	}
}

12. Give out bonuses

Topic: Bonuses issued by companies are based on profit commissions. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is more than 100,000 yuan, and when the profit is less than 200,000 yuan, the portion of less than 100,000 yuan will be a 10% commission, which is higher than 100,000 yuan For the part of, the commission can be 7.5%; when it is between 200,000 and 400,000, the part of more than 200,000 yuan can be commissioned 5%; when it is between 400,000 and 600,000, the part of more than 400,000 yuan can be commissioned 3% ; Between 600,000 and 1 million, the portion above 600,000 yuan will receive a commission of 1.5%. When it is above 1 million yuan, the portion exceeding 1 million yuan will be commissioned at 1%. Enter the monthly profit from the keyboard and pay upon request. Total bonus?

12.1 Program analysis

  • Realize the idea
    1. Define the required variables and enter the appropriate data
    2. Judge the data and calculate the bonus
    3. Print the result

12.2 Code implementation

import java.util.Scanner;

public class test {
    
    

	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		//1、定义变量及输入合适数据
		double profit;//利润
		double bonus = 0;//奖金
		do {
    
    
			System.out.print("请输入当月利润,求应发放奖金总数,利润单位(万元):");
			profit = input.nextInt();
			if (profit <= 0) {
    
    
				System.out.println("当月利润数据不合格,请重新输入");
			}
		} while (profit <= 0);
		//2、判断数据并计算奖金
		if (profit <= 10) {
    
    
			bonus = profit * 0.10;
		} else if (profit > 10 && profit <= 20) {
    
    
			bonus = 1.0 + (profit - 10) * 0.075;
		} else if (profit > 20 && profit <= 40) {
    
    
			bonus = 1.75 + (profit - 20) * 0.05;
		} else if (profit > 40 && profit <= 60) {
    
    
			bonus = 2.75 + (profit - 40) * 0.03;
		} else if (profit > 60 && profit <= 100) {
    
    
			bonus = 3.95 + (profit - 60) * 0.015;
		} else {
    
    
			bonus = profit * 0.01;
		}
		//3、打印结果
		System.out.printf("当月利润为" + profit + "万元,应发放奖金总数" + bonus+"万元。");
	}
}

Guess you like

Origin blog.csdn.net/Bennettgxd/article/details/114195078