java编程题【11-30】
程序11 :排列组合问题
题目描述:
有1、2、3、4四个数,能组成多少个互不相同且无重复数字的三位数,都是多少
代码:
public class Programming11 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 4; k++) {
if (k != i && k != j && i != j) {
int num = i * 100 + j * 10 + k;
count++;
System.out.print(num + "\t");
}
}
}
}
System.out.println("\n共能组成" + count + "个数");
}
}
程序12 :分段函数求取提成问题
题目描述:
企业发放的奖金按照利润提成。利润低于或等于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%提成,从键盘输入当月利润,求应发放奖金总数
代码:
public class Programming12 {
public static void main(String[] args) {
System.out.println("请输入公司利润:");
Scanner scanner = new Scanner(System.in);
int profits = scanner.nextInt();
double w = 0;
if (profits <= 10) {
w = profits * 0.1;
} else if (profits > 10 && profits <= 20) {
w = 10 * 0.1 + (profits - 10) * 0.075;
} else if (profits > 20 && profits <= 40) {
w = 10 * 0.1 + 10 * 0.075 + (profits - 20) * 0.05;
} else if (profits > 40 && profits <= 60) {
w = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profits - 40) * 0.03;
} else if (profits > 60 && profits <= 100) {
w = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (profits - 60) * 0.015;
} else if (profits > 100) {
w = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (profits - 100) * 0.01;
}
System.out.println("公司利润为:" + profits + "万");
System.out.println("公司应当发放的奖金总数为:" + w + "万");
}
}
程序13 :方程求解问题
题目描述:
一个整数加上100之后是一个完全平方数,加上168之后也是一个完全平方数,求这个数是多少?
代码:
计算机更擅长的是一个一个试出答案
public class Programming13 {
public static void main(String[] args) {
for (int i = -100; i < 1600; i++) {
if (Math.sqrt(i + 100) % 1 == 0 && Math.sqrt(i + 168) % 1 == 0) {
System.out.println(i);
}
}
}
}
程序14 :判断日期是一年当中的第几天
题目描述:
输入某年某月某日,判断这一天是这一年的第几天
代码:
public class Programming14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
System.out.println("请输入 日:");
int day = scanner.nextInt();
int sum = 0;
for (int i = 1; i < month; i++) {
int num = 0;
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
num = 31;
break;
case 4:
case 6:
case 9:
case 11:
num = 30;
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
num = 29;
} else {
num = 28;
}
break;
}
sum = sum + num;
}
sum = sum + day;
System.out.println(year + "年" + month + "月" + day + "日 是" + year + "年的第 " + sum + " 天,祝您开心度过每一天!");
}
}
程序15 :三个数字之间的排序
题目描述:
将x,y,z三个整数从小到大输出
代码:
public class Programming15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x = scanner.nextInt();
System.out.println("请输入第二个整数");
int y = scanner.nextInt();
System.out.println("请输入第三个整数");
int z = scanner.nextInt();
int min = x;
if (x > y) {
min = y;
if (y > z) {
min = z;
System.out.println(z + " " + y + " " + x);
} else {
if (x < z) {
System.out.println(y + " " + x + " " + z);
} else {
System.out.println(y + " " + z + " " + x);
}
}
} else {
if (x > z) {
min = z;
System.out.println(z + " " + x + " " + y);
} else {
if (z < y) {
System.out.println(x + " " + z + " " + y);
} else {
System.out.println(x + " " + y + " " + z);
}
}
}
f(8, 3, 5);
}
public static void f(int x, int y, int z) {
/**
* @description:考虑使用数组排序实现
* @param x
* @param y
* @param z
* @createDate: 2020/7/6 15:03
* @return: void
*/
int arr[] = {x, y, z};
Arrays.sort(arr);
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
}
}
程序16 :9*9 乘法表
题目描述:
规范输出9*9乘法表
代码:
public class Programming16 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println();
}
}
}
程序17 :猴子吃桃问题
题目描述:
猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃了一半,又多吃了一个。以后的每一天早上都吃了前一天剩下桃子数目的一半零一个。到第十天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少个桃子。
代码:
public class Programming17 {
public static void main(String[] args) {
int x = 1;
for (int i = 1; i <= 9; i++) {
x = (x + 1) * 2;
}
System.out.println("猴子在第一天摘了 "+x+" 个桃子。");
}
}
程序18 :条件约束性比赛名单
题目描述:
两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比赛,c说他不和x,z比,编写程序找出三队赛手的名单。
代码:
public class Programming18 {
public static void main(String[] args) {
char arr1[] = {'a', 'b', 'c'};
char arr2[] = {'x', 'y', 'z'};
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == 'a' && arr2[j] == 'x') {
continue;
} else if (arr1[i] == 'c' && (arr2[j] == 'x' || arr2[j] == 'z')) {
continue;
} else if (arr1[i] == 'a' && arr2[j] == 'y') {
continue;
} else if (arr1[i] == 'b' && arr2[j] == 'y') {
continue;
} else if (arr1[i] == 'b' && arr2[j] == 'z') {
continue;
} else {
System.out.println(arr1[i] + " VS " + arr2[j]);
}
}
}
}
}
程序19 :打印半个菱形
题目描述:
打印以下图形:
代码:
public class Programming19 {
public static void main(String[] args) {
int count = 0;
for (int i = 5; i >= 1; ) {
for (int j = 1; j <= 5; j++) {
if (j < i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
count++;
if (count < 5) {
i--;
} else if (count < 10) {
i++;
} else {
break;
}
}
}
}
程序20 :斐波那契相关数列求和
题目描述:
求数列 2/1 3/2 5/3 8/5 13/8 21/13 …的前20项和
代码:
public class Programming20 {
public static void main(String[] args) {
double x = 1, y = 2, sum = y / x, a;
for (int i = 1; i < 20; i++) {
a = x;
x = y;
y = a;
sum = sum + y / x;
}
System.out.println("该数列的前20项之和为:" + sum);
}
}
程序21 :递归求5的阶乘
题目描述:
利用递归求5的阶乘
代码:
public class Programming21 {
public static void main(String[] args) {
System.out.println(jc(5));
}
public static int jc(int num) {
int res = 1;
if (num == 1) {
res = num;
return res;
} else {
return jc(num - 1) * num;
}
}
}
程序22 :计算10-15阶乘之和
题目描述:
计算10-15阶乘之和
分析:只要计算10的阶乘即可,其余迭代
代码:
public class Programming22 {
public static void main(String[] args) {
int res = 1;
for (int i = 1; i <= 10; i++) {
res = res * i;
}
int factorial10 = res;
int factorial11 = factorial10 * 11;
int factorial12 = factorial11 * 12;
int factorial13 = factorial12 * 13;
int factorial14 = factorial13 * 14;
int factorial15 = factorial14 * 15;
int sum = factorial10 + factorial11 + factorial12 + factorial13 + factorial14 + factorial15;
System.out.println(sum);
}
}
程序23 :求1-20的阶乘之和
题目描述:
求1-20的阶乘之和
代码:
public class Programming23 {
public static void main(String[] args) {
int sum = 0;
int fac = 1;
for (int i = 1; i <= 20; i++) {
fac = fac * i;
sum = sum + fac;
}
System.out.println("1-20的阶乘之和为:" + sum);
}
}
程序24 :逆序输出整数
题目描述:
给一个不低于5位的整数,输出其位数并逆序打印其数字 使用长整型最多输入18位
代码:
public class Programming24 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num = scanner.nextLong();
int n = 0;
for (long i = num; i != 0; i = i / 10) {
n++;
System.out.print(i % 10 + " ");
}
System.out.println("\n输入的整数是" + n + "位数");
}
}
程序25 :回文数
题目描述:
输入一个五位数,判断其是否为回文数
回文数:将其数字反向排列的数与其本身相同。如12321,14541是回文数
代码:
public class Programming25 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int num = n;
int a = num % 10;
num = num / 10;
int b = num % 10;
num = num / 10;
num = num / 10;
int d = num % 10;
num = num / 10;
int e = num % 10;
if (a == e && b == d) {
System.out.println(n + "是一个回文数");
} else {
System.out.println(n + "不是一个回文数");
}
String s = Integer.toString(n);
char arr[] = s.toCharArray();
boolean res = true;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == arr[arr.length - 1 - i]) {
continue;
} else {
res = false;
}
}
if (res) {
System.out.println(n + "是一个回文数");
} else {
System.out.println(n + "不是一个回文数");
}
}
}
程序26 :星期几?
题目描述:
输入星期的第一个字母来判断是星期几,如果第一个字母一样则接着判断第二个
代码:
public class Programming26 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个字母:");
String s = scanner.nextLine();
String res = "";
switch (s) {
case "M":
res = "星期一";
break;
case "T":
System.out.println("请输入第二个字母:");
String s1 = scanner.nextLine();
if (s1 == "u") {
res = "星期二";
} else {
res = "星期四";
}
break;
case "W":
res = "星期三";
break;
case "F":
res = "星期五";
break;
case "S":
System.out.println("请输入第二个字母:");
String s2 = scanner.nextLine();
if (s2 == "a") {
res = "星期六";
} else {
res = "星期日";
}
break;
}
System.out.println(res);
}
}
程序27 :求素数的算法改进
题目描述:
求100以内的素数的另一种算法 效率高但通行率低
代码:
public class Programming27 {
public static void main(String[] args) {
int a[] = {2, 3, 5, 7};
boolean res = true;
for (int j = 0; j < 4; j++) {
System.out.print(a[j] + " ");
}
for (int i = 11; i < 100; i++) {
for (int j = 0; j < 4; j++) {
if (i % a[j] == 0) {
res = false;
break;
}
}
if (res) {
System.out.print(i + " ");
}
res = true;
}
}
}
程序28 :冒泡排序
题目描述:
对十个数进行数字排序
代码:
public class Programming28 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int arr[]=new int[10];
System.out.println("请输入十个数:");
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
程序29 :矩阵对角线之和
题目描述:
求一个3*3矩阵对角元素之和
代码:
public class Programming29 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int arr[][] = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int a = scanner.nextInt();
arr[i][j] = a;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < 3; i++) {
sum = sum + arr[i][i];
}
System.out.println("其对角线之和为: "+sum);
}
}
程序30 :折半法数组排序
题目描述:
将一个数字插入到一个已经排好序的数组中
代码:
public class Programming30 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt();
int arr[] = {1, 4, 10, 18, 23, 36, 41, 56};
int res[] = new int[arr.length + 1];
int a = 0, b = arr.length - 1, index = 0;
//折半法找到数组应该插到的位置index
for (int i = 0; i <= 8; i++) {
if (arr[(b + a) / 2] > n) {
b = (b + a) / 2;
} else if (arr[(b + a) / 2] == n) {
index = (b + a) / 2;
break;
} else {
a = (b + a) / 2;
}
if (b - a == 1) {
index = b;
break;
}
}
if (arr[arr.length - 1] <= n) {
index = arr.length;
}
if (arr[0] >= n) {
index = 0;
}
//下标index的数组值的复制
res[index] = n;
//下标index前的数组的复制
for (int j = 0; j < index; j++) {
res[j] = arr[j];
}
//下标index后的数组的复制
for (int j = res.length - 1; j > index; j--) {
res[j] = arr[j - 1];
}
//遍历输出新数组
int num = 0;
for (int each : res) {
if (num == res.length - 1) {
System.out.print(each);
} else {
System.out.print(each + ",");
}
num++;
}
}
}
}