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

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


1. Topic

1.1 [Procedure 1]

[Procedure 41]
Title: There are a bunch of peaches on the beach, and five monkeys will share them. The first monkey divided the pile of peaches into five parts on average, and when there was one more, the monkey threw the extra peach into the sea and took one part. The second monkey divided the remaining peaches into five equally, and there was one more. It also threw the extra peach into the sea and took a share. The third, fourth, and fifth monkeys did the same. Yes, ask how many peaches there are at least on the beach?

1.2 [Procedure 2]

[Procedure 42]
Title: 809*??=800*??+9*??+1 Where?? represents two digits, the result of 8*?? is two digits, and the result of 9*?? is 3 digits. Find the two digits represented by ?? and the result after 809*??.

1.3 [Procedure 3]

[Procedure 43]
Title: Find the odd numbers that can be formed from 0-7 (1-8 digits).

1.4 [Procedure 4]

[Procedure 44]
Title: An even number not less than 6 can always be expressed as the sum of two odd prime numbers.

1.5 [Procedure 5]

[Procedure 45]
Title: Determine how many prime numbers divisible by 9.

1.6 [Procedure 6]

[Procedure 46]
Title: Two string concatenation programs

1.7 [Procedure 7]

[Procedure 47]
Title: Read the integer values ​​of 7 numbers (1-50), and each time a value is read, the program prints out the * of the value.

1.8 [Procedure 8]

[Procedure 48]
Title: A company uses a public telephone to transmit data. The data is a four-digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each number, and then divide the sum by the remainder of 10 Instead of that number, swap the first and fourth digits, and the second and third digits.

1.9 [Procedure 9]

[Procedure 49]
Title: Count the number of occurrences of a substring in a string

1.10 [Procedure 10]

[Procedure 50]
Title: There are five students, and each student has grades of 3 courses. Input the above data (including student number, name, and grades of three courses) from the keyboard, calculate the average grade, and combine the original data and The calculated average score is stored in the disk file "stud".


Two, the answer

2.1 [Procedure 1]

public class BeachDividedPeach {
    
    

