java基础编程练习5

【程序11】题目:有1、2、3、4,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。

 

public class TestP11{
	public static void main(String[] args){
		int count = 0;
		for(int x = 1;x < 5;x++){
			for(int y =1;y<5;y++){
				for(int z=1;z<5;z++){
					if(x!=y&&x!=z&&y!=z){
						count++;
						System.out.println("the number is:"+(x*100+y*10+z));
					}
				}
			}
		}
		System.out.print("the sum is:"+count);
	}
}


 

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

import java.util.*;
public class TestP12{
	public static void main(String[] args){
		double x,sum=0;
		Scanner s = new Scanner(System.in);
		x = s.nextInt();
		if(x <= 10&& x >0){
			sum = x*0.1;
		}else if(x<=20 && x > 10){
			sum = (x-10)*0.075 + 10*0.1;
		}else if(x <=40&&x>20){
			sum = (x-20)*0.05+10*0.075+10*0.1;
		}else if(x<=60&&x>40){
			sum = (x-40)*0.03+20*0.05+10*0.075+10*0.1;
		}else if(x<=100&&x>60){
			sum = (x-60)*0.015+20*0.03+20*0.05+10*0.075+10*0.1;
		}else if(x >100){
			sum = (x-100)*0.01 + 40*0.015+20*0.03+20*0.05+10*0.075+10*0.1;
		}
		System.out.println("the sum is:"+sum);
	}
}


 

【程序13】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足
如下条件,即是结果。

public class TestP13{
	public static void main(String[] args){
		for(int i = 1;i<100000;i++){
			if(Math.sqrt(i+100)%1==0){
				if(Math.sqrt(i+268)%1==0){
					System.out.println("the number is:"+i);
				}
			}
		}
	}
}

 

【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且
输入月份大于3时需考虑多加一天。

import java.util.*;
public class TestP14{
	public static void main(String[] args){
		int day,month,year;
		int days = 0;
		int d = 0;
		Scanner s = new Scanner(System.in);
		System.out.println("input the year:");
		year = s.nextInt();
		System.out.println("input the month:");
		month = s.nextInt();
		System.out.println("input the day:");
		day = s.nextInt();
		if(year<=0||month<=0||day<=0||month>12||day>31){
			System.out.println("you input the error date,try again!");
		}
		else{
			int i;
			for(i=1;i < month;i++){
				switch (i) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						days = 31;
						break;
					case 4:
					case 6:
					case 9:
					case 11:
						days = 30;
						break;
					case 2:
						if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
							days = 29;
						} else {
							days = 28;
							break;
						}
				}
				d += days;
			}
		}
		System.out.println(year + "-" + month + "-" + day + "是这年的第" + (d+day) + "天。");
	}
}


 

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

import java.util.*;
public class TestP15{
	public static void main(String[] args){
		int x,y,z;
		int temp;
		Scanner s = new Scanner(System.in);
		System.out.println("input the number x:");
		x = s.nextInt();
		System.out.println("input the number y:");
		y = s.nextInt();
		System.out.println("input the number z:");
		z = s.nextInt();
		if(x<y&&x<z){
			System.out.println("output the number by order:"+x+"---"+y+"---"+z);
		}else if(x>y && x<z){
			temp = x;
			x = y;
			y = temp;
			System.out.println("output the number by order:"+x+"---"+y+"---"+z);
		}else if(x>y&&x>z){
			if(y>z){
				temp = z;
				z = x;
				x = temp;
				System.out.println("output the number by order:"+x+"---"+y+"---"+z);
			}else{
				temp = z;
				z = x;
				x = temp;
				temp = y;
				y = x;
				x = temp;
				System.out.println("output the number by order:"+x+"---"+y+"---"+z);
			}
		}
	}
}


 

以上5个程序, 比较简单,可以从代码中分析出来,这些程序主要是数学上的分析,只要数学上的思路理清楚了,再转换为代码就能实现。这几道题,主要是练习了if判断。代码我也已经验证过了,希望大家能进行优化。

在Java中,程序14,还可以变的更好,可以从所输入的时期直接判断,而不用一个一个的输入。具体的代码,以后再分享出来。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值