1.冒泡排序。【重点】
int[] arrs= {3,656,43,76,123};
for(int i=0;i<arrs.length-1;i++) {
for(int j=0;j<arrs.length-1-i;j++) {
if(arrs[j]>arrs[j+1]) {
int temp=arrs[j];
arrs[j]=arrs[j+1];
arrs[j+1]=temp;
}
}
}
2.两个有序数组的合并。【重点】
int[] num1=new int[]{1,2,4,6,7,123,411,5334,1414141,1314141414};
int[] num1=new int[]{0,2,57,89,113,5623,6353,134134};
//变量用于存储两个集合应该被比较的索引(存入新集合就加一)
int a=0;
int b=0;
int[] num3=new int[num1.length+num2.length];
for(int i=0;i<num3.length;i++){
if(a<num1.length && b<num2.length){
if(num1[a]>num2[b]){
num3[i]=num2[b];
b++;
}else{
num3[i]=num2[a];
a++;
}
}else if(a<num1.length){
num3[i]=num1[a];
a++;
}else if(b<num2.length){
num3[i]=num2[b];
b++;
}
}
System.out.println("排序后:"+Arrays.toString(num3));
3.一个数组的倒序。【重点】
public static int[] reverse(int[] a){
int[] b=a;
for( int start=0,end-b.length-1;start<end;start++,end--){
int temp=b[start];
b[start]=b[end];
b[end]=temp;
}
return b;
}
4.计算一个正整数的正平方根。【了解】
public static double MySqrt(int value,double t){
if(value<0||t<0)
return 0;
double left=0;
double right=value;
double mid=(right+left)/2;
double offset=2*t;
while(offset>t){
double temp=mid*mid;
if(temp>value){
right=(left+right)/2;
offset=temp-value;
}
if(temp<=value){
left=(left+right)/2;
offset=value-temp;
}
mid=(left+right)/2;
}
return mid;
}
5.快速排序算法。【重点】
public class QuickSort {
private int[] array;
public QuickSort(int[] array) { this.array = array; }
public void sort() { quickSort(array, 0, array.length - 1); }
public void print() {
for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }
}
private void quickSort(int[] src, int begin, int end) {
if (begin < end) {
int key = src[begin];
int i = begin;
int j = end;
while (i < j) {
while (i < j && src[j] > key) { j--; }
if (i < j) { src[i] = src[j]; i++; }
while (i < j && src[i] < key) { i++; }
if (i < j) { src[j] = src[i]; j--; }
}
src[i] = key;
quickSort(src, begin, i - 1);
quickSort(src, i + 1, end);
}
}
}
6.二叉树的遍历算法。【了解】
节点类:
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
public TreeNode() { }
@Override
public String toString() { return "[" + val + "]"; }
}
二叉树算法类:
public class BinaryTree {
//前序遍历
public static void preOrder(TreeNode tree) {
if (tree == null) return;
System.out.printf(tree.val + "");
preOrder(tree.left);
preOrder(tree.right);
}
//非递归写法
public static void preOrder2(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> q1 = new Stack<>();
q1.push(tree);//压栈
while (!q1.empty()) {
TreeNode t1 = q1.pop();//出栈
System.out.println(t1.val);
if (t1.right != null) { q1.push(t1.right); }
if (t1.left != null) { q1.push(t1.left); }
}
}
//中序遍历
public static void inOrderTraversal(TreeNode node) {
if (node == null)
return;
inOrderTraversal(node.left);
System.out.println(node.val);
inOrderTraversal(node.right);
}
//非递归的写法
public static void inOrderTraversal2(TreeNode tree) {
Stack<TreeNode> stack = new Stack<>();
while (tree != null || !stack.isEmpty()) {
while (tree != null) {
stack.push(tree);
tree = tree.left;
}
if (!stack.isEmpty()) {
tree = stack.pop();
System.out.println(tree.val);
tree = tree.right;
}
}
}
//后续遍历
public static void postOrder(TreeNode tree) {
if (tree == null) return;
postOrder(tree.left);
postOrder(tree.right);
System.out.println(tree.val);
}
//非递归的写法
public static void postOrder2(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
s1.push(tree);
while (!s1.isEmpty()) {
tree = s1.pop();
s2.push(tree);
if (tree.left != null) { s1.push(tree.left); }
if (tree.right != null) { s1.push(tree.right); }
}
while (!s2.isEmpty()) { System.out.print(s2.pop().val + " "); }
}
//或者
public static void postOrder3(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(tree);
TreeNode c;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && tree != c.left && tree != c.right) {
stack.push(c.left);
} else if (c.right != null && tree != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().val + " ");
tree = c;
}
}
}
//BFS(宽度优先搜索(又称广度优先搜索))
public static void levelOrder(TreeNode tree) {
if (tree == null) return;
LinkedList<TreeNode> list = new LinkedList<>();//链表,这里我们可以把它看做队列
list.add(tree);//相当于把数据加入到队列尾部
while (!list.isEmpty()) {
TreeNode node = list.poll();//poll方法相当于移除队列头部的元素
System.out.println(node.val);
if (node.left != null) list.add(node.left);
if (node.right != null) list.add(node.right);
}
}
//递归的写法
public static void levelOrder(TreeNode tree) {
int depth = depth(tree);
for (int level = 0; level < depth; level++) { printLevel(tree, level); }
}
private static int depth(TreeNode tree) {
if (tree == null) return 0;
int leftDepth = depth(tree.left);
int rightDepth = depth(tree.right);
return Math.max(leftDepth, rightDepth) + 1;
}
private static void printLevel(TreeNode tree, int level) {
if (tree == null)
return;
if (level == 0) {
System.out.print(" " + tree.val);
} else {
printLevel(tree.left, level - 1);
printLevel(tree.right, level - 1);
}
}
//如果想把遍历的结果存放到list中,我们还可以这样写
public static List<List<Integer>> levelOrder(TreeNode tree) {
if (tree == null) return null;
List<List<Integer>> list = new ArrayList<>();
bfs(tree, 0, list);
return list;
}
private static void bfs(TreeNode tree, int level, List<List<Integer>> list) {
if (tree == null) return;
if (level >= list.size()) {
List<Integer> subList = new ArrayList<>();
subList.add(tree.val);
list.add(subList);
} else { list.get(level).add(tree.val); }
bfs(tree.left, level + 1, list);
bfs(tree.right, level + 1, list);
}
//DFS(深度优先搜索)
public static void treeDFS(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
System.out.println(node.val);
if (node.right != null) { stack.push(node.right); }
if (node.left != null) { stack.push(node.left); }
}
}
//递归的写法
public static void treeDFS(TreeNode root) {
if (root == null) return;
System.out.println(root.val);
treeDFS(root.left);
treeDFS(root.right);
}
}
7.时间类型转换。【掌握】
public class DateFormat{
public static void fun(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String newDate;
try{
newDate=sdf.format(new SimpleDateFormat("yyyyMMdd").parse("20121115"))
System.out.println(newDate);
}
}
public static void main(String args[]){
fun();
}
}
8.逆波兰计算器。【了解】
public class PolandNotation{
public static void main(String[] args){
//4*5-8+60+8/2
String expression="4 5 * 8 - 60 + 8 2 / +";
List<String> list=getStrList(expression);
System.out.println(list);
//计算值,得结果
int res=calc(list);
System.out.println(res);
}
public static List<String> getStrList(String exp){
String arr[]=exp.split(" ");//将字符串遍历得到数组
List<String> list=new ArrayList<>();
for(String str:arr){
list.add(str);
}
return list;
}
//计算表达式
public static int calc(List<String> list){
Stack<String> stack=new Stack<>();
//遍历list
for(int i=0;i<list.size;i++){
//正则表达式匹配是否是数字
if(list.get(i).matcher("\\d+")){
stack.push(list.get(i)); //是数字则放入栈中
}else{
int num2=Integer.parseInt(stack.pop()); //弹出数字1
int num1=Integer.parseInt(stack.pop()); //弹出数字2
int res=0;
//进行运算
if(list.get(i).equals("+")){
res=num1+num2;
}else if(list.get(i).equals("-")){
res=num1-num2;
}else if(list.get(i).equals("*")){
res=num1*num2;
}else if(list.get(i).equals("/")){
res=num1/num2;
}else{
throw new RuntimeException("不是操作符号!");
}
stack.push(""+res);
}
}
//留在栈中的值就是最后的计算表达式结果
return Integer.parseInt(stack.pop());
}
}
9.阶乘。【重点】
public class Multiply {
public static int multiply(int num) {
if (num < 0) {
System.out.println("请输入大于 0 的数!");
return -1;
} else if (num == 0 || num == 1) {
return 1;
} else {
return multiply(num - 1) * num;
}
}
public static void main(String[] args) {
System.out.println(multiply(10));
}
}
10.斐波那契数列。【了解】
问题描述:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
// 这是一个菲波拉契数列问题
public class lianxi{
public static void main(String[] args) {
System.out.println("第1个月的兔子对数: 1");
System.out.println("第2个月的兔子对数: 1");
int f1 = 1, f2 = 1, f, M=24;
for(int i=3; i<=M; i++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.println("第" + i +"个月的兔子对数: "+f2);
}
}
}
11.判断101-200之间有多少个素数,并输出所有素数。【重点】
程序分析:判断素数的方法,用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class lianxi {
public static void main(String[] args) {
int count = 0;
for(int i=101; i<200; i+=2) {
boolean b = false;
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
b = false; break;
} else {
b = true;
}
}
if(b == true) {
count ++;
System.out.println(i );
}
}
System.out.println( "素数个数是: " + count);
}
}
12.水仙花数。【了解】
问题描述:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
public class lianxi {
public static void main(String[] args) {
int b1, b2, b3;
for(int m=101; m<1000; m++) {
b3 = m / 100;
b2 = m % 100 / 10;
b1 = m % 10;
if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {
System.out.println(m+"是一个水仙花数");
}
}
}
}
13.正整数分解质因数。【了解】
问题描述:将一个正整数分解质因数,例如:输入90,打印出 90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可;
如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数,重复执行第一步;
如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print( "请键入一个正整数: ");
int n = s.nextInt();
int k=2;
System.out.print(n + "=" );
while(k <= n) {
if(k == n) {
System.out.println(n);
break;
} else if( n % k == 0) {
System.out.print(k + "*");
n = n / k;
} else {
k++;
}
}
14.最大公约数和最小公倍数。【了解】
程序分析:在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的 较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
import java.util.*;
public class lianxi {
public static void main(String[] args) {
int a ,b,m;
Scanner s = new Scanner(System.in);
System.out.print( "键入一个整数:");
a = s.nextInt();
System.out.print( "再键入一个整数: ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公约数: "+m);
System.out.println("最小公倍数: "+n);
}
}
class deff{
public int deff(int x, int y){
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
15.完数。【了解】
问题描述:一个数如果恰好等于它的因子之和,这个数就称为"完数",编程找出1000以内的所有完数,例如:6=1+2+3。
public class lianxi {
public static void main(String[] args) {
System.out.println("1到1000的完数有:");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
t = t + j;
}
}
if(t == i) {
System.out.print(i + " ");
}
}
}
}
16.完全平方数。【了解】
问题描述:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
public class lianxi {
public static void main(String[] args) {
for(int x =1; x<100000; x++) {
if(Math.sqrt(x+100) % 1 == 0) {
if(Math.sqrt(x+268) % 1 == 0) {
System.out.println(x + "加100 是一个完全平方数,再加168 又是一个完全平方数");
}
}
}
}
}
17.猴子吃桃。【了解】
问题描述:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少?
public class lianxi {
public static void main(String[] args) {
int x = 1;
for(int i=2; i<=10; i++)
x = (x+1)*2;
System.out.println("猴子第一天摘了" + x + " 个桃子");
}
}
18.乒乓球比赛。【了解】
问题描述:两个乒乓球队进行比赛,各出三人。甲队为a、b、c三人,乙队为x、y、z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x、z比,请编程序找出三队赛手的名单。
public class lianxi {
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]);
}
}
}
}
19.求1+2!+3!+...+20!的和。【重点】
public class lianxi {
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=20; i++) {
fac = fac * i;
sum += fac;
}
ystem.out.println(sum);
}
}
20.计算年龄。【了解】
问题描述:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
public class lianxi {
public static void main(String[] args) {
int age = 10;
for(int i=2; i<=5; i++) { age =age+2; }
System.out.println(age);
}
}
21.回文数。【了解】
问题描述:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a;
do{
System.out.print("请输入一个5 位正整数:");
a = s.nextInt();
}while(a<10000||a>99999);
String ss =String.valueOf(a);
char[] ch = ss.toCharArray();
if(ch[0]==ch[4]&&ch[1]==ch[3]){
System.out.println("这是一个回文数");
} else {
System.out.println("这不是一个回文数");
}
}
}
//这个更好,不限位数
public class lianxi{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean is =true;
System.out.print("请输入一个正整数:");
long a = s.nextLong();
String ss = Long.toString(a);
char[] ch = ss.toCharArray();
int j=ch.length;
for(int i=0; i<j/2; i++) {
if(ch[i]!=ch[j-i- 1])
is=false;
}
if(is==true){
System.out.println("这是一个回文数");
} else {
System.out.println("这不是一个回文数");
}
}
}
22.判断100之内的素数。【重点】
//使用除sqrt(n)的方法求出的素数不包括2 和3
public class lianxi {
public static void main(String[] args) {
boolean b =false;
System.out.print(2 + " ");
System.out.print(3 + " ");
for(int i=3; i<100; i+=2) {
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
b = false;
break;
} else{
b = true;
}
}
if(b == true) {
System.out.print(i + " ");
}
}
}
}
//该程序使用除1 位素数得2 位方法,运行效率高通用性差。
public class lianxi {
public static void main(String[] args) {
int[] a = new int[]{2, 3, 5, 7};
for(int j=0; j<4; j++)
System.out.print(a[j] + " ");
boolean b =false;
for(int i=11; i<100; i+=2) {
for(int j=0; j<4; j++) {
if(i % a[j] == 0) {
b = false; break;
} else{
b = true;
}
}
if(b == true) {
System.out.print(i + " ");
}
}
}
}
23.3*3矩阵对角线元素之和。【了解】
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[][] a = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("输入的3 * 3 矩阵是:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int sum = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i == j) {
sum += a[i][j];
}
}
}
System.out.println("对角线之和是:"+sum);
}
}
24.杨辉三角形。【了解】
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
public class lianxi{
public static void main(String[] args) {
int[][] a = new int[10][10];
for(int i=0; i<10; i++) {
a[i][i] = 1; a[i][0] = 1;
}
for(int i=2; i<10; i++) {
for(int j=1; j<i; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for(int i=0; i<10; i++) {
for(int k=0; k<2*(10-i)-1; k++) {
System.out.print(" ");
}
for(int j=0; j<=i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
25.报数游戏。【了解】
问题描述:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("请输入排成一圈的人数:");
int n = s.nextInt();
boolean[] arr = new boolean[n];
for(int i=0; i<arr.length; i++) {
arr[i] = true;
}
int leftCount = n;
int countNum = 0;
int index = 0;
while(leftCount > 1) {
if(arr[index] == true) {
countNum ++;
if(countNum == 3) {
countNum =0;
arr[index] = false;
leftCount --;
}
}
index ++;
}
index = 0;
}
for(int i=0; i<n; i++) {
if(arr[i] == true) {
System.out.println("原排在第"+(i+1)+"位的人留下了。");
}
}
}
26.猴子分桃。【了解】
问题描述:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个, 它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
public class lianxi{
public static void main (String[] args) {
int i,m,j=0,k,count;
for(i=4;i<10000;i+=4) {
count=0; m=i;
for(k=0;k<5;k++) {
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else
break;
}
i=m;
if(count==4) {
System.out.println("原有桃子"+j+" 个");
break;
}
}
}
}
27.一个偶数总能表示为两个素数之和。【了解】
//由于用除sqrt(n)的方法求出的素数不包括2 和3,
//因此在判断是否是素数程序中人为添加了一个3。
public class lianxi{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n,i;
do{
System.out.print("请输入一个大于等于6 的偶数:");
n = s.nextInt();
} while(n<6||n%2!=0);
//判断输入是否是>=6 偶数,不是,重新输入
fun fc = new fun();
for(i=2;i<=n/2;i++){
if((fc.fun(i))==1&&(fc.fun(n-i)==1)) {
int j=n-i;
System.out.println(n+" = "+i+" + "+j);
}
//输出所有可能的素数对
}
}
}
class fun{
//判断是否是素数的函数
public int fun (int a) {
int i,flag=0;
if(a==3){
flag=1;
return(flag);
}
for(i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {
flag=0;
break;
} else
flag=1;
}
return (flag) ;
//不是素数,返回0,是素数,返回1
}
}
//解法二
import java.util.*;
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
do{
System.out.print("请输入一个大于等于6 的偶数:");
n = s.nextInt();
} while(n<6||n%2!=0);
//判断输入是否是>=6 偶数,不是,重新输入
for(int i=3;i<=n/2;i+=2){
if(f un(i)&&fun(n-i)) {
}
//输出所有可能的素数对
}
}
static boolean fun (int a){
//判断是否是素数的函数
boolean flag=false;
if(a==3){
flag=true;return(flag);
}
for(int i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {
flag=false;
break;
} else
flag=true;
}
return (flag) ;
}
}
28.选择排序。【重点】
/**
* 选择排序
* 在未排序序列中找到最小元素,存放到排序序列的起始位置。
* 再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。
* 以此类推,直到所有元素均排序完毕。
*/
public static void selectSort(int[] numbers) {
int size = numbers.length, temp;
for (int i = 0; i < size; i++) {
int k = i;
for (int j = size - 1; j >i; j--) {
if (numbers[j] < numbers[k])
k = j;
}
temp = numbers[i];
numbers[i] = numbers[k];
numbers[k] = temp;
}
}
29.插入排序。【重点】
/**
* 插入排序
* 从第一个元素开始,该元素可以认为已经被排序
* 取出下一个元素,在已经排序的元素序列中从后向前扫描
* 如果该元素(已排序)大于新元素,将该元素移到下一位置
* 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
* 将新元素插入到该位置中
* 重复步骤2
*/
public static void insertSort(int[] numbers) {
int size = numbers.length, temp, j;
for(int i=1; i<size; i++) {
temp = numbers[i];
for(j = i; j > 0 && temp < numbers[j-1]; j--)
numbers[j] = numbers[j-1];
numbers[j] = temp;
}
}