【Java】『蓝桥杯』10道编程题及答案(五)

系列文章

【Java】『蓝桥杯』10道编程题及答案(一)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130223115

【Java】『蓝桥杯』10道编程题及答案(二)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130304773

【Java】『蓝桥杯』10道编程题及答案(三)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130305068

【Java】『蓝桥杯』10道编程题及答案(四)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130392388

【Java】『蓝桥杯』10道编程题及答案(五)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130444113



前言

我能抽象出整个世界,但是我不能抽象你。 想让你成为私有常量,这样外部函数就无法访问你。 又想让你成为全局常量,这样在我的整个生命周期都可以调用你。 可惜世上没有这样的常量,我也无法定义你,因为你在我心中是那么的具体。

哈喽大家好,本专栏为【Java】专栏,『蓝桥杯』部分,面向于初学者或者对算法感兴趣的朋友们。主要分享基础编程题,一些有趣、新颖的算法,我们要习惯于掌握解题的思路,如果对实操感兴趣,可以关注我的【C#项目】专栏。

本专栏会持续更新,不断完善。大家有任何问题,可以私信我。如果您对本专栏感兴趣,欢迎关注吧,大家一起学习,一起进步。

【Java】『蓝桥杯』10道编程题及答案(五)
在这里插入图片描述


一、题目

1.1 【程序1】

【程序41】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

1.2 【程序2】

【程序42】
题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。

1.3 【程序3】

【程序43】
题目:求0—7所能组成(1-8位)的奇数个数。

1.4 【程序4】

【程序44】
题目:一个不小于6的偶数总能表示为两个奇素数之和。

1.5 【程序5】

【程序45】
题目:判断几个9能被哪几个素数整除。

1.6 【程序6】

【程序46】
题目:两个字符串连接程序

1.7 【程序7】

【程序47】
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

1.8 【程序8】

【程序48】
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

1.9 【程序9】

【程序49】
题目:计算字符串中子串出现的次数

1.10 【程序10】

【程序50】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。


二、答案

2.1 【程序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 【程序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 【程序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 【程序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 【程序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 【程序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 【程序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 【程序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 【程序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 【程序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;
	}

}


猜你喜欢

转载自blog.csdn.net/youcheng_ge/article/details/130444113
今日推荐