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

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

蓝桥杯10道经典编程题及答案(二)
在这里插入图片描述


一、题目

1.1 【程序1】

【程序11】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

1.2 【程序2】

【程序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.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

1.3 【程序3】

【程序13】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

1.4 【程序4】

【程序14】题目:输入某年某月某日,判断这一天是这一年的第几天?

1.5 【程序5】

【程序15】题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

1.6 【程序6】

【程序16】题目:输出9*9口诀。
1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

1.7 【程序7】

【程序17】 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析:采取逆向思维的方法,从后往前推断。

1.8 【程序8】

【程序18】题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

1.9 【程序9】

【程序19】题目:打印出如下图案(菱形) 
   * 
  *** 
 ***** 
******* 
 ***** 
  *** 
   * 
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。

1.10 【程序10】

【程序20】题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。


二、答案

2.1 【程序1】

public class ArrangementNumber {
    
    
	/**
	 * 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
	 */
	public static void main(String[] args) {
    
    
		int ct = 0;
		for (int i = 1; i < 5; i++) {
    
    
			for (int j = 1; j < 5; j++) {
    
    
				if (i == j)
					continue;
				for (int k = 1; k < 5; k++) {
    
    
					if (j == k || i == k)
						continue;
					ct++;
					System.out.print((i * 100 + j * 10 + k) + " ");
				}
			}
		}
		System.out.println("\n共有:" + ct + "个不同的三位数。");
	}

}


2.2 【程序2】

import java.util.Scanner;
public class PrizeCommision {
    
    
	public static void main(String[] args) {
    
    
		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.3 【程序3】

public class SquareNumber {
    
    
	/**
		 * 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		for (int i = 1; i < 100000001; i++) {
    
    
			int sq1 = (int) Math.sqrt(i + 100);
			int sq2 = (int) Math.sqrt(i + 268);
			if ((i + 100) == sq1 * sq1 && (i + 268) == sq2 * sq2)
				System.out.println(i);
		}
	}
}

2.4 【程序4】

import java.util.Scanner;

public class YearMonthDays {
    
    
	/**
		 * 1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
	 */
	public static void main(String[] args) {
    
    
		int[] days = {
    
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int year, month, day;
		Scanner sc = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入一个正整数作为年份:");
			year = sc.nextInt();
			if (year > 0)
				break;
		}
		while (true) {
    
    
			System.out.println("请输入一个1~12之间的整数作为月份:");
			month = sc.nextInt();
			if (month > 0 && month < 13)
				break;
		}
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			days[1] = 29;
		while (true) {
    
    
			System.out.println("请输入一个1" + "~" + days[month - 1] + "之间的整数作为号数:");
			day = sc.nextInt();
			if (day > 0 && day <= days[month - 1])
				break;
		}
		int sum = day;
		for (int i = 0; i < month - 1; i++)
			sum += days[i];
		System.out.println(year + "年" + month + "月" + day + "日是今年的第" + sum
				+ "天。");
	}

}



2.5 【程序5】

import java.util.Scanner;
public class CompareTheSizeOf {
    
    
	/*
	 * 1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换
	 * ,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
	 */
	public static void main(String[] args) {
    
    
		float a,b,c,temp;
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入三个实数(在同一行时,用空格分开):");
		a=sc.nextFloat();
		b=sc.nextFloat();
		c=sc.nextFloat();
				if(a>b){
    
    
			temp=a;
			a=b;
			b=temp;
		}
		if(b>c){
    
    
			temp=b;
			b=c;
			c=temp;
		}
		if(a>b){
    
    
			temp=a;
			a=b;
			b=temp;
		}
		System.out.println(a+" <= "+b+" <= "+c);
	}
}


2.6 【程序6】

public class NineNineTable {
    
    
	/**
	 * 【程序16】 题目:输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
	 */
	public static void main(String[] args) {
    
    
		String s;
		for (int i = 1; i < 10; i++) {
    
    
			for (int j = 1; j <= i; j++) {
    
    
				s = i + "*" + j + "=" + i * j + "      ";
				System.out.print(s.substring(0, 9));
			}
			System.out.println();
		}
	}
}


2.7 【程序7】

public class MonkeyEatingPeach {
    
    
	/**
* 1.程序分析:采取逆向思维的方法,从后往前推断。
	 * 此程序包含验证!!!
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int a = 1;
		for (int i = 1; i < 10; i++) {
    
    
			a = 2 * (a + 1);
		}
		System.out.println("一共有:"+ a+ "个桃子!\n==========================================================================");
		// 以下为验证:
		int b = a;
		int i = 1;
		while (b != 1) {
    
    
			b = a / 2 - 1;
			System.out.println("第" + (i++) + "天,猴子吃了:" + (a / 2 + 1)
					+ "个桃子;还剩下" + b + "个桃子(和为:" + (a / 2 + 1 + b)
					+ ",是头一天所剩的桃子数)。");
			a = b;
		}
		System.out.println("======================================================================\n第"+ i + "天,剩下的桃子数为:" + a + "个。");
	}
}


2.8 【程序8】

public class PingpongGame {
    
    
	public static void main(String[] args) {
    
    
		char[] a = {
    
     'a', 'b', 'c' };
		char[] b = {
    
     'x', 'y', 'z' };
		int ct=0;
				for (int i = 0; i < a.length; i++) {
    
    
			for (int j = 0; j < b.length; j++) {
    
    
				if (i == 0 && j == 0)
					continue;
				if ((i == 2 && j == 0) || (i == 2 && j == 2))
					continue;
				System.out.println(a[i] + " : " + b[j]);
				ct++;
			}
		}
		System.out.println("一共有:"+ct+"场乒乓比赛!");
	}

}


2.9 【程序9】

public class DiamontPattern {
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		StringBuffer preBlank;
		StringBuffer star;
		int tmp;
		for (int i = 1; i < 8; i++) {
    
    
			preBlank = new StringBuffer();
			star = new StringBuffer();
			if (i < 5)
				tmp = i;
			else
				tmp = 8 - i;
			for (int j = 1; j < 2 * tmp; j++) {
    
    
				if (j % 2 != 0)
					preBlank.deleteCharAt(0);
				star.append('*');
			}
			System.out.print(preBlank);
			System.out.println(star);
		}
	}

}


2.10 【程序10】

public class FractionSeries {
    
    

	/**
	 * 【程序20】 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
	 * 1.程序分析:请抓住分子与分母的变化规律。
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		double fz = 2.;
		double fm = 1.;
		double he;
		double sum = fz / fm;
		System.out.print((int) fz + "/" + (int) fm);

		for (int i = 1; i < 20; i++) {
    
    
			he = fz + fm;
			fm = fz;
			fz = he;
			sum += fz / fm;
			System.out.print(" + " + (int) fz + "/" + (int) fm);
		}

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

}

猜你喜欢

转载自blog.csdn.net/youcheng_ge/article/details/130304773