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

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

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



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 (3)
insert image description here


1. Topic

1.1 [Procedure 1]

[Procedure 21]
Title: Find the sum of 1+2!+3!+...+20!
1. Program analysis: This program just turns accumulation into cumulative multiplication.

1.2 [Procedure 2]

[Procedure 22]
Title: Use the recursive method to find 5!.
1. Program analysis: recursive formula: fn=fn_1*4!

1.3 [Procedure 3]

[Procedure 23]
Topic: There are 5 people sitting together, how old is the fifth person? He said he was 2 years older than the 4th person. Ask the age of the fourth person, he said that he is 2 years older than the third person. Ask the third person, and say that he is two years older than the second person. Ask the second person and say that he is two years older than the first person. Finally asked the first person, he said he was 10 years old. How old is the fifth person?
1. Program analysis: Using the recursive method, the recursion is divided into two stages: backtracking and recursion. If you want to know the age of the fifth person, you need to know the age of the fourth person, and so on, to the first person (10 years old), and then push back.

1.4 [Procedure 4]

[Procedure 24]
Title: Given a positive integer with no more than 5 digits, requirements: 1. Find how many digits it is, 2. Print out the digits in reverse order.

1.5 [Procedure 5]

[Procedure 25] Title: A 5-digit number, determine whether it is a palindromic number. That is, 12321 is a palindrome number, the ones and ten thousand digits are the same, and the tens and thousands digits are the same.

1.6 [Procedure 6]

[Procedure 26]
Title: Please enter the first letter of the day of the week to judge the day of the week. If the first letter is the same, continue to judge the second letter.
1. Program analysis: It is better to use a situational statement. If the first letter is the same, use a situational statement or an if statement to judge the second letter.

1.7 [Procedure 7]

[Procedure 27]
Title: Find the prime numbers within 100

1.8 [Procedure 8]

[Procedure 28]
Topic: Sort 10 numbers
1. Program analysis: You can use the selection method, that is, from the last 9 comparison processes, select a smallest element to exchange with the first element, and so on next time, that is, use the second elements are compared with the last 8 and exchanged.

1.9 [Procedure 9]

[Procedure 29]
Title: Find the sum of the diagonal elements of a 3*3 matrix
1. Program analysis: Use double for loops to control the input of a two-dimensional array, and then accumulate a[i][i] and output.

1.10 [Procedure 10]

[Procedure 30]
Title: There is an array that has been sorted. Now enter a number and ask to insert it into the array according to the original rules.

  1. Program analysis: first judge whether this number is greater than the last number, and then consider the case of inserting the middle number. After the insertion, the numbers after this element are moved back one position in turn.


Two, the answer

2.1 [Procedure 1]

public class FactorialAddSum {
    
    