	/**
	 * 【程序41】 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。
	 * 第二只猴子把剩下的桃子又平均分成五份
	 * ,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int sum = 0;
		int cnt = 0;
		outer: for (int i = 6; i < 10000; i++) {
    
    
			sum = i;
			cnt = 0;
			for (int j = 0; j < 5; j++) {
    
    
				if (sum % 5 == 1) {
    
    
					sum = sum - ((sum / 5) + 1);
					cnt++;
					if (cnt == 5) {
    
    
						System.out.println(i);
						break outer;
					}
				} else {
    
    
					break;
				}
			}
		}
	}
}


2.2 [Procedure 2]

public class DoubleDigit {
    
    

	/**
	 * 【程序42】 题目:809*??=800*??+9*??+1(不是等式)
	 * 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int x;
		for (x = 10; x < 100; x++) {
    
    
			int a = 809 * x + 1;
			if (x * 8 < 100 && x * 9 > 99) {
    
    
				if (a >= 1000 && a <= 100000) {
    
    
					System.out.println("?? = " + x);
					System.out.println("809 * " + x + " = " + 809 * x);
				}
			}
		}
	}
}

2.3 [Procedure 3]

public class Odd1_8 {
    
    

	/**
	 * 【程序43】 题目:求0—7所能组成(1-8位)的奇数个数。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int sum = 0;
		int t;
		for (int i = 1; i < 9; i++) {
    
    
			sum += count(i);
		}

		System.out.println(sum);// 下面一句为硬算,由此可以验证上述程序的正确性
		System.out.println(4 + 4 * 7 + 4 * 7 * 8 + 4 * 7 * 8 * 8 + 4 * 7 * 8
				* 8 * 8 + 4 * 7 * 8 * 8 * 8 * 8 + 4 * 7 * 8 * 8 * 8 * 8 * 8 + 4
				* 7 * 8 * 8 * 8 * 8 * 8 * 8);
	}

	private static int count(int i) {
    
    
		if (i == 1)
			return 4;
		if (i == 2)
			return 7 * count(i - 1);
		return 8 * count(i - 1);
	}

}

2.4 [Procedure 4]

public class GoldbachConjecture {
    
    

	/**
	 * Goldbach Conjecture 【程序44】 题目:一个不小于6的偶数总能表示为两个奇素数之和。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		boolean[] f=new boolean[50];
		for (int i = 6; i < 101; i += 2) {
    
    
			String str = i + " = ";
			int c = 0;
			f[(i-6)/2]=false;
			for (int j = 3; j < i; j += 2) {
    
    
				if (isPrime(j) && isPrime(i - j)) {
    
    
					c++;
					f[(i-6)/2]=true;
					if (c == 1) {
    
    
						str += j + " + " + (i - j);
					} else {
    
    
						str += " 或 " + j + " + " + (i - j);
					}
				} 
			}
			System.out.println(str);
		}
		boolean f1=true;
		for(int i=6;i<101;i+=2){
    
    
			if(!f[(i-6)/2]){
    
    
				f1=false;
			}
		}
		System.out.println("========================================");
		if (f1) {
    
    
			System.out.println("一个不小于6的偶数总能表示为两个奇素数之和。");
		} else {
    
    
			System.out.println("一个不小于6的偶数不是总能表示为两个奇素数之和。");
		}
	}

	public static boolean isPrime(int a) {
    
    
		if (a == 1)
			return false;
		for (int i = 3; i <= Math.sqrt(a); i++) {
    
    
			if (a % i == 0)
				return false;
		}
		return true;
	}
}



2.5 [Procedure 5]

public class PrimeDivisible {
    
    

	/**
	 * 【程序45】 题目:判断几个9能被哪几个素数整除。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		long k, t;
		t = 10;
		for (int i = 1; i <= 10; i++) {
    
    // i为9的个数
			t *= 10;
			k = t - 1;
			String str = k + "能够";
			int c = 0;
			for (int j = 2; j <= Math.sqrt(k); j++) {
    
    
				if (k % j == 0 && isPrime(j)) {
    
    
					c++;
					if (c == 1) {
    
    
						str += "能够被质数" + j + "整数。";
					} else {
    
    
						str += "还能够被质数" + j + "整数。";
					}
				}
			}
			System.out.println(str);
		}
	}

	private static boolean isPrime(int a) {
    
    
		if (a == 1)
			return false;
		for (int i = 2; i <= Math.sqrt(a); i++) {
    
    
			if (a % i == 0)
				return false;
		}
		return true;
	}
}


2.6 [Procedure 6]

import java.util.Scanner;

public class StringConcat {
    
    

	/**
	 * 【程序46】 题目:两个字符串连接程序
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请分别输入两个字符串(用空格隔开,多余的自动去除):");
		String str1 = sc.next();
		String str2 = sc.next();
		System.out.println("\""+str1 + "\"和\"" + str2 + "\"连接起来的结果是:" + (str1 + str2));
	}

}

2.7 [Procedure 7]

import java.util.Scanner;

public class ReadSevenNumbers {
    
    

	/**
	 * 【程序47】 题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int[] a = new int[7];
		for (int i = 0; i < 7; i++) {
    
    
			System.out.println("请输入(1-50)之间的一个整数(第"+(i+1)+"个):");
			while (true) {
    
    
				a[i] = sc.nextInt();
				if (a[i] > 0 && a[i] < 51) {
    
    
					break;
				}
			}
			String str = "";
			for (int j = 0; j < a[i]; j++) {
    
    
				str += "*";
			}
			System.out.println(str);
		}
	}

}


2.8 [Procedure 8]

import java.util.Scanner;

public class EncodingDecoding {
    
    

	/**
	 * 【程序48】
	 * 题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字
	 * ,再将第一位和第四位交换,第二位和第三位交换。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str;
		while (true) {
    
    
			System.out.println("请输入一个四位数,可以以0开头:");
			str = sc.next();
			boolean f = true;
			if (str.length() == 4) {
    
    
				for (int i = 0; i < 4; i++) {
    
    
					if (!Character.isDigit(str.charAt(i))) {
    
    
						f = false;
					}
				}
			} else {
    
    
				f = false;
			}
			if (f)
				break;
		}
		System.out.println("==========\n你输入的数字是:" + str);
		String s = encoding(str);
		System.out.println("==========\n加密后数字是:" + s);
		System.out.println("==========\n解密后数字是:" + decoding(s));
		System.out.println("==========\n输入数据与加密再解密的数字比较结果为:"
				+ (str.equals(decoding(s))));
	}

	private static String encoding(String str) {
    
    
		int[] a = new int[4];
		String s = "";
		int tmp;
		for (int i = 0; i < 4; i++) {
    
    
			a[i] = Integer.parseInt(str.substring(i, i + 1));
			tmp = (a[i] + 5) % 10;
			a[i] = tmp;
		}
		tmp = a[0];
		a[0] = a[3];
		a[3] = tmp;
		tmp = a[1];
		a[1] = a[2];
		a[2] = tmp;
		for (int i = 0; i < 4; i++) {
    
    
			s += "" + a[i];
		}
		return s;
	}

	private static String decoding(String str) {
    
    
		int[] a = new int[4];
		String s = "";
		int tmp;
		for (int i = 0; i < 4; i++) {
    
    
			a[i] = Integer.parseInt(str.substring(i, i + 1));
			if (a[i] >= 5)
				tmp = a[i] - 5;
			else
				tmp = a[i] + 10 - 5;
			a[i] = tmp;
		}
		tmp = a[0];
		a[0] = a[3];
		a[3] = tmp;
		tmp = a[1];
		a[1] = a[2];
		a[2] = tmp;
		for (int i = 0; i < 4; i++) {
    
    
			s += "" + a[i];
		}
		return s;
	}
}

2.9 [Procedure 9]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringAndSubstring {
    
    

	/**
	 * 【程序49】 题目:计算字符串中子串出现的次数
	 * 
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
    
    
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入一个字符串(任意长度):");
		String str = br.readLine();
		System.out.println("请输入要查找的子串:");
		String sub = br.readLine();
		int l = sub.length();
		
		int count = 0;
		for (int i = 0; i < str.length() - l; i++) {
    
    
			if (sub.equals(str.substring(i, i + l))) {
    
    
				count++;
			}
		}

		System.out.println("子串出现的总次数为:" + count);
	}

}


2.10 [Procedure 10]

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

public class OutputToDisk {
    
    

	/**
	 * 【程序50】 题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,
	 * 将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
	 * 
	 * @throws Exception
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException,
			Exception {
    
    
		// TODO Auto-generated method stub
		int number;
		String name;
		float score1;
		float score2;
		float score3;
		Student[] stud = new Student[5];

		Scanner sc = new Scanner(System.in);
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"stud.txt", true));
		for (int i = 0; i < 5; i++) {
    
    
			System.out.println("请输入第" + (i + 1) + "个同学的(学号、姓名、三科成绩):");
			number = sc.nextInt();
			name = sc.next();
			score1 = sc.nextFloat();
			score2 = sc.nextFloat();
			score3 = sc.nextFloat();// 以上为键盘输入学号、姓名及三科成绩

			stud[i] = new Student(number, name, score1, score2, score3);
			// 以下将上述输入及平均成绩写入到文件stud.txt中
			oos.writeInt(stud[i].getNumber());
			oos.writeUTF(stud[i].getName());
			oos.writeFloat(stud[i].getScore1());
			oos.writeFloat(stud[i].getScore2());
			oos.writeFloat(stud[i].getScore3());
			oos.writeFloat(stud[i].getAvg());
		}

		oos.flush();
		oos.close();

		// 以下为从文件中读取上述信息,然后输出姓名及平均成绩
		System.out.println("以下为从文件中读取上述信息,然后输出姓名及平均成绩:");
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"stud.txt"));
		for (int i = 0; i < 5; i++) {
    
    
			int h;
			String xm;
			float a;
			float b;
			float c;
			float d;
			h = ois.readInt();
			xm = ois.readUTF();
			a = ois.readFloat();
			b = ois.readFloat();
			c = ois.readFloat();
			d = ois.readFloat();
			stud[i] = new Student(h, xm, a, b, c);
			System.out.println(stud[i].getName() + ": " + stud[i].getAvg());
		}
		ois.close();
	}

}

class Student implements Serializable {
    
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int number;
	private String name;
	private float score1;
	private float score2;
	private float score3;
	private float avg;

	public Student() {
    
    

	}

	public Student(int number, String name, float score1, float score2,
			float score3) {
    
    
		this.number = number;
		this.name = name;
		this.score1 = score1;
		this.score2 = score2;
		this.score3 = score3;
		this.avg = (score1 + score2 + score3) / 3.0f;
	}

	public int getNumber() {
    
    
		return number;
	}

	public String getName() {
    
    
		return name;
	}

	public float getScore1() {
    
    
		return score1;
	}

	public float getScore2() {
    
    
		return score2;
	}

	public float getScore3() {
    
    
		return score3;
	}

	public float getAvg() {
    
    
		return avg;
	}

}


Guess you like

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