同步操作将从 Java精选/Ebooks 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
程序分析:利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
程序代码如下:
package com.yoodb.util;
import java.util.Scanner;
public class Demo02 {
private static Scanner s;
public static void main(String[] args) {
s = new Scanner(System.in);
System.out.print("请输入该月的利润:(万元)");
int I = s.nextInt();
long sum = 0;
if (I <= 100000) {
sum = I / 100 * 10;
} else if (I < 200000) {
sum = (long) (100000 / 100 * 10 + (I - 100000) / 100 * 7.5);
} else if (I < 400000) {
sum = (long) (100000 / 100 * 10 + 100000 / 100 * 7.5 + (I - 200000) / 100 * 5);
} else if (I < 600000) {
sum = (long) (100000 / 100 * 10 + 100000 / 100 * 7.5 + 200000 / 100 * 5 + (I - 400000) / 100 * 3);
} else if (I < 1000000) {
sum = (long) (100000 / 100 * 10 + 100000 / 100 * 7.5 + 200000 / 100 * 5 + 200000 / 100 * 3
+ (I - 600000) / 100 * 1.5);
} else {
sum = (long) (100000 / 100 * 10 + 100000 / 100 * 7.5 + 200000 / 100 * 5 + 200000 / 100 * 3
+ 400000 / 100 * 1.5 + (I - 1000000) / 100);
}
System.out.println("该月发放的奖金为:" + sum);
}
}
运行结果如下:
请输入该月的利润:(万元)120
该月发放的奖金为:10
所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个水仙花数,因为153=1的三次方+5的三次方+3的三次方。
程序分析:
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
程序代码
public class Demo02 {
public static void main(String args[]) {
for (int i = 100; i <= 999; i++)
if (shuixianhua(i) == true)
System.out.println(i + " ");
}
public static boolean shuixianhua(int x) {
int i = 0, j = 0, k = 0;
i = x / 100;
j = (x % 100) / 10;
k = x % 10;
if (x == i * i * i + j * j * j + k * k * k)
return true;
else
return false;
}
}
运行结果
153 370 371 407
程序分析:
质数(prime number)又称素数,有无限个。
质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。
程序代码
public class Demo13 {
public static void main(String args[]) {
int sum, i;
for (sum = 2; sum <= 100; sum++) {
for (i = 2; i <= sum / 2; i++) {
if (sum % i == 0)
break;
}
if (i > sum / 2)
System.out.print(sum + " ");
}
System.out.println("都是素数");
}
}
或
public class Demo131 {
public static void main(String args[]) {
int w = 1;
for (int i = 2; i <= 100; i++) {
for (int j = 2; j < i; j++) {
w = i % j;
if (w == 0)
break;
}
if (w != 0)
System.out.print(i + " ");
}
System.out.println("都是素数");
}
}
运行结果
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 都是素数
import java.io.File;
public class ListFiles {
public static void listAll(File directory) {
if(!(directory.exists() && directory.isDirectory())) {
throw new RuntimeException("目录不存在");
}
File[] files = directory.listFiles();
for (File file : files) {
System.out.println(file.getPath() + file.getName());
if(file.isDirectory()) {
listAll(file);
}
}
}
public static void main(String[] args) {
File directory = new File("E:\\Program Files (x86)");
listAll(directory);
}
}
假设三个字符串s1、s2、s3,验证s3是否是由s1和s2交错组成的。注意是s1与s2交错组成s2字符串。
两个字符串s和t交错的定义与过程如下,其中每个字符串都会被分割成若干非空子字符串:
s=s1+s2+...+sn
t=t1+t2+...+tm
|n-m|<=1
交错是s1+t1+s2+t2+s3+t3+...或者t1+s1+t2+s2+t3+s3+...
提示:a+b意味着字符串a和b连接。
示例1
输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
输出: true
示例2
输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
输出: false
程序代码
class Solution {
public static void main(String[] args) {
String s1 = "ab", s2 = "cd", s3 = "acbd";
System.out.println(isInterleave(s1, s2, s3));
}
public static boolean isInterleave(String s1, String s2, String s3) {
if ((s1.length() + s2.length()) != s3.length())
return false;
boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];
dp[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
dp[0][i] = dp[0][i - 1] && s1.charAt(i - 1) == s3.charAt(i - 1) ? true : false;
}
for (int i = 1; i <= s2.length(); i++) {
dp[i][0] = dp[i - 1][0] && s2.charAt(i - 1) == s3.charAt(i - 1) ? true : false;
}
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = (dp[i][j - 1] && s1.charAt(j - 1) == s3.charAt(i + j - 1))
|| (dp[i - 1][j] && s2.charAt(i - 1) == s3.charAt(i + j - 1));
}
}
return dp[s2.length()][s1.length()];
}
}
运行结果
true
递归实现
当输入n时,获取斐波那契数列的第n个数的值。
public static int fibonacci(int n){
if (n == 1 || n == 2){ //特殊情况,分开讨论
return 1;
}
if (n > 2) {
return fibonacci(n - 1) + fibonacci(n - 2); //递归调用
}
return -1; //如果输入错误的n,一律返回-1
}
这种实现方式比较简单,但是效率低。当n>=40时,会发现计算时间明显变长,当n接近50时,运行窗口需等待很久才有反应。
注意:由于int的取值范围有限,最大值为(2^32)-1 = 2147483647,当n>46的时候,会发生取值范围溢出的情况,所以这里如果想要验证n>46时的计算耗时情况,请将返回值类型int改为long。
for循环实现
public static long fibonacci2(int n) {
if (n < 1) {
return -1;
}
if (n ==1 || n == 2) {
return 1;
}
long a =1l, b= 1l, c =0l; //定义三个long类型整数
for (int i = 0; i < n - 2; i++) {
c = a + b; //第3个数的值等于前两个数的和
a = b; //第2个数的值赋值给第1个数
b = c; //第3个数的值赋值给第2个数
}
return c;
}
相比第一种方式明显计算速度提高了不是一点两点,哪怕n>10000,都能瞬间完成计算。
for循环和数组方式实现
类似第二种实现方式,只不过把数据都放到数组中,可以取出斐波那契数列的第1个一直到第n个的数值。采用long类型,防止溢出。
public static long fibonacci3(int n) {
if (n < 1) {
return -1;
}
if (n == 1 || n == 2) {
return 1;
}
long[] arr = new long[n];
arr[0] = arr[1] = 1; //第一个和第二个数据特殊处理
for (int i = 2; i < n; i++) {
arr[i] = arr[i -2] + arr[i - 1];
arr[n -1] = arr[i]; //数列第n个数对应数组arr[n - 1]
}
return arr[n - 1];
}
程序分析:
利用正则表达式判断是否输入信息为数字,通过调用字符串的封装源码方法,进行逆序排序。
程序代码
import java.util.Scanner;
public class Demo10 {
private static Scanner in;
public static void main(String[] args) {
System.out.println("请输入数字:");
in = new Scanner(System.in);
String str = in.next();
if (str.matches("\\d+")) {
System.out.print("输入的是" + str.length() + "位数");
StringBuffer buf = new StringBuffer(str);
System.out.println("逆序打印:" + buf.reverse());
}
}
}
运行结果
请输入数字:
871236
输入的是6位数,逆序打印:632178
解题思路
假设一组数[ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ],如果以步长为5开始进行排序,可以将这列表放在有5列的表中来更好地描述算法,这样他们就应该看起来是这样(竖着的元素是步长组成):
13 14 94 33 82
25 59 94 65 23
45 27 73 25 39
10
然后对每列进行排序:
10 14 73 25 23
13 27 94 33 39
25 59 94 65 82
45
将上述四行数字,依序接在一起时得到:[ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ]。这时10已经移至正确位置,然后再以3为步长进行排序:
10 14 73
25 23 13
27 94 33
39 25 59
94 65 82
45
排序之后变为:
10 14 13
25 23 33
27 25 59
39 65 73
45 94 82
94
最后以1步长进行排序,此时就是简单的插入排序。
程序代码
Java中实现希尔排序算法代码如下:
package com.jingxuan.system;
import java.util.Arrays;
public class ShellSort {
public static void main(String[] args) {
int[] num = { 7, 6, 9, 3, 1, 5, 2, 4 };
System.out.println("未排序的数组:" + Arrays.toString(num));
// 增量序列的选择没有具体的公式,可以根据自己的数据取个合适的增量序列
for (int increment = num.length / 2; increment > 0; increment = increment / 2) {
// 根据增量对数组进行分组
for (int i = increment; i < num.length; i++) {
int index = i;
// 进行插入排序
// 注意:index-increment值的变化
while ((index - increment) >= 0 && num[index] < num[index - increment]) {
int temp = num[index];
num[index] = num[index - increment];
num[index - increment] = temp;
index = index - increment;
}
}
}
System.out.println("排序后的数组:" + Arrays.toString(num));
}
}
运行结果
未排序的数组:[7, 6, 9, 3, 1, 5, 2, 4]
排序后的数组:[1, 2, 3, 4, 5, 6, 7, 9]
程序分析:利用指针方法。
程序代码如下:
package com.jingxuan.system;
public class ArraysOrder {
public static void main(String[] args) {
int[] arrays = { 600, 56, 220, 122, 501 };
for (int i = arrays.length; --i >= 0;) {
for (int j = 0; j < i; j++) {
if (arrays[j] > arrays[j + 1]) {
int temp = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = temp;
}
}
}
for (int n = 0; n < arrays.length; n++)
System.out.println(arrays[n]);
}
}
运行结果如下:
56
122
220
501
600
程序代码如下:
package com.jingxuan.system;
import java.util.Scanner;
public class ArraysWeiYi {
private static Scanner s;
public static void main(String[] args) {
int N = 5;
int[] a = new int[N];
s = new Scanner(System.in);
System.out.println("请输入5个整数:");
for (int i = 0; i < N; i++) {
a[i] = s.nextInt();
}
System.out.print("你输入的数组为:");
for (int i = 0; i < N; i++) {
System.out.print(a[i] + " ");
}
System.out.print("\n请输入向后移动的位数:");
int m = s.nextInt();
int[] b = new int[m];
for (int i = 0; i < m; i++) {
b[i] = a[N - m + i];
}
for (int i = N - 1; i >= m; i--) {
a[i] = a[i - m];
}
for (int i = 0; i < m; i++) {
a[i] = b[i];
}
System.out.print("位移后的数组是:");
for (int i = 0; i < N; i++) {
System.out.print(a[i] + " ");
}
}
}
运行结果如下:
请输入5个整数:
1
2
3
4
5
你输入的数组为:1 2 3 4 5
请输入向后移动的位数:3
位移后的数组是:3 4 5 1 2
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。