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

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


1. Topic

1.1 [Procedure 1]

[Procedure 1] Title: Classical problem: There is a pair of rabbits, and a pair of rabbits will be born every month from the third month after birth, and a pair of rabbits will be born every month after the third month. If none of them die, what is the total number of rabbits each month?
1. Program analysis: The law of the rabbit is the sequence 1, 1, 2, 3, 5, 8, 13, 21...

1.2 [Procedure 2]

[Procedure 2] Title: Judge how many prime numbers there are between 101-200, and output all prime numbers.
1. Program analysis: The method of judging the prime number: use a number to divide 2 to sqrt (this number), if it can be divisible, it means that the number is not a prime number, otherwise it is a prime number.

1.3 [Procedure 3]

[Procedure 3] Title: Print out all "Narcissus Numbers". The so-called "Daffodils Number" refers to a three-digit number, and the sum of the cubes of each digit is equal to the number itself. For example: 153 is a "daffodil number", because 153=1 cube +5 cube +3 cube.
1. Program analysis: Use the for loop to control 100-999 numbers, and decompose each number into ones, tens, and hundreds.

1.4 [Procedure 4]

[Procedure 4] Title: Decompose a positive integer into prime factors. For example: input 90, print out 90=2 3 3*5.
Program analysis: To decompose the prime factor of n, you should first find a minimum prime number k, and then follow the steps below:
(1) If the prime number is exactly equal to n, it means that the process of decomposing the prime factor has ended, just print it out .
(2) If n<>k, but n can be divisible by k, the value of k should be printed out, and the quotient of n divided by k should be used as a new positive integer n, and the first step should be repeated.
(3) If n is not divisible by k, use k+1 as the value of k, and repeat the first step.

1.5 [Procedure 5]

[Procedure 5] Title: Use the nesting of conditional operators to complete this question: students with academic scores >= 90 points are represented by A, those with scores between 60 and 89 are represented by B, and students with scores below 60 are represented by C.
1. Program analysis: (a>b)?a:b This is a basic example of a conditional operator.

1.6 [Procedure 6]

[Procedure 6] Title: Input two positive integers m and n, find their greatest common divisor and least common multiple.
1. Program analysis: use the elimination method.

1.7 [Procedure 7]

[Procedure 7] Title: Enter a line of characters, and count the number of English letters, spaces, numbers and other characters in it.
1. Program analysis: use the while statement, the condition is that the input character is not '\n'.

1.8 [Procedure 8]

[Procedure 8] Title: Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number. For example, 2+22+222+2222+22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard.
1. Program analysis: The key is to calculate the value of each item.

1.9 [Procedure 9]

[Procedure 9] Title: If a number is exactly equal to the sum of its factors, this number is called a "perfect number". For example, 6=1+2+3. Program to find all the perfect numbers within 1000.

1.10 [Procedure 10]

[Procedure 10] Title: A ball falls freely from a height of 100 meters, and bounces back to half of the original height after each landing; and then falls, how many meters did it pass when it landed for the 10th time? How high is the 10th bounce?


Two, the answer

2.1 [Procedure 1]

import java.util.Scanner;
public class RabbitsQuit {
    
    
	/**
	 * 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月 起每个月都生一对兔子,小兔子长到第三个月后每个月又生 一
	 * 对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		RabbitsQuit rq = new RabbitsQuit();
		System.out.println("请输入一个整数:");
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
    
    
			System.out.println("第" + (i + 1) + "个月:" + rq.calc(i + 1));
		}
	}
	private int calc(int x) {
    
    
		if (x == 1 || x == 2)
			return 1;
		else
			return calc(x - 2) + calc(x - 1);
	}
}

2.2 [Procedure 2]

import java.util.Scanner;
public class RabbitsQuit {
    
    
	/**
	 * 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月 起每个月都生一对兔子,小兔子长到第三个月后每个月又生 一
	 * 对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		RabbitsQuit rq = new RabbitsQuit();
		System.out.println("请输入一个整数:");
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
    
    
			System.out.println("第" + (i + 1) + "个月:" + rq.calc(i + 1));
		}
	}
	private int calc(int x) {
    
    
		if (x == 1 || x == 2)
			return 1;
		else
			return calc(x - 2) + calc(x - 1);
	}
}
法二:
import java.util.Vector;
public class PrimeNumber2 {
    
    
	/**
	 * 【程序2】 题目:判断101-200之间有多少个素数,并输出所有 素数。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt
	 * (这个数),如果能被整除, 则表明此数不是素数,反之是素数。
	 */
	@SuppressWarnings({
    
     "unchecked", "rawtypes" })
	public static void main(String[] args) {
    
    
		Vector v = new Vector();// 使用向量来装素数
		int i, j, ct = 0;
		for (i = 101; i < 200; i++) {
    
    
			for (j = 2; j < i; j++) {
    
    
				if (i % j == 0) {
    
    
					break;
				}
			}
			if (i == j) {
    
    
				v.add(i);
				ct++;
			}
		}
		System.out.println("101~200之间一共有:" + ct + "个素数。");
		System.out.println("===============================");
		System.out.println(v.toString().substring(1,v.toString().length()-1));
	}
}

