啊哈!算法
1、排序
1.1、桶排序
package 第一章;
import java.util.Scanner;
public class _1_1 {
//算是 桶排序
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int []book = new int[1001];
for(int i = 0 ; i <= 1000 ; i++){
book[i] = 0; //初始化为0
}
int t=0 ;
int n = sc.nextInt(); //输入一个数n ,要输入n个数
for(int i = 1 ; i <= n ; i++){
//读入,并进行桶排序
t = sc.nextInt(); //把每一个数读到变量 t 中
book[t]++; //进行计数,对编号为t 的桶放一个小旗子
}
for(int i = 1000 ; i >= 0 ; i--){
//依次判断编号 1000~0的桶
for(int j = 1 ; j <= book[i] ; j++){
//出现了几次就将桶的编号打印几次
System.out.printf("%d " , i);
}
}
}
}
1.2、冒泡排序
package 第一章;
import java.util.Scanner;
public class _1_1 {
//算是 桶排序
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int []book = new int[1001];
for(int i = 0 ; i <= 1000 ; i++){
book[i] = 0; //初始化为0
}
int t=0 ;
int n = sc.nextInt(); //输入一个数n ,要输入n个数
for(int i = 1 ; i <= n ; i++){
//读入,并进行桶排序
t = sc.nextInt(); //把每一个数读到变量 t 中
book[t]++; //进行计数,对编号为t 的桶放一个小旗子
}
for(int i = 1000 ; i >= 0 ; i--){
//依次判断编号 1000~0的桶
for(int j = 1 ; j <= book[i] ; j++){
//出现了几次就将桶的编号打印几次
System.out.printf("%d " , i);
}
}
}
}
1.3、快速排序
package 第一章;
import java.util.Scanner;
public class _1_3快速排序 {
static int [] a = new int[101];
static int n ;
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1 ; i <= n ; i++){
a[i] = sc.nextInt();
}
quicksort(1 ,n); //快速排序调用
for(int i = 1 ; i <= n ; i++){
System.out.printf("%d ", a[i]);
}
}
private static void quicksort(int left , int right){
if(left > right){
return ;
}
int temp = a[left]; //temp中存的就是基准数
int i = left;
int j = right;
while(i != j){
//顺序很重要,要先从右往左找
while(a[j] >= temp && i < j){
j--;
}
//再从左往右找
while(a[i] <= temp && i < j){
i++;
}
//交换两个数在数组中的位置
if(i < j){
//当哨兵 i 和哨兵 j 没有相遇时
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
//最终将基准数归位
a[left] = a[i];
a[i] = temp;
quicksort(left , i-1); //继续处理左边的,递归的进程
quicksort(i+1 , right); //继续处理右边,递归
}
}
1.4、案例:买书
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-84JN3TY3-1592641609413)(F:\算法笔记\1.4.png)]](https://i-blog.csdnimg.cn/blog_migrate/50fc5eafb1e5cc4652ee1e0b2998b14c.png#pic_center)
//方法一
package 第一章;
import java.util.Scanner;
public class _1_4去重加冒泡 {
static int[] a = new int[101];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 输入n
for (int i = 1; i <= n; i++) {
// 循环读入n个图书的 ISBN号
a[i] = sc.nextInt();
}
// 冒泡排序
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
System.out.printf("%d ", a[1]); //输出第一个数
for(int i = 2 ; i <= n ; i++){
//从2循环到n
if(a[i] != a[i-1]) {
//如果当前这个数是第一个出现则输出
System.out.printf("%d ", a[i]);
}
}
}
}
//方法二
package 第一章;
/*
* 桶排序是最快的,它的时间复杂度是
O(N+M);冒泡排序是 O(N 2 );快速排序是 O(NlogN)。
*/
import java.io.InputStream;
import java.util.Scanner;
public class _1_4桶排序去重 {
static int[] a = new int[1001];
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
a[i] = 0; // 初始化
}
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 输入n
for (int i = 1; i <= n; i++) {
// 循环读入n个图书的 ISBN号
int t = sc.nextInt();
a[t] = 1; // 标记出现过的 ISBN 号
}
for (int i = 1; i <= 1000; i++) {
// 依次判断 1~1000这个桶
if (a[i] == 1) {
// 如果这个出现过 就打印出来
System.out.printf("%d ", i);
}
}
}
}
3、枚举!很暴力
3.1、坑爹的奥数
**题目:小明在数学课上遇到一道奥数题是这样的, 3 X 6528 = 3 * X 8256 (代表括号要填入的一个数),在两个括号内填入相同的数字使得等式成立。
package 第三章;
public class _01坑爹的奥数 {
public static void main(String[] args) {
for(int i = 1 ; i <= 9 ; i++){
if((i*10+3)*6528 == (3*10+i)*8256){
System.out.println(i);
}
}
}
}
3.2、坑爹的奥数升级版
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ahuz9PTM-1592641609416)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1588723228908.png)]](https://i-blog.csdnimg.cn/blog_migrate/2260339e763e9bc8059da9ed53b9e2d5.png)
**方法一:**太多了就不写了(哈哈哈哈!)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-raKDPuLk-1592641609417)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1588723578713.png)]](https://i-blog.csdnimg.cn/blog_migrate/0f3e4f17bb1aa012f1cab76fc0719ad1.png)
方法二:
package 第三章;
public class _01_1 {
public static void main(String[] args) {
int[] a = new int[10];
int[] book = new int[10];
int i;
int sum;
int total = 0;
// 这里用 a[1] - a[9] 来代a,b,c,d,e,f,h,h,i
for (a[1] = 1; a[1] <= 9; a[1]++) {
for (a[2] = 1; a[2] <= 9; a[2]++) {
for (a[3] = 1; a[3] <= 9; 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]++) {
for (a[9] = 1; a[9] <= 9; a[9]++) {
for (i = 1; i <= 9; i++) {
// 初始化book数组
book[i] = 0;
}
for (i = 1; i <= 9; i++) {
// 如果某个数出现过就标记一下
book[a[i]] = 1;
}
// 统计共出现了多少个不同的数
sum = 0;
for (i = 1; i <= 9; i++) {
sum += book[i];
}
// 如果正好出现了9个不同的数,并且满足等式条件,则输出
if (sum == 9
&& a

这篇博客详细介绍了各种排序算法,包括桶排序、冒泡排序和快速排序,并通过买书案例进行应用说明。此外,博主探讨了枚举法解决奥数问题,如火柴棍等式,并深入讨论了深度优先搜索(DFS)和宽度优先搜索(BFS)在迷宫、炸弹人游戏和宝岛探险等问题中的应用。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



