#include <stdio.h> #include <stdlib.h> // 第一题 // 找出N个数的第k个最大者 // 方法1:排序(冒泡),降序找出第k个值 // 方法2:选前k个点进行降序排序,后面的数进行比较, // 如果数比第k个数小则忽略, 复杂度低于方法1 #define TYPE int #define TESTBUBLESORT 1 #define TESTBLOCKCOMPARE 1 #define TESTWORDPUZZLE 1 int findk_bublesort(TYPE *pData, int N, int k); int findk_blockcompare(TYPE *pData, int N, int k); // 缺点修改了原数据 // 第二题 // wordpuzzle猜字谜游戏,在三个方向上找单词 // dirction = 0 水平方向 从左至右 // dirction = 1............右..左 // ...........2............上..下 // ...........3............下..上 // ...........4............正对角线-> // ...........5....................<- // ...........6............负对角线<- // ...........7....................-> // pData字谜 // pattern:欲猜的单词 int wordpuzzle(char *pData, char *pattern, int row, int col, int driction); int my_strlen(char *s); // 获取字符串的长度 void my_strcat(char *s, char *t); // 连接字符串 void my_strcpy(char *s, char *t); // 复制字符串 // Test Model int main() { printf("Hello world!\n"); int N = 10000; int k = N / 2; TYPE *pTestData = (TYPE *)malloc(sizeof(TYPE) * N); int i; for (i = 0; i < N; ++i) pTestData[i] = i; #ifdef TESTBUBLESORT // printf("the k = %d in N is %d\n", k, findk_bublesort(pTestData, N, k)); #endif #ifdef TESTBLOCKCOMPARE printf("the k = %d in N is %d\n", k, findk_blockcompare(pTestData, N, k)); #endif // for (i = 0; i < N; ++i) // printf("%d ", pTestData[i]); // printf("\n"); free(pTestData); #ifdef TESTWORDPUZZLE int row = 4; int col = 4; char *WorldModle = (char *)malloc(sizeof(char) * row * col); char *a1 = "this"; char *a2 = "wats"; char *a3 = "oahg"; char *a4 = "fght"; my_strcpy(WorldModle, a1); my_strcat(WorldModle, a2); my_strcat(WorldModle, a3); my_strcat(WorldModle, a4); char *pattern = "that"; int np = my_strlen(pattern); if (np > row || np > col) { fputs("the pattern size is bigger!", stderr); return -1; } for (i = 0; i < 8; ++i) { if (wordpuzzle(WorldModle, pattern, row, col, i)) { printf("find word:[%s] at dirction [%d] of wordwidget\n", pattern, i); break; } } #endif return 0; } void my_strcpy(char *s, char *t) { while (*s++ = *t++) ; } void my_strcat(char *s, char *t) { while(*s) { ++s; } my_strcpy(s, t); } int findk_bublesort(TYPE *pData, int N, int k) { // 对数据进行冒泡排序 降序 int i, j; for (i = 0; i < N; ++i) { for (j = i + 1; j < N; ++j) { if (pData[i] < pData[j]) { TYPE temp; temp = pData[i]; pData[i] = pData[j]; pData[j] = temp; } } } return pData[k - 1]; } int findk_blockcompare(TYPE *pData, int N, int k) { // 先读入前k个数进行降序排列,然后与后面的数比较 // 前k个数进行降序排列 int i, j, z; for (i = 0; i < k; ++i) { for (j = i + 1; j < k; ++j) { if (pData[i] < pData[j]) { TYPE temp; temp = pData[i]; pData[i] = pData[j]; pData[j] = temp; } } } // 与后面的数与前k个数进行比较 for (i = k; i < N; ++i) { for (j = 0; j < k; ++j) { if (pData[j] <= pData[i]) { // 大于k个数中的一个 插入新元素 for (z = k - 2; z >= j; --z) pData[z + 1] = pData[z]; // 右移元素 pData[j] = pData[i]; // 插入新元素 break; } } } return pData[k - 1]; } int wordpuzzle(char *pData, char *pattern, int row, int col, int driction) { int result = 0; int i, j; int np; int k = 0; switch (driction) { case 0: // 从水平方向从左至右找 for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { if (pData[i * col + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } k = 0; } break; case 1: // 从水平方向上从右至左找 for (i = 0; i < row; ++i) { for (j = col - 1; j >= 0; --j, --np) { if (pData[i * col + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } k = 0; } break; case 2: // 按列从上往下找 for (i = 0; i < col; ++i) { for (j = 0; j < row; ++j) { if (pData[j * row + i] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } k = 0; } break; case 3: // 按列从下往上找 for (i = 0; i < col; ++i) { for (j = row - 1; j >= 0; --j) { if (pData[i * row + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } k = 0; } break; case 4: // 按正对角线从左到右 for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { if (i == j) { if (pData[i * row + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } } } k = 0; break; case 5: // 按正对角线从右到左 for (i = row - 1; i >= 0; --i) { for (j = col - 1; j >= 0; --j) { if (i == j) { if (pData[i * row + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } } } k = 0; break; case 6: // 按负对角线从右到左 for (i = col - 1; i >= 0; --i) { for (j = 0; j < row ; --j) { if (i + j == col - 1) { if (pData[i * row + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } } } k = 0; break; case 7: // 从负对角线从左到右 for (i = row - 1; i >= 0; --i) { for (j = 0; j < col; ++j) { if (i + j == row - 1) { if (pData[i * row + j] == pattern[k]) { k++; } if (pattern[k] == '\0') result = 1; } } } k = 0; break; default: break; } return result; } int my_strlen(char *s) { int n = 0; while (*s++) ++n; return n; }