同步操作将从 Java精选/Ebooks 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
程序分析
可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。
程序代码
import java.util.Arrays;
import java.util.Scanner;
public class Demo14 {
private static Scanner in;
public static void main(String[] args) {
System.out.println("请输入10个数:");
in = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = in.nextInt();
}
System.out.println("原数组为:");
for (int x : arr) {
System.out.print(x + "\t");
}
Arrays.sort(arr);
System.out.println();
System.out.println("排序后为:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
运行结果
请输入10个数:
43 343 98 93 329 23 12 34 54 68
原数组为:
43 343 98 93 329 23 12 34 54 68
排序后为:
12 23 34 43 54 68 93 98 329 343
程序分析:
对n进行分解质因数,应先找到一个最小的质数i,然后按下述步骤完成:
1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
2)如果n > i,但n能被i整除,则应打印出i的值,并用n除以i的商,作为新的正整数你,重复执行第一步。
3)如果n不能被i整除,则用i+1作为i的值,重复执行第一步。
程序代码
import java.util.Scanner;
public class Demo03 {
private static Scanner in;
public static void fenjie(int n) {
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
System.out.print(i);
if(n!=i){
System.out.print("*");
}
fenjie(n/i);
}
}
System.exit(0);
}
public static void main(String[] args) {
in = new Scanner(System.in);
System.out.println("请输入N的值:");
int n = in.nextInt();
System.out.print( "分解质因数:" + n +"=");
fenjie(n);
}
}
运行结果
请输入N的值:
100
分解质因数:100=2*2*5*5
树是一种重要的非线性数据结构,它是数据元素(在树中称为结点)按分支关系组织起来的结构,类似自然界中的真实的树。
树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都可用树形象表示。
树在计算机领域中也得到广泛应用,如在编译源程序时,可用树表示源程序的语法结构。又如在数据库系统中,树型结构也是信息的重要组织形式之一。一切具有层次关系的问题都可用树来描述。
树是由一个或多个结点组成的有限集合:
1)必有一个特定的称为根(ROOT)的结点;
2)剩下的结点被分成n>=0个互不相交的集合T1、T2、......Tn,而且, 这些集合的每一个又都是树。树T1、T2、......Tn被称作根的子树(Subtree)。
树的递归定义如下:
1、至少有一个结点,称为根;
2、其它是互不相交的子树。
树结构的特点是每一个结点都可以有不止一个直接后继,除根结点外的所有结点都有且只有一个直接前趋。
程序分析:利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
程序代码如下:
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
程序分析:
根据不同队员进行对比,如果不符合条件,遍历另一队员进行判断。
程序代码
public class Demo07 {
static char[] m = { 'a', 'b', 'c' };
static char[] n = { 'x', 'y', 'z' };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++) {
if (m[i] == 'a' && n[j] == 'x') {
continue;
} else if (m[i] == 'a' && n[j] == 'y') {
continue;
} else if ((m[i] == 'c' && n[j] == 'x')
|| (m[i] == 'c' && n[j] == 'z')) {
continue;
} else if ((m[i] == 'b' && n[j] == 'z')
|| (m[i] == 'b' && n[j] == 'y')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
或
public class Demo071 {
public String a, b, c;
public Demo071(String a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
public static void main(String[] args) {
Demo071 arr_a = new Demo071("a", "b", "c");
String[] b = { "x", "y", "z" };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
Demo071 arr_b = new Demo071(b[i], b[j], b[k]);
if (!arr_b.a.equals(arr_b.b) & !arr_b.b.equals(arr_b.c)
& !arr_b.c.equals(arr_b.a) & !arr_b.a.equals("x")
& !arr_b.c.equals("x") & !arr_b.c.equals("z")) {
System.out.println(arr_a.a + " vs " + arr_b.a);
System.out.println(arr_a.b + " vs " + arr_b.b);
System.out.println(arr_a.c + " vs " + arr_b.c);
}
}
}
}
}
}
运行结果
a vs z
b vs x
c vs y
程序分析:
我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
程序代码
import java.util.Arrays;
import java.util.Scanner;
public class Demo05 {
private static Scanner in;
public static void main(String[] args) {
System.out.println("请输入三个数:");
in = new Scanner(System.in);
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
for (int i=0;i<arr.length;i++) {
System.out.print(arr[i] + " ");
}
}
}
或
import java.util.Scanner;
public class Demo051 {
private static Scanner in;
public static void main(String[] args) {
System.out.println("请输入三个数:");
in = new Scanner(System.in);
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
arr[i] = in.nextInt();
}
int x = arr[0], y = arr[1], z = arr[2];
if (x > y) {
int t = x;
x = y;
y = t;
}
if (x > z) {
int t = x;
x = z;
z = t;
}
if (y > z) {
int t = y;
y = z;
z = t;
}
System.out.print(x + " " + y + " " + z);
}
}
运行结果
请输入三个数:
4
8
6
4 6 8
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上168后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:
程序代码如下:
package com.yoodb.util;
public class Demo03 {
public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
int m = (int) Math.sqrt((i + 100));
int n = (int) Math.sqrt((i + 100 + 168));
if (m * m == i + 100 && n * n == i + 100 + 168) {
System.out.println("这个数是" + i);
}
}
}
}
运行结果如下:
这个数是21
这个数是261
平衡二叉树是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
构造与调整方法平衡二叉树的常用算法有红黑树、AVL、Treap等。
最小二叉平衡树的节点的公式如下 F(n)=F(n-1)+F(n-2)+1 这个类似于一个递归的数列,可以参考Fibonacci数列,1是根节点,F(n-1)是左子树的节点数量,F(n-2)是右子树的节点数量。
平衡二叉树是对二叉排序树的优化,防止二叉排序树在最坏情况(插入顺序恰好是有序的)下平均查找时间为n,二叉排序树在此时形如一个单链表,而平衡二叉树查找元素的次数不超过树的深度,时间复杂度为logN。
程序分析:
利用正则表达式判断是否输入信息为数字,通过调用字符串的封装源码方法,进行逆序排序。
程序代码
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
程序分析:
质数(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 都是素数
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。