	/**
	 * 【程序21】 题目:求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		long t = 1;
		long sum = 1;
		String s = "1!";
		for (int i = 2; i < 21; i++) {
    
    
			t *= i;
			sum += t;
			s += " + " + i + "!";
		}

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

}



2.2 [Procedure 2]

import java.util.Scanner;

public class FactorialRecursive {
    
    

	/**
	 * @param args
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入一个非负整数:");
			if (0 <= (n = sc.nextInt()))
				break;
		}

		System.out.println(n + "! = " + factorial(n));
	}

	private static long factorial(int n) {
    
    
		if (n == 0) {
    
    
			return 1l;
		} else {
    
    
			return factorial(n - 1) * n;
		}
	}
}




2.3 [Procedure 3]

public class CalcAge {
    
    

	/**
	 * 【程序23】
	 * 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。
	 * 问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
	 * 1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		System.out.println("第五个人的年龄是:" + calcAge(5));
	}

	private static int calcAge(int n) {
    
    
		if (n == 1)
			return 10;
		else
			return calcAge(n - 1) + 2;
	}
}


2.4 [Procedure 4]

import java.util.Scanner;

public class OfNumbersReverse {
    
    

	/**
	 * 【程序24】 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		long n;
		while (true) {
    
    
			System.out.println("请输入一个不多于5位的正整数:");
			n = sc.nextLong();
			if (n > 0 && n < 100000)
				break;
		}
		String s = "" + n;
		System.out.println(s + "是" + s.length() + "位数;\n其反序输出结果为:");
		for (int i = s.length(); i > 0; i--) {
    
    
			System.out.print(s.charAt(i - 1));
		}
	}
}


2.5 [Procedure 5]

public class ReturnText {
    
    
	/**
	 * 【程序25】 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int ct = 0;
		for (int i = 10000; i < 100000; i++) {
    
    
			if (isReturnText("" + i)) {
    
    
				System.out.println(i);
				ct++;
			}
		}
		System.out.println("在10000~99999之间共有:" + ct + "个回文数。");
	}

	private static boolean isReturnText(String s) {
    
    
		char[] ch = s.toCharArray();
		if (ch[0] == ch[4] && ch[1] == ch[3]) {
    
    
			return true;
		} else {
    
    
			return false;
		}
	}
}


2.6 [Procedure 6]

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FindWeek {
    
    

	/**
	 * 【程序26】 题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
	 * 1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Week[] week = Week.values();
		List<Week> list = new ArrayList<Week>();
		List<Week> list2 = new ArrayList<Week>();
		char ch, ch2;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个字符:");
		ch = Character.toUpperCase(sc.nextLine().charAt(0));
		for (int i = 0; i < 7; i++) {
    
    
			if (ch == week[i].toString().charAt(0)) {
    
    
				list.add(week[i]);
			}
		}
		switch (list.size()) {
    
    
		case 0:
			System.out.println("星期名称中没有以字母" + ch + "开头的。");
			break;
		case 1:
			System.out.println("以字母" + ch + "开头的周日期是:" + list.get(0) + ",即是:"
					+ list.get(0).toChString() + "。");
			break;
		default:
			System.out.println("请输入第二个字符:");
			ch2 = Character.toLowerCase(sc.nextLine().charAt(0));
			for (int i = 0; i < list.size(); i++) {
    
    
				if (ch2 == list.get(i).toString().charAt(1)) {
    
    
					list2.add(list.get(i));
				}
			}
			if (list2.size() == 1) {
    
    
				System.out.println("以" + ch + ch2 + "开头的周日期是:" + list2.get(0)
						+ ",即是:" + list2.get(0).toChString() + "。");
			} else {
    
    
				System.out.println("星期名称中没有以" + ch + ch2 + "开头的。");
			}
		}
	}

}

enum Week {
    
    
	Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;

	public String toChString() {
    
    
		String str = "";
		switch (this.ordinal()) {
    
    
		case 0:
			str = "星期日";
			break;
		case 1:
			str = "星期一";
			break;
		case 2:
			str = "星期二";
			break;
		case 3:
			str = "星期三";
			break;
		case 4:
			str = "星期四";
			break;
		case 5:
			str = "星期五";
			break;
		case 6:
			str = "星期六";
			break;
		}
		return str;
	}
}

2.7 [Procedure 7]

public class PrimeDown100 {
    
    

	/**
	 * 【程序27】 题目:求100之内的素数
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int ct = 0;
		for (int i = 2; i < 101; i++) {
    
    
			int j;
			for (j = 2; j < i; j++) {
    
    
				if (i % j == 0)
					break;
			}
			if (i == j) {
    
    
				ct++;
				System.out.println(i + "  ");
			}
		}
		System.out.println("2~100之间共有:" + ct + "个质数。");
	}

}


2.8 [Procedure 8]

① Method 1:

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class SortArray10_2 {
    
    

	/**
	 * 【程序28】 题目:对10个数进行排序 。 1、分析 使用Array及Arrays进行排序处理。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		float f[] = new float[10];
		System.out.println("请输入十个实数(中间可以用空格分开,也可以是回车,但不能够为其它字符,多则仅前十个有效):");
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < 10; i++) {
    
    
			f[i] = sc.nextFloat();
		}
		System.out.println("你输入的十个数分别为:");
		for (int i = 0; i < Array.getLength(f); i++) {
    
    
			System.out.print(Array.getFloat(f, i) + "  ");
		}
		Arrays.sort(f);
		System.out.println("\n你输入的十个数排序(从小到大)后为:");
		for (int i = 0; i < Array.getLength(f); i++) {
    
    
			System.out.print(Array.getFloat(f, i) + "  ");
		}
	}
}

②Method 2:

import java.util.Scanner;

public class SortArray10 {
    
    

	/**
	 * 【程序28】 题目:对10个数进行排序 1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,
	 * 下次类推,即用第二个元素与后8个进行比较,并进行交换。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		float[] f = new float[10];
		System.out.println("请输入十个实数(中间可以用空格分开,也可以是回车,但不能够为其它字符,多则仅前十个有效):");
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < 10; i++) {
    
    
			f[i] = sc.nextFloat();
		}
		System.out.println("你输入的十个数分别为:");
		output(f);

		float tmp;
		for (int i = 0; i < f.length - 1; i++) {
    
    
			for (int j = i + 1; j < f.length; j++) {
    
    
				if (f[i] > f[j]) {
    
    
					tmp = f[i];
					f[i] = f[j];
					f[j] = tmp;
				}
			}
		}
		System.out.println("你输入的十个数排序后(从小到大)为:");
		output(f);
	}

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

2.9 [Procedure 9]

import java.util.Scanner;

public class DiagonalMatrix {
    
    

	/**
	 * 1、设M=(αij)为n阶方阵.M的两个下标相等的所有元素都叫做M的对角元素,而序列(αii)1≤i≤n叫做M的主对角线.
	 * 2、所有非主对角线元素全等于零的n阶矩阵,称为对角矩阵或称为对角方阵。
	 */
	// *【程序29】 题目:求一个3*3矩阵对角线元素之和
	// * 1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[][] a = new int[3][3];
		Scanner sc = new Scanner(System.in);
		String[] str = new String[3];
		int sum = 0;
		for (int i = 0; i < 3; i++) {
    
    
			str[i] = "";
			for (int j = 0; j < 3; j++) {
    
    
				String tmp = "";
				if (i == j) {
    
    
					System.out.println("请输入对角矩阵的第" + (i + 1) + "个对角上的数:");
					a[i][j] = sc.nextInt();
					sum += a[i][j];
				} else {
    
    
					a[i][j] = 0;
				}
				tmp += a[i][j] + "            ";
				str[i] += tmp.substring(0, 8);
			}
		}

