对《啊哈!算法》做一个总结
第一章-排序
桶排序
冒泡排序
快速排序
第二章-栈、队列、链表
栈
队列
链表,这里学到了模拟链表,可以用两个数组data、right来模拟链表,其中right存储data对应位置的下一个数字的索引
第三章-枚举
枚举
求 [ ][ ][ ] +[ ][ ][ ] = [ ][ ][ ] 将数字1-9填入9个 中,每个数字只能使用1次,使得等式成立
a[0]-a[8]分别代表每个数字的百、十、个位
用book存储每个数字是否出现过,如果和为9说明每个数字都出现了,即没有重复的数字
package test;
public class MyTest {
public static void main(String[] args){
int[] a = new int[9];
for(a[0]=1;a[0]<=5;a[0]++){
for(a[1]=1;a[1]<=9;a[1]++){
for(a[2]=1;a[2]<=9;a[2]++){
for(a[3]=1;a[3]<=5;a[3]++){
for(a[4]=1;a[4]<=9;a[4]++){
for(a[5]=1;a[5]<=9;a[5]++){
for(a[6]=1;a[6]<=9;a[6]++){
for(a[7]=1;a[7]<=9;a[7]++){
for(a[8]=1;a[8]<=9;a[8]++){
int[] book = new int[10];
for(int i=0;i<9;i++){
book[a[i]] = 1;
}
int sum=0;
for(int i=1;i<10;i++){
sum += book[i];
}
int num1,num2,num3;
num1 = a[0]*100 + a[1]*10 + a[2];
num2 = a[3]*100 + a[4]*10 + a[5];
num3 = a[6]*100 + a[7]*10 + a[8];
if(sum==9 && num1 + num2 == num3){
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
}
//System.out.println("1");
}
}
}
}
}
}
}
}
}
}
}
火柴棍等式
用火柴棍摆出成立的加和等式,其中数字0-9分别需要 6 2 5 5 4 5 6 3 7 6个火柴棍,加号和等号需要2个火柴棍。
现在有任意m(<=24)根火柴棍,问可以摆出哪些公式。要求火柴棍必须用完
分析 A + B = C的话,24个火柴去掉+ = 号 则还剩20根火柴。全部摆1能摆10个,则每个数不能超过1111
写一个返回数字需要火柴棍的方法,判断数字需要的火柴棍是否==m-4
package test;
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for(int a=0;a<=1111;a++){
for(int b=0;b<=1111;b++){
int c=a+b;
if(count(a)+count(b)+count(c) == m-4){
System.out.