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

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.

[Java] "Blue Bridge Cup" 10 programming questions and answers (4)
insert image description here


1. Topic

1.1 [Procedure 1]

[Procedure 31]
Title: Output an array in reverse order.
1. Program analysis: exchange with the first and last.

1.2 [Procedure 2]

[Procedure 32]
Title: Take the 4~7 digits from the right end of an integer a.
Program analysis: You can think of it like this:
(1) First shift a to the right by 4 bits.
(2) Set a number whose lower 4 bits are all 1 and the rest are all 0. ( 0<<4) (3) can
perform & operation on the above two.

1.3 [Procedure 3]

【程序33】 
题目:打印出杨辉三角形(要求打印出10行如下图) 
1.程序分析: 
     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 
1 5 10 10 5 1 

1.4 [Procedure 4]

[Procedure 34]
Title: Input 3 numbers a, b, c, and output them in order of size.
1. Program analysis: use the pointer method.

1.5 [Procedure 5]

[Procedure 35]
Title: Input an array, exchange the largest element with the first element, exchange the smallest element with the last element, and output the array.

1.6 [Procedure 6]

[Procedure 36]
Title: There are n integers, so that the previous numbers are moved backward by m positions, and the last m numbers become the first m numbers

1.7 [Procedure 7]

[Procedure 37]
Title: There are n people forming a circle, and they are numbered sequentially. Start counting from the first person (counting from 1 to 3), and everyone who has reported to 3 will withdraw from the circle, and ask who is the original number left at the end.

1.8 [Procedure 8]

[Procedure 38]
Title: Write a function to find the length of a string, input the string in the main function, and output its length.

1.9 [Procedure 9]

[Procedure 39]
Title: Write a function, when the input n is an even number, call the function to find 1/2+1/4+...+1/n, when the input n is an odd number, call the function 1/1+1/3+ ...+1/n (using pointer functions)

1.10 [Procedure 10]

[Procedure 40]
Title: String sorting.


Two, the answer

2.1 [Procedure 1]

public class ReversePrintArray {
    
    

	/**
	 * 【程序31】 题目:将一个数组逆序输出。 1.程序分析:用第一个与最后一个交换。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] a = {
    
     123, 45, 34, 64, 16, 49, 70, 57, 19 };

		output(1, "该数组正序输出为:", a);
		output(-1, "该数组反序输出为:", a);
	}

	private static void output(int f, String str, int[] a) {
    
    
		int k;
		System.out.print(str);
		for (int i = 0; i < a.length; i++) {
    
    
			if (f >= 0) {
    
    
				k = i;
			} else {
    
    
				k = a.length - i - 1;
			}
			System.out.print(a[k] + "  ");
		}
		System.out.println();
	}
}


2.2 [Procedure 2]

public class MoveSpecifiedNumbers {
    
    

	/**
	 * 【程序32】 题目:取一个整数a从右端开始的4~7位。 程序分析:可以这样考虑: (1)先使a右移4位。
	 * (2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4) (3)将上面二者进行&运算。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		char a = '\u0f40';// 0000 1111 0100 0000b
		int b = 15;// b为0000 0000 0000 1111b
		a >>= 4;// a右移四位得:0000 0000 1111 0100b
		int c;
		c = a & b; // a&b后,c为:0000 0000 0000 0100b,亦即4。
		System.out.println(c);

	}
}

2.3 [Procedure 3]

import java.util.Scanner;

public class YangHuiTriangle {
    
    

	/**
	 * 【程序33】 题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析:
	 */
	// 1
	// 1 1
	// 1 2 1
	// 1 3 3 1
	// 1 4 6 4 1
	// 1 5 10 10 5 1

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入杨辉三角的阶数(>=0)的整数");
			n = sc.nextInt();
			if (n > 0)
				break;
		}
		int[][] a = new int[n][];

		for (int i = 0; i < n; i++) {
    
    
			a[i] = new int[i + 1];
			String str = "";
			for (int j = 0; j <= i; j++) {
    
    
				if (i == 0 && j == 0) {
    
    
					a[i][j] = 1;
				} else if (j == 0 || j == i) {
    
    
					a[i][j] = 1;
				} else {
    
    
					a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
				}
				str += (a[i][j] + "       ").substring(0, 6);
			}
			System.out.println(space(20 - i) + str);
		}
	}

	private static String space(int n) {
    
    
		String s = "";
		for (int i = 0; i <  n; i++) {
    
    
			s += "   ";
		}
		return s;
	}
}

2.4 [Procedure 4]

import java.util.Scanner;

public class ThreeNumbersCompare {
    
    

