[Java] "Blue Bridge Cup" 10 programming questions and answers (2)

series of articles

[Java] "Blue Bridge Cup" 10 programming questions and answers (1)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130223115

[Java] "Blue Bridge Cup" 10 programming questions and answers (2)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130304773

[Java] "Blue Bridge Cup" 10 programming questions and answers (3)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130305068

[Java] "Blue Bridge Cup" 10 programming questions and answers (4)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130392388



foreword

I can abstract the whole world, but I cannot abstract you. Wants to make you a private constant so outside functions can't access you. I also want you to be a global constant so that I can call you throughout my lifetime. It's a pity that there is no such constant in the world, and I can't define you, because you are so specific in my heart.

Hello everyone, this column is the [Java] column, the "Blue Bridge Cup" part, for beginners or friends who are interested in algorithms. Mainly share basic programming questions, some interesting and novel algorithms, we must get used to mastering the ideas of solving problems, if you are interested in practical operations, you can pay attention to my [C# project] column.

This column will be continuously updated and improved. If you have any questions, you can private message me. If you are interested in this column, please pay attention to it, let's learn and make progress together.

Blue Bridge Cup 10 classic programming questions and answers (2)
insert image description here


1. Topic

1.1 [Procedure 1]

[Procedure 11] Title: There are 1, 2, 3, and 4 numbers, how many three-digit numbers that are different from each other and have no repeated numbers can be formed? How much are they?

1.2 [Procedure 2]

[Procedure 12] Title: The bonus issued by the company is based on the profit commission. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%. 7.5% of the cocoa commission; between 200,000 and 400,000, 5% for the part higher than 200,000 yuan; 3% for the part above 400,000 yuan between 400,000 and 600,000 ; When between 600,000 and 1 million, the portion higher than 600,000 can be commissioned at 1.5%. Total bonuses paid out?
1. Program analysis: Please use the number axis to divide and locate. Note that the bonus needs to be defined as a growing integer when defining.

1.3 [Procedure 3]

[Procedure 13] Title: An integer, adding 100 to it is a perfect square number, and adding 168 is another perfect square number, what is the number?

1.4 [Procedure 4]

[Procedure 14] Title: Enter a certain month and day in a certain year, and judge whether this day is the day of the year?

1.5 [Procedure 5]

[Procedure 15] Title: Input three integers x, y, z, please output these three numbers from small to large.
1. Program analysis: We find a way to put the smallest number on x, first compare x with y, if x>y, exchange the values ​​of x and y, and then compare x with z, if x >z, exchange the values ​​of x and z, so that x can be minimized.

1.6 [Procedure 6]

[Procedure 16] Title: Output 9*9 formulas.
1. Program analysis: consider rows and columns, a total of 9 rows and 9 columns, i controls the row, and j controls the column.

1.7 [Procedure 7]

[Procedure 17] Topic: Monkey eats peaches Problem: The monkey picked a few peaches on the first day, ate half of them immediately, and was not addicted to it. He ate one more and ate half of the remaining peaches the next morning. I ate one more. After that, I ate half and one of the remaining one from the previous day every morning. When I wanted to eat again in the morning on the 10th day, I saw that there was only one peach left. Find out how much you picked on the first day.
1. Program analysis: adopt the method of reverse thinking and infer from the back to the front.

1.8 [Procedure 8]

[Procedure 18] Title: Two table tennis teams compete with three players each. Team A has three players a, b, and c, and team B has three players x, y, and z. The list of matches has been drawn. Someone asked the players for the list of games. a says he doesn't compare with x, c says he doesn't compare with x, z, please program to find out the list of the three teams.

1.9 [Procedure 9]

【程序19】题目:打印出如下图案(菱形) 
   * 
  *** 
 ***** 
******* 
 ***** 
  *** 
   * 
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。

1.10 [Procedure 10]

[Procedure 20] Title: There is a sequence of scores: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13...Find the sum of the first 20 items of this sequence.
1. Program analysis: Please grasp the changing law of numerator and denominator.


Two, the answer

2.1 [Procedure 1]

public class ArrangementNumber {
    
    
	/**
	 * 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
	 */
	public static void main(String[] args) {
    
    
		int ct = 0;
		for (int i = 1; i < 5; i++) {
    
    
			for (int j = 1; j < 5; j++) {
    
    
				if (i == j)
					continue;
				for (int k = 1; k < 5; k++) {
    
    
					if (j == k || i == k)
						continue;
					ct++;
					System.out.print((i * 100 + j * 10 + k) + " ");
				}
			}
		}
		System.out.println("\n共有:" + ct + "个不同的三位数。");
	}

}


2.2 [Procedure 2]

import java.util.Scanner;
public class PrizeCommision {
    
    
	public static void main(String[] args) {
    
    
		long profit;
		long prize;
		Scanner sc=new Scanner(System.in);
		
		while(true){
    
    
			System.out.println("请输入利润:");
			profit=sc.nextLong();
			if(profit>0)break;
		}
		
		if(profit<=100000){
    
    
			prize=(long) (profit*.1);
		}else if(profit<=200000){
    
    
			prize=(long) (100000*.1+(profit-100000)*.075);
		}else if(profit<=400000){
    
    
			prize=(long) (100000*.1+100000*.075+(profit-200000)*.05);
		}else if(profit<=600000){
    
    
			prize=(long) (100000*.1+100000*.075+200000*.05+(profit-400000)*.03);
		}else if(profit<=1000000){
    
    
			prize=(long) (100000*.1+100000*.075+200000*.05+200000*.03+(profit-600000)*.015);
		}else {
    
    
			prize=(long) (100000*.1+100000*.075+200000*.05+200000*.03+400000*.015+(profit-1000000)*.01);
		}
				System.out.println("你应该得到的奖金为:"+prize);
	}
}



2.3 [Procedure 3]

public class SquareNumber {
    
    
	/**
		 * 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		for (int i = 1; i < 100000001; i++) {
    
    
			int sq1 = (int) Math.sqrt(i + 100);
			int sq2 = (int) Math.sqrt(i + 268);
			if ((i + 100) == sq1 * sq1 && (i + 268) == sq2 * sq2)
				System.out.println(i);
		}
	}
}

2.4 [Procedure 4]

import java.util.Scanner;

public class YearMonthDays {
    
    
	/**
		 * 1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
	 */
	public static void main(String[] args) {
    
    
		int[] days = {
    
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int year, month, day;
		Scanner sc = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入一个正整数作为年份:");
			year = sc.nextInt();
			if (year > 0)
				break;
		}
		while (true) {
    
    
			System.out.println("请输入一个1~12之间的整数作为月份:");
			month = sc.nextInt();
			if (month > 0 && month < 13)
				break;
		}
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			days[1] = 29;
		while (true) {
    
    
			System.out.println("请输入一个1" + "~" + days[month - 1] + "之间的整数作为号数:");
			day = sc.nextInt();
			if (day > 0 && day <= days[month - 1])
				break;
		}
		int sum = day;
		for (int i = 0; i < month - 1; i++)
			sum += days[i];
		System.out.println(year + "年" + month + "月" + day + "日是今年的第" + sum
				+ "天。");
	}

}



2.5 [Procedure 5]

import java.util.Scanner;
public class CompareTheSizeOf {
    
    
	/*
	 * 1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换
	 * ,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
	 */
	public static void main(String[] args) {
    
    
		float a,b,c,temp;
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入三个实数(在同一行时,用空格分开):");
		a=sc.nextFloat();
		b=sc.nextFloat();
		c=sc.nextFloat();
				if(a>b){
    
    
			temp=a;
			a=b;
			b=temp;
		}
		if(b>c){
    
    
			temp=b;
			b=c;
			c=temp;
		}
		if(a>b){
    
    
			temp=a;
			a=b;
			b=temp;
		}
		System.out.println(a+" <= "+b+" <= "+c);
	}
}


2.6 [Procedure 6]

public class NineNineTable {
    
    
	/**
	 * 【程序16】 题目:输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
	 */
	public static void main(String[] args) {
    
    
		String s;
		for (int i = 1; i < 10; i++) {
    
    
			for (int j = 1; j <= i; j++) {
    
    
				s = i + "*" + j + "=" + i * j + "      ";
				System.out.print(s.substring(0, 9));
			}
			System.out.println();
		}
	}
}


2.7 [Procedure 7]

public class MonkeyEatingPeach {
    
    
	/**
* 1.程序分析:采取逆向思维的方法,从后往前推断。
	 * 此程序包含验证!!!
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int a = 1;
		for (int i = 1; i < 10; i++) {
    
    
			a = 2 * (a + 1);
		}
		System.out.println("一共有:"+ a+ "个桃子!\n==========================================================================");
		// 以下为验证:
		int b = a;
		int i = 1;
		while (b != 1) {
    
    
			b = a / 2 - 1;
			System.out.println("第" + (i++) + "天,猴子吃了:" + (a / 2 + 1)
					+ "个桃子;还剩下" + b + "个桃子(和为:" + (a / 2 + 1 + b)
					+ ",是头一天所剩的桃子数)。");
			a = b;
		}
		System.out.println("======================================================================\n第"+ i + "天,剩下的桃子数为:" + a + "个。");
	}
}


2.8 [Procedure 8]

public class PingpongGame {
    
    
	public static void main(String[] args) {
    
    
		char[] a = {
    
     'a', 'b', 'c' };
		char[] b = {
    
     'x', 'y', 'z' };
		int ct=0;
				for (int i = 0; i < a.length; i++) {
    
    
			for (int j = 0; j < b.length; j++) {
    
    
				if (i == 0 && j == 0)
					continue;
				if ((i == 2 && j == 0) || (i == 2 && j == 2))
					continue;
				System.out.println(a[i] + " : " + b[j]);
				ct++;
			}
		}
		System.out.println("一共有:"+ct+"场乒乓比赛!");
	}

}


2.9 [Procedure 9]

public class DiamontPattern {
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		StringBuffer preBlank;
		StringBuffer star;
		int tmp;
		for (int i = 1; i < 8; i++) {
    
    
			preBlank = new StringBuffer();
			star = new StringBuffer();
			if (i < 5)
				tmp = i;
			else
				tmp = 8 - i;
			for (int j = 1; j < 2 * tmp; j++) {
    
    
				if (j % 2 != 0)
					preBlank.deleteCharAt(0);
				star.append('*');
			}
			System.out.print(preBlank);
			System.out.println(star);
		}
	}

}


2.10 [Procedure 10]

public class FractionSeries {
    
    

	/**
	 * 【程序20】 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
	 * 1.程序分析:请抓住分子与分母的变化规律。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		double fz = 2.;
		double fm = 1.;
		double he;
		double sum = fz / fm;
		System.out.print((int) fz + "/" + (int) fm);

		for (int i = 1; i < 20; i++) {
    
    
			he = fz + fm;
			fm = fz;
			fz = he;
			sum += fz / fm;
			System.out.print(" + " + (int) fz + "/" + (int) fm);
		}

		System.out.println(" = " + sum);
	}

}

Guess you like

Origin blog.csdn.net/youcheng_ge/article/details/130304773