		System.out.println("                                        这个对角矩阵的形式为:");
		for (int i = 0; i < 3; i++) {
    
    
			System.out.println("                " + str[i]);
		}
		System.out.println("这个对角矩阵的对角上线数值的和为:" + sum);
	}

}




2.10 [Procedure 10]

① Method 1:

import java.util.Scanner;

public class InsertNumberInOrderedArray {
    
    

	/**
	 * 【程序30】 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 1.
	 * 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
	 */
	// 此程序实际上就是插入排序法,可以根据输入你要进行排序的
	// 数据个数,然后排序出来(是升序——从小到大排列的)。
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] a = new int[1];
		int[] b = new int[1];
		int k = 2;
		int tmp, tmp2;
		Scanner sc = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入一个大于1的整数:");
			tmp2 = sc.nextInt();
			if (tmp2 > 1)
				break;
		}
		System.out.println("请你再输入" + tmp2 + "个数(可用空格隔开,也可以打回车,多的将被去掉)");
		a[0] = sc.nextInt();
		while (k <= tmp2) {
    
    
			tmp = sc.nextInt();
			boolean f = false;
			b = new int[k];
			ShiftArrayElements(b, a);// 临时将数组a的内容存入数组b中
			int m;
			for (m = k - 2; m >= 0; m--) {
    
    
				if (tmp < b[m]) {
    
    
					b[m + 1] = b[m];
					f = true;
				} else {
    
    
					break;
				}
			}
			if (!f) {
    
    //如果在它本身最大,则放入最后一个元素中
				b[k - 1] = tmp;
			} else {
    
    
				b[m + 1] = tmp;//否则在中间或开头插入
			}
			a = new int[k++];
			ShiftArrayElements(a, b);// 临时将数组b的内容存入数组a中
		}

		System.out.println("============================\n这" + tmp2
				+ "个数从小到到的顺序如下:");
		for (int i = 0; i < a.length; i++) {
    
    
			System.out.print(a[i] + "  ");
		}
	}

	private static void ShiftArrayElements(int[] a, int[] b) {
    
    
		for (int i = 0; i < b.length; i++) {
    
    
			a[i] = b[i];
		}
	}
}

②Method 2:

import java.util.Scanner;

public class InsertNumberInOrderedArray2 {
    
    

	/**
	 * 【程序30】 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 1.
	 * 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] a = new int[8];
		int[] b = {
    
     23, 45, 46, 78, 90, 102, 120 };
		for (int i = 0; i < b.length; i++) {
    
    
			a[i] = b[i];
		}
		Scanner sc = new Scanner(System.in);
		int d, i, k;
		boolean f = true;
		System.out.println("请输入一个数:");
		d = sc.nextInt();
		for (i = b.length - 1; i > -1; i--) {
    
    
			if (d < a[i]) {
    
    
				a[i + 1] = a[i];
				f = false;
			} else {
    
    
				break;
			}
		}
		if (f) {
    
    
			a[b.length] = d;
			k = b.length;
		} else {
    
    
			a[i + 1] = d;
			k = i + 1;
		}
		output("插入数据前,原来的有序数组为:", b, -1);
		output("插入数\"" + d + "\"后,得到的结果是:", a, k);
	}

	private static void output(String str, int[] a, int k) {
    
    
		System.out.println(str);//k为插入的数作标记,该数前后都加^
		for (int i = 0; i < a.length; i++) {
    
    
			if (k != -1 && k == i) {
    
    
				System.out.print("^" + a[i] + "^  ");
			} else {
    
    
				System.out.print(a[i] + "  ");
			}
		}
		System.out.println();
	}
}




Guess you like

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