	/**
	 * 【程序34】 题目:输入3个数a,b,c,按大小顺序输出。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入三个数(中间用空格隔开或者回车,多余的无效):");
		int a, b, c;
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		System.out.println("原序:" + a + ", " + b + ", " + c);

		int tmp;
		if (a < b) {
    
    
			tmp = a;
			a = b;
			b = tmp;
		}
		if (a < c) {
    
    
			tmp = a;
			a = c;
			c = tmp;
		}
		if (b < c) {
    
    
			tmp = b;
			b = c;
			c = tmp;
		}

		System.out.println("现在:" + a + " >= " + b + " >= " + c);
	}
}


2.5 [Procedure 5]

import java.util.Scanner;

public class PrizeCommision {
    
    

	/**
	 * 【程序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,求应发放奖金总数?
	 * 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		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.6 [Procedure 6]

import java.util.Scanner;

public class ShiftMNumbers {
    
    

	/**
	 * 【程序36】 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n, m;
		while (true) {
    
    
			System.out.println("请输入一个大于1的整数:");
			n = sc.nextInt();
			if (n > 1)
				break;
		}
		int[] a = new int[n];
		int[] b = new int[n];
		while (true) {
    
    
			System.out.println("请输入一个大于0而且小于" + n + "的整数:");
			m = sc.nextInt();
			if (m > 0 && m < n)
				break;
		}

		for (int i = 0; i < n; i++) {
    
    
			System.out.println("请输入" + n + "个数(用空格或回车分隔,多余无效):");
			a[i] = sc.nextInt();
		}

		for (int i = 0; i < n; i++) {
    
    
			if (i < m) {
    
    
				b[i] = a[i + m];
			} else {
    
    
				b[i] = a[i - m];
			}
		}

		output("移位前:", a);

		output(n + "个整数,使其前面各数顺序向后移" + m + "个位置,最后" + m + "个数变成最前面的" + m
				+ "个数,最后得到:", b);
	}

	private static void output(String s, int[] a) {
    
    
		System.out.println(s);
		for (int i = 0; i < a.length; i++) {
    
    
			System.out.print(a[i] + "  ");
		}
		System.out.println();
	}
}


2.7 [Procedure 7]

① Method 1:

import java.util.Scanner;

public class CircleNumber {
    
    

	/**
	 * 【程序37】 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入一个大于1的整数:");
			n = sc.nextInt();
			if (n > 1)
				break;
		}

		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
    
    
			a[i] = i + 1;
		}

		int e = 0, c = 0;
		int[] b = new int[n - 1];
		int bi = 0;
		System.out.print("\n==========================\n当为" + n
				+ "个人时,去掉的人的序号有:");
		whileCircle: while (true) {
    
    
			for (int i = 0; i < n; i++) {
    
    
				boolean f = true;
				for (int j = 0; j < bi; j++) {
    
    
					if (a[i] == b[j]) {
    
    
						f = false;
						break;
					}
				}
				if (f) {
    
    
					c++;
					if (c == 3) {
    
    
						b[bi] = a[i];
						System.out.print(b[bi] + " ");
						c = 0;
						bi++;
						if (bi == (n - 1)) {
    
    
							break whileCircle;
						}
					}
				}
			}
		}

		for (int i = 0; i < n; i++) {
    
    
			boolean f = false;
			for (int j = 0; j < n - 1; j++) {
    
    
				if (a[i] == b[j]) {
    
    
					f = true;
					break;
				}
			}
			if (!f) {
    
    
				System.out.println("\n最后剩下的人的号码为:" + a[i]
						+ "号。\n==========================");
				break;
			}
		}
	}
}

②Method 2:

import java.util.Scanner;

public class CircleNumber2 {
    
    

	/**
	 * 【程序37】 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入一个大于1的整数:");
			n = sc.nextInt();
			if (n > 1)
				break;
		}

		boolean[] b = new boolean[n];
		for (int i = 0; i < b.length; i++) {
    
    
			b[i] = true;// 最初所有人均在圈中,如果为false,表示人已经离开圈子
		}

		int index = 0;
		int left = n;
		int count = 0;
		while (true) {
    
    
			if (left == 1) {
    
    
				break;
			}
			if (b[index]) {
    
    
				count++;
				if (count == 3) {
    
    
					count = 0;
					left--;
					b[index] = false;
				}
			}
			index++;
			if (index == n) {
    
    
				index = 0;
			}
		}

		for (int i = 0; i < b.length; i++) {
    
    
			if (b[i]) {
    
    
				System.out.println("最后剩下的是:" + (i + 1) + "号。");
				break;
			}
		}
	}
}



2.8 [Procedure 8]

public class MainParaLength {
    
    

	/**
	 * 【程序38】 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		System.out.println(args[0].length());
	}

}


2.9 [Procedure 9]

import java.util.Scanner;

public class OddEvenTest {
    
    

	/**
	 * 【程序39】
	 * 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入一个正整数:");
			n = sc.nextInt();
			if (n > 0)
				break;
		}

		float sum = 0;
		int start;
		if (n % 2 == 0)
			start = 2;
		else
			start = 1;
		String s = "";
		for (int i = start; i <= n; i += 2) {
    
    
			s += " + 1/" + i;
			sum += 1.0 / i;
		}
		System.out.println(s.substring(3) + " = " + sum);
	}
}



2.10 [Procedure 10]

public class StringArraySort {
    
    

	/**
	 * 【程序40】 题目:字符串排序。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		String[] str={
    
    "hello","Hello","world","abc","wor_ld","123","adk5433","14bcd","kdard"," fdkajd"};
		for(int i=0;i<str.length-1;i++){
    
    
			for(int j=i+1;j<str.length;j++){
    
    
				String s;
				if(str[i].compareTo(str[j])>1){
    
    //比较结果大于1,则str[i]位置应该在str[j]后面
					s=str[i];
					str[i]=str[j];
					str[j]=s;
				}
			}
			System.out.print(str[i]+"   ");
		}
		System.out.println(str[str.length-1]);
	}
}


Guess you like

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