2.3 [Procedure 3]

public class DaffodilsNumber {
    
    
	/**
	 * 【程序3】 题目:打印出所有的"水仙花数",所谓"水仙花数"是指 一个三位数,其各位数字立方和等于该数本身。例如:
	 * 153是一个"水仙花数",因为153=1的三次方+5的三次方 +3的三次方。 1.程序分析:
	 * 利用for循环控制100-999个数,每个数分解出个位, 十位,百位。
	 */
	public static void main(String[] args) {
    
    
		DaffodilNumber dn;
		int ct = 0;
		for (int i = 100; i < 1000; i++) {
    
    
			dn = new DaffodilNumber(i);
			if (dn.isDaffodilNumber()) {
    
    
				ct++;
				System.out.println(i + " = " + dn);
			}
		}
		System.out.println("==============================");
		System.out.println("100~999之间一共有:" + ct + "个水仙花数。");
	}

}
class DaffodilNumber {
    
    
	private int bw;
	private int sw;
	private int gw;
	private int num;
	public DaffodilNumber(int num) {
    
    
		this.num = num;
		this.bw = num / 100;
		this.sw = (num-bw*100)/10;
		this.gw = num % 10;
	}
	public boolean isDaffodilNumber() {
    
    
		if (num == bw * bw * bw + sw * sw * sw + gw * gw * gw)
			return true;
		else
			return false;
	}
	public String toString() {
    
    
		return bw + "*" + bw + "*" + bw + "+" + sw + "*" + sw + "*" + sw + "+"
				+ gw + "*" + gw + "*" + gw;
	}
}

2.4 [Procedure 4]

import java.util.Scanner;
public class PrimeSplit {
    
    
	/**
* 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
	 * (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
	 * (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
	 * (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n;
		while (true) {
    
    
			System.out.println("请输入一个大于1的整数:");
			n = sc.nextInt();
			if (n > 1)
				break;
		}
		System.out.print(n + " = ");
		int i = 2;
		String str = "";
		while (i <= n) {
    
    
			if (n % i == 0 && isPrime(i)) {
    
    
				str += "*" + i;
				n /= i;
				i = 2;
				continue;
			}
			i++;
		}
		System.out.println(str.substring(1));
	}
	private static boolean isPrime(int n) {
    
    
		for (int i = 2; i < n; i++) {
    
    
			if (n % i == 0) {
    
    
				return false;
			}
		}
		return true;
	}
}

2.5 [Procedure 5]

import java.util.Scanner;
public class PerformanceRating {
    
    
	/**
	 * 【程序5】 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
	 * 1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		float f;
		while (true) {
    
    
			System.out.println("请输入一个成绩:");
			f = sc.nextFloat();
			if (f >= 0 && f <= 100)
				break;
		}
		System.out.print("成绩为:" + f + "的,其等级是:");
		System.out.println((f >= 90 ? "A" : (f < 60 ? "C" : "B")) + "。");
	}
}

2.6 [Procedure 6]

Method one:

import java.util.Scanner;
public class DivisorMultiple {
    
    
	/**
	 * 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。
	 */
	public static void main(String[] args) {
    
    
		int m, n, c;
		Scanner sc = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入第一个正整数:");
			m = sc.nextInt();
			if (m > 0)
				break;
		}
		while (true) {
    
    
			System.out.println("请输入第二个正整数:");
			n = sc.nextInt();
			if (n > 0)
				break;
		}
		c = gcd(m, n);
		System.out.println("[" + m + "," + n + "] = " + c);
		System.out.println("(" + m + "," + n + ") = " + m * n / c);
	}
	private static int gcd(int a, int b) {
    
    //使用辗转相除法
		int r=a%b;
		if(r==0){
    
    
			return b;
		}else{
    
    
			return gcd(b,r);
		}
	}
}

Law 2:

import java.util.Scanner;
public class MultipleDivisor {
    
    
	/**
	 * 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int m, n, c;
		Scanner sc = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入第一个正整数:");
			m = sc.nextInt();
			if (m > 0)
				break;
		}
		while (true) {
    
    
			System.out.println("请输入第二个正整数:");
			n = sc.nextInt();
			if (n > 0)
				break;
		}
		c = gcd(m, n);
		System.out.println("[" + m + "," + n + "] = " + c);
		System.out.println("(" + m + "," + n + ") = " + m * n / c);
	}
	private static int gcd(int a, int b) {
    
    //使用辗转相减法完成的
		if (a < b) {
    
    
			int tmp;
			tmp = a;
			a = b;
			b = tmp;
		}
		if (a % b == 0)
			return b;
		else
			return gcd(b, a - b);
	}
}

2.7 [Procedure 7]

Method one:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JudgeBlankLetterEtc {
    
    
	/**
	 * 【程序7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
	 * 1.程序分析:利用while语句,条件为输入的字符不为'\n'.
	 * 
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
    
    
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入一串字符串(可包含字母、数字、空格及其它字符):");
		StringBuffer sb = new StringBuffer(br.readLine());
		int letterCount = 0;
		int blankCount = 0;
		int numberCount = 0;
		int otherCount = 0;
		for (int i = 0; i < sb.length(); i++) {
    
    
			char c = sb.charAt(i);
			if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
    
    
				letterCount++;
			} else if ('0' <= c && c <= '9') {
    
    
				numberCount++;
			} else if (' ' == c) {
    
    
				blankCount++;
			} else {
    
    
				otherCount++;
			}
		}
		System.out.println("字符串:" + sb + "中,");
		System.out.println("字母:" + letterCount);
		System.out.println("数字:" + numberCount);
		System.out.println("空格:" + blankCount);
		System.out.println("其它字符:" + otherCount);
	}
}

Law 2:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JudgeLetterBlankNumberEtc {
    
    
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception {
    
    
		// TODO Auto-generated method stub
		System.out.println("请输入一串字符:");
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String str = br.readLine();
		int lc = 0;
		int nc = 0;
		int bc = 0;
		int oc = 0;
		for (int i = 0; i < str.length(); i++) {
    
    
			char ch = str.charAt(i);
			if (Character.isLetter(ch)) {
    
    
				lc++;
			} else if (Character.isDigit(ch)) {
    
    
				nc++;
			} else if (Character.isSpace(ch)) {
    
    
				bc++;
			} else {
    
    
				oc++;
			}
		}
		System.out.println("字符串\"" + str + "\"中包含的:");
		System.out.println("字母有:" + lc + "个;");
		System.out.println("数字有:" + nc + "个;");
		System.out.println("空格有:" + bc + "个;");
		System.out.println("其它字符有:" + oc + "个;");
	}

}

2.8 [Procedure 8]

import java.util.Scanner;
public class CycleNumberAdd {
    
    
	/**
	 * 程序分析:关键是计算出每一项的值。
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int m, n;
		while (true) {
    
    
			System.out.println("请输入一个1~9之间的一个数字:");
			m = sc.nextInt();
			if (m > 0 && m < 10)
				break;
		}
		while (true) {
    
    
			System.out.println("请输入循环次数(>1,<19):");
			n = sc.nextInt();
			if (n > 1 && n < 19)
				break;
		}
		long sum = m;
		long t = m;
		String s = "" + m;
		for (int i = 0; i < n - 1; i++) {
    
    
			t = 10 * t + m;
			s += " + " + t;
			sum += t;
		}
		System.out.println(s + " = " + sum);
	}
}

2.9 [Procedure 9]

public class PerfectNumber {
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		System.out.println("1000以内,所有的完全数有:");
		for (int i = 2; i < 1001; i++) {
    
    
			String str = "1";
			int sum = 1;
			for (int j = 2; j <= i / 2; j++) {
    
    
				if (i % j == 0) {
    
    
					sum += j;
					str += " + " + j;
				}
			}
			if (i == sum) {
    
    
				System.out.println(sum + " = " + str);
			}
		}
	}

}

2.10 [Procedure 10]

public class BallBounce {
    
    
	/**
	 * 【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
	 */
	public static void main(String[] args) {
    
    
		double high = 100.0;
		double sum = 0.0;
		for (int i = 0; i < 10; i++) {
    
    
			sum += high;
			high /= 2;
		}
		System.out.println("小球10后的经过的距离为:" + sum + "米。");
		System.out.println("小球第10次落地后,再弹起的高度为:" + high + "米。");
	}

}

Guess you like

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