啊哈算法Demo
Posted 孤独的菜鸟----
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了啊哈算法Demo相关的知识,希望对你有一定的参考价值。
根据《啊哈算法》书籍,自己跟着敲的Demo,慢慢的添加,当做记录。
前1至4章,中间有部分demo没有敲。
//
// main.c
// ahasuanfa
//
// Created by pacino on 16/7/22.
// Copyright © 2016年 pacino. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
* 010101 代表第一章第一节第一个demo
*/
void test010101()
int a[10];
int i, t;
for (i = 0; i < 10; i++)
a[i] = 0;
printf("请输入数据:");
for (i = 0; i < 5; i++)
scanf("%d", &t);
a[t]++;
for (i = 0; i < 10; i++)
for (int j = 0; j < a[i] ; j++)
printf("%d\\t", i);
getchar();
getchar();
void test010102()
int n = 1000;
int a[n];
int i, t;
for (i = 0; i <= n; i++)
a[i] = 0;
printf("请输入数据:");
for (i = 0; i < 10; i++)
scanf("%d", &t);
a[t]++;
for (i = n; i >= 0; i--)
for (int j = 0; j < a[i] ; j++)
printf("%d\\t", i);
getchar();
getchar();
void test010201()
int i, j, n;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
int a[n];
printf("请输入数:\\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i; j++)
if (a[j] > a[j + 1])
a[j] ^= a[j + 1];
a[j + 1] ^= a[j];
a[j] ^= a[j + 1];
for (i = 0; i < n; i++)
printf("%d\\t", a[i]);
getchar();
getchar();
void test010202()
struct student
char name[21];
int score;
;
int i, j, n;
printf("输入一个数n,表示接下来要输入人数:\\n");
scanf("%d", &n);
struct student a[n];
struct student temp;
printf("请输入数:\\n");
for (i = 0; i < n; i++)
scanf("%s %d", a[i].name, &a[i].score);
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (a[j].score > a[j + 1].score)
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
printf("运行结果:\\n");
for (i = 0; i < n; i++)
printf("%s\\t%d\\n", a[i].name, a[i].score);
getchar();
getchar();
void quickSort010301(int a[], int left, int right)
if (left >= right)
return;
int i = left;
int j = right;
while (i != j)
while (a[left] <= a[j] && i < j)
j--;
while (a[left] >= a[i] && i < j) // 刚开始left和i是同一位置,所以必须 >=
i++;
if (i < j)
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
a[i] ^= a[left];
a[left] ^= a[i];
a[i] ^= a[left];
quickSort010301(a, left, i - 1);
quickSort010301(a, i + 1, right);
void test010301()
int i, n;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
int a[n];
printf("请输入数:\\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
quickSort010301(a, 0, n -1);
for (i = 0; i < n; i++)
printf("%d\\t", a[i]);
getchar();
getchar();
void test010401()
int i, n, temp;
int a[1000];
for (i = 0; i < 1000; i++)
a[i] = 0;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
printf("请输入数:\\n");
for (i = 0; i < n; i++)
scanf("%d", &temp);
a[temp] = 1;
for (i = 0; i < 1000; i++)
if (a[i])
printf("%d\\t", i);
getchar();
getchar();
void test010402()
int i, n;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
int a[n];
printf("请输入数:\\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
quickSort010301(a, 0, n -1);
printf("%d\\t", a[0]);
for (i = 1; i < n; i++)
if (a[i] == a[i - 1]) continue;
printf("%d\\t", a[i]);
getchar();
getchar();
void test020101()
int a[] = 6, 3, 1, 7, 5, 8, 9, 2, 4;
int head = 0;
int tail = 8;
while (head <= tail)
printf("%d\\t", a[head]);
head++;
a[++tail] = a[head];
head++;
getchar();
getchar();
void test020102()
struct queue
int data[100];
int tail;
int head;
;
struct queue q;
int a[9] = 6, 3, 1, 7, 5, 8, 9, 2, 4;
for (int i = 0; i < 9; i++)
q.data[i] = a[i];
q.head = 0;
q.tail = 8;
while (q.head <= q.tail)
printf("%d\\t", q.data[q.head]);
q.head++;
q.tail++;
q.data[q.tail] = q.data[q.head];
q.head++;
getchar();
getchar();
void test020201()
char a[255];
printf("请输入字符串\\n");
gets(a);
int length = (int)strlen(a);
int mid = length / 2;
int top = -1;
int temp[255];
for (int i = 0; i < mid; i++)
temp[++top] = a[i];
int next = length % 2 ? mid+1 : mid;
while (top > 0)
if (temp[top--] != a[next++]) break;
if (top == 0)
printf("YES");
else
printf("NO");
getchar();
void test020301()
// 重点:当赢牌的时候勿忘将标记book里面的数据初始化
/**
* 啊哈算法里面结果不同
*/
struct queque
int data[1024];
int head;
int tail;
char *name;
;
struct stack
int data[1024];
int top;
;
struct queque q1, q2;
q1.name = "小哼";
q2.name = "小哈";
q1.head = q1.tail = 0;
q2.head = q2.tail = 0;
// 初始化q1
printf("请输入小哼的牌\\n");
for (int i = 0; i < 6; i++)
scanf("%d", &q1.data[q1.tail++]);
// 初始化q2
printf("请输入小哈的牌\\n");
for (int i = 0; i < 6; i++)
scanf("%d", &q2.data[q2.tail++]);
struct stack a;
a.top = 0;
int book[10];
// 初始化标记按钮
for (int i = 0; i < 10; i++)
book[i] = 0;
while (q1.head < q1.tail && q2.head < q2.tail)
int num = 0;
// 取q1手中的牌
num = q1.data[q1.head++];
if (book[num] == 1)
q1.data[q1.tail++] = num;
while (a.top > 0)
q1.data[q1.tail++] = a.data[--a.top];
book[a.data[a.top]] = 0;
if (num == a.data[a.top]) break;
else
a.data[a.top++] = num;
book[num] = 1;
if (q1.tail == q1.head)
printf("%swin\\n", q2.name);
for (; q2.head < q2.tail; q2.head++)
printf("%d\\t", q2.data[q2.head]);
break;
num = q2.data[q2.head++];
if (book[num] == 1)
q2.data[q2.tail++] = num;
while (a.top > 0)
q2.data[q2.tail++] = a.data[--a.top];
book[a.data[a.top]] = 0;
if (num == a.data[a.top]) break;
else
a.data[a.top++] = num;
book[num] = 1;
if (q2.tail == q2.head)
printf("%swin\\n", q1.name);
for (; q1.head < q1.tail; q1.head++)
printf("%d\\t", q1.data[q1.head]);
break;
printf("\\n桌子上的纸牌\\n");
while (a.top > 0)
printf("%d\\t", a.data[--a.top]);
getchar();
getchar();
void test020401()
struct Node
int data;
struct Node *next;
;
struct Node *head = NULL, *p = NULL, *q = NULL;
int n;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
printf("请输入数:\\n");
for (int i = 0; i < n; i++)
p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
printf("申请空间失败");
else
scanf("%d", &p->data);
p->next = NULL;
if (head == NULL)
head = p;
q = head;
else
q->next = p;
q = p;
p = NULL;
p = head;
while (p != NULL)
printf("%d\\t", p->data);
p = p->next;
getchar();
getchar();
void test020402()
struct Node
int data;
struct Node *next;
;
struct Node *head = NULL, *p = NULL, *q = NULL;
int n;
printf("输入一个数n,表示接下来要输入n个数:\\n");
scanf("%d", &n);
printf("请输入数:\\n");
for (int i = 0; i < n; i++)
p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
printf("申请空间失败");
else
scanf("%d", &p->data);
p->next = NULL;
if (head == NULL)
head = p;
q = head;
else
q->next = p;
q = p;
p = NULL;
printf("输入插入数据:\\n");
p = (struct Node *)malloc((sizeof(struct Node)));
scanf("%d", &p->data);
q = head;
while (q != NULL)
if (q->next->data > p->data)
p->next = q->next;
q->next = p;
p = NULL;
break;
q = q->next;
q = head;
while (q != NULL)
printf("%d\\t", q->data);
q = q->next;
getchar();
getchar();
int countHuoChai030301(int x)
int f[] = 6, 2, 5, 5, 4, 5, 6, 3, 7, 6;
int sum = 0;
while (x / 10 >0)
sum += f[x % 10];
x /= 10;
sum += f[x];
return sum;
void test030301()
int n;
int sum = 0;
printf("输入火柴数:\\n");
scanf("%d", &n);
if (n > 24)
printf("火柴太多");
else if (n < 10)
printf("火柴太少,构不成式子");
for (int i = 0; i < 1111; i++)
for (int j = 0; j < 1111; j++)
sum = i + j;
if (sum > 1111) break;
if (countHuoChai030301(i) + countHuoChai030301(j) + countHuoChai030301(sum) == n - 4)
printf("%d + %d = %d\\n", i, j, sum);
getchar();
getchar();
void dtp040101(int *a,int *book,int step)
if (step == 10)
if (a[1]*100+a[2]*10+a[3] + a[4]*100+a[5]*10+a[6] == a[7]*100+a[8]*10+a[9])
printf("%d + %d = %d\\n", a[1]*100+a[2]*10+a[3], a[4]*100+a[5]*10+a[6], a[7]*100+a[8]*10+a[9]);
return;
for (int i = 1; i < 10; i++)
if (book[i] == 0)
book[i] = 1;
a[step] = i;
dtp040101(a, book, step + 1);
book[i] = 0;
void test040101()
int a[10] = 0;
int book[10] = 0;
dtp040101(a, book, 1);
getchar();
getchar();
void dtp040201(int *a, int *book, int startX, int startY, int endX, int endY, int row, int col, int step)
if (startX == endX && startY == endY)
printf("%d步成功\\n", step);
return;
int tempX = startX;
int tempY = startY;
for (int i = 0; i < 4; i++)
switch (i)
case 0:
tempX = startX + 1;
tempY = startY;
break;
case 1:
tempX = startX;
tempY = startY + 1;
break;
case 2:
tempX = startX - 1;
tempY = startY;
break;
case 3:
tempX = startX;
tempY = startY - 1;
break;
default:
break;
if (tempX<0 || tempX>=row || tempY<0 || tempY>=col)
continue;
int aInt = *(a+tempX*50+tempY);
int bookInt = *(book+tempX*50+tempY);
if (aInt==0 && bookInt==0)
*(a+tempX*50+tempY) = 1;
dtp040201(a, book, tempX, tempY, endX, endY, row, col, step+1);
*(a+tempX*50+tempY) = 0;
void test040201()
int i, j, endX, endY;
int m, n;
int a[50][50] = 0;
int book[50][50] = 0;
printf("初始化n行m列\\n");
scanf("%d %d", &n, &m);
printf("初始化迷宫\\n");
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &a[i][j]);
printf("读入小哈位置\\n");
scanf("%d %d", &endX, &endY);
book[0][0] = 1;
dtp040201((int *)a, (int *)book, 0, 0, endX, endY, n, m, 0);
getchar();
getchar();
void test040301()
struct Point
int x;
int y;
int step;
struct Point *next;
;
struct Queue
struct Point *head;
struct Point *tail;
;
int i, j, endX, endY;
int m, n;
int a[50][50] = 0;
int book[50][50] = 0;
printf("初始化n行m列\\n");
scanf("%d %d", &n, &m);
printf("初始化迷宫\\n");
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &a[i][j]);
printf("读入小哈位置\\n");
scanf("%d %d", &endX, &endY);
struct Point *p1 = (struct Point *)malloc(sizeof(struct Point));
p1->x = 0;
p1->y = 0;
p1->step = 0;
p1->next = NULL;
struct Queue *queue = (struct Queue *)malloc(sizeof(struct Queue));
queue->head = p1;
queue->tail = p1;
book[0][0] = 1;
do
struct Point *p2;
if (queue->head->next == NULL)
p2 = queue->head;
else
p2 = queue->head->next;
free(queue->head);
queue->head = p2;
for (int i = 0; i < 4; i++)
struct Point *p = (struct Point *)malloc(sizeof(struct Point));
p->x = p2->x;
p->y = p2->y;
p->step = p2->step;
switch (i)
case 0:
p->x += 1;
break;
case 1:
p->y += 1;
break;
case 2:
p->x -= 1;
break;
case 3:
p->y -= 1;
break;
default:
break;
if (p->x<0 || p->x>=n || p->y<0 || p->y>=m)
free(p);
p = NULL;
continue;
if (book[p->x][p->y]==0 && a[p->x][p->y]==0)
book[p->x][p->y] = 1;
p->step++;
if (p->x==endX && p->y==endY)
printf("%d步", p->step);
break;
queue->tail->next = p;
p->next = NULL;
queue->tail = p;
else
free(p);
p = NULL;
while (queue->head != queue->tail);
free(queue);
queue = NULL;
free(p1);
p1 = NULL;
getchar();
getchar();
struct Point040601
int x[1024];
int y[1024];
int top;
*p040601;
void dtp040601(int a[][50], int book[][50], int x, int y, int direction, int row, int col)
if (x<1 || x>row || y<1 || y>col || book[x][y]==1 || a[x][y]==0)
return;
book[x][y] = 1;
p040601->x[p040601->top] = x;
p040601->y[p040601->top] = y;
p040601->top++;
if (x==row && y==col)
printf("成功\\n");
for (int i = 0; i<p040601->top; i++)
printf("(%d,%d)\\t", p040601->x[i], p040601->y[i]);
printf("\\n");
return;
switch (direction)
case 0:
if (a[x][y] >=5)
dtp040601(a, book, x, y+1, 0, row, col);
else
dtp040601(a, book, x+1, y, 1, row, col);
dtp040601(a, book, x-1, y, 3, row, col);
break;
case 1:
if (a[x][y] >=5)
dtp040601(a, book, x+1, y, 1, row, col);
else
dtp040601(a, book, x, y+1, 0, row, col);
dtp040601(a, book, x, y-1, 2, row, col);
break;
case 2:
if (a[x][y] >=5)
dtp040601(a, book, x, y-1, 2, row, col);
else
dtp040601(a, book, x+1, y, 1, row, col);
dtp040601(a, book, x-1, y, 3, row, col);
break;
case 3:
if (a[x][y] >=5)
dtp040601(a, book, x-1, y, 3, row, col);
else
dtp040601(a, book, x, y+1, 0, row, col);
dtp040601(a, book, x, y-1, 2, row, col);
break;
default:
break;
book[x][y] = 0;
p040601->top--;
p040601->x[p040601->top] = x;
p040601->y[p040601->top] = y;
void test040601()
int i, j;
int m, n;
int a[50][50] = 0;
int book[50][50] = 0;
p040601 = (struct Point040601 *)malloc(sizeof(struct Point040601));
p040601->top = 0;
printf("初始化n行m列\\n");
scanf("%d %d", &n, &m);
printf("初始化水管\\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
dtp040601(a, book, 1, 1, 0, n, m);
free(p040601);
p040601 = NULL;
getchar();
getchar();
以上是关于啊哈算法Demo的主要内容,如果未能解决你的问题,请参考以下文章