From 98ed4336bcf0af5281fcb2277215eb8930a39566 Mon Sep 17 00:00:00 2001 From: nonstriater <510495266@qq.com> Date: Sun, 18 May 2014 13:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=80=E4=BA=9BC=E7=89=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 二叉树/README.md | 43 +++++++++++++++++- 算法问题选编/二叉树.md | 86 ++++++++++++++++------------------- 算法问题选编/测试代码/20.c | 13 ++++++ 算法问题选编/测试代码/21.c | 9 ++++ 算法问题选编/测试代码/22.c | 10 ++++ 算法问题选编/测试代码/23.c | 16 +++++++ 算法问题选编/测试代码/24.c | 11 +++++ 算法问题选编/测试代码/25.c | 18 ++++++++ 算法问题选编/测试代码/26.c | 11 +++++ 算法问题选编/测试代码/27.c | 10 ++++ 算法问题选编/测试代码/28.c | 10 ++++ 算法问题选编/测试代码/bt1.c | 27 +++++++++++ 算法问题选编/测试代码/c1.c | 23 ++++++++++ 算法问题选编/测试代码/c10.c | 10 ++++ 算法问题选编/测试代码/c11-2.c | 20 ++++++++ 算法问题选编/测试代码/c11.c | 18 ++++++++ 算法问题选编/测试代码/c12.c | 16 +++++++ 算法问题选编/测试代码/c13-1.c | 1 + 算法问题选编/测试代码/c13-2.c | 19 ++++++++ 算法问题选编/测试代码/c14.c | 16 +++++++ 算法问题选编/测试代码/c15.c | 17 +++++++ 算法问题选编/测试代码/c16.c | 38 ++++++++++++++++ 算法问题选编/测试代码/c17.c | 31 +++++++++++++ 算法问题选编/测试代码/c18.c | 12 +++++ 算法问题选编/测试代码/c19.c | 23 ++++++++++ 算法问题选编/测试代码/c2.c | 40 ++++++++++++++++ 算法问题选编/测试代码/c3.c | 16 +++++++ 算法问题选编/测试代码/c4.c | 24 ++++++++++ 算法问题选编/测试代码/c5.c | 16 +++++++ 算法问题选编/测试代码/c6.c | 20 ++++++++ 算法问题选编/测试代码/c7.c | 8 ++++ 算法问题选编/测试代码/c8.c | 20 ++++++++ 算法问题选编/测试代码/c9.c | 13 ++++++ 33 files changed, 618 insertions(+), 47 deletions(-) create mode 100644 算法问题选编/测试代码/20.c create mode 100644 算法问题选编/测试代码/21.c create mode 100644 算法问题选编/测试代码/22.c create mode 100644 算法问题选编/测试代码/23.c create mode 100644 算法问题选编/测试代码/24.c create mode 100644 算法问题选编/测试代码/25.c create mode 100644 算法问题选编/测试代码/26.c create mode 100644 算法问题选编/测试代码/27.c create mode 100644 算法问题选编/测试代码/28.c create mode 100644 算法问题选编/测试代码/bt1.c create mode 100644 算法问题选编/测试代码/c1.c create mode 100644 算法问题选编/测试代码/c10.c create mode 100644 算法问题选编/测试代码/c11-2.c create mode 100644 算法问题选编/测试代码/c11.c create mode 100644 算法问题选编/测试代码/c12.c create mode 100644 算法问题选编/测试代码/c13-1.c create mode 100644 算法问题选编/测试代码/c13-2.c create mode 100644 算法问题选编/测试代码/c14.c create mode 100644 算法问题选编/测试代码/c15.c create mode 100644 算法问题选编/测试代码/c16.c create mode 100644 算法问题选编/测试代码/c17.c create mode 100644 算法问题选编/测试代码/c18.c create mode 100644 算法问题选编/测试代码/c19.c create mode 100644 算法问题选编/测试代码/c2.c create mode 100644 算法问题选编/测试代码/c3.c create mode 100644 算法问题选编/测试代码/c4.c create mode 100644 算法问题选编/测试代码/c5.c create mode 100644 算法问题选编/测试代码/c6.c create mode 100644 算法问题选编/测试代码/c7.c create mode 100644 算法问题选编/测试代码/c8.c create mode 100644 算法问题选编/测试代码/c9.c diff --git a/二叉树/README.md b/二叉树/README.md index 880bce3..2457e33 100644 --- a/二叉树/README.md +++ b/二叉树/README.md @@ -142,12 +142,53 @@ n个节点的完全二叉树,其查找,删除的复杂度都是O(logN),但 ## R树 -## 赫夫曼编码 +## 赫夫曼编码 Huffman + +这是一个经典的压缩算法。通过`字符出现的频率`,`优先级`,`二叉树`进行的压缩算法。 + +对一个字符串,计算每个字符出现的次数,把这些字符放到优先队列(priority queue) +这这个priority queue转出二叉树 + +需要一个字符编码表来解码,通过二叉树建立huffman编码和解码的字典表 +原始串: +二级制编码: +huffman编码: +###存储结构和基本操作 +struct node{ + char *huffCode; // 叶子节点的huff编码 + int weight; + struct node *left,right; +} + +*构建赫夫曼树* + +原则:出现频率越多的会在越上层,编码也越短,出现频率越少的在越下层,编码也越长。 +不存在某一个编码是另一个编码的前缀,字符都在叶节点上,所以不会存在一个编码是另一个编码的前缀 +二叉树每个节点要么是叶子节点,要么是双分支节点(且左分支编码为0,右分支编码为1) + + +*压缩* + +1. 扫描输入文件,统计各个字符出现的次数,对结构排序 (hash统计每个字符出现的次数) +2. 根据排序结构,构建赫夫曼树 (贪心策略,每次选频率值最低的2个节点合并,需要优先队列帮组(priority queue,又叫最小堆)) +3. 对树进行遍历(左分支编码为0,右分支编码为1),得到各个字符的huffman编码,存到hash表中(这个就是编解码表,也可直接存储到节点中,如上面的char *huffCode) +4. 重新对文件扫描,根据hash表进行压缩 + +压缩的文件为了能够解压缩,需要一个文件头,用来重建赫夫曼树,包括: +被编码的文本长度 unsigned int size +字符频率表 unsigned char freqs[NUM_CHARS] + +*解压缩* + +1. 读取文件头 +2. 遍历编码后的bits,从赫夫曼树的根节点出发,遇到0,进入左子树,遇到1进入右子树,直到叶节点 + + ## 字典树trie(前缀树,单词查找树) diff --git a/算法问题选编/二叉树.md b/算法问题选编/二叉树.md index e6741ee..ceb9436 100644 --- a/算法问题选编/二叉树.md +++ b/算法问题选编/二叉树.md @@ -2,19 +2,18 @@ 把二元查找树转变成排序的双向链表 -输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 -要求不能创建任何新的结点,只调整指针的指向。 +输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求`不能创建任何新的结点`,只调整指针的指向。 10 - / / - 6 14 - / / / / + / \ + 6 14 +/\ / \ 4 8 12 16 - 转换成双向链表 -4=6=8=10=12=14=16。 + +转换成双向链表 4=6=8=10=12=14=16。 - 首先我们定义的二元查找树 节点的数据结构如下: +首先我们定义的二元查找树 节点的数据结构如下: struct BSTreeNode { int m_nValue; // value of node @@ -22,24 +21,25 @@ BSTreeNode *m_pRight; // right child of node }; +分析:题目要求不能创建任何节点,也就是只能调整各个节点的指向 +思路一: -4.在二元树中找出和为某一值的所有路径 +在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数22和如下二元树 10 - / / + / \ 5 12 - / / + /\ 4 7 则打印出两条路径:10, 12和10, 5, 7。 二元树节点的数据结构定义为: - struct BinaryTreeNode // a node in the binary tree { int m_nValue; // value of node @@ -47,16 +47,19 @@ BinaryTreeNode *m_pLeft; // left child of node BinaryTreeNode *m_pRight; // right child of node }; +1. 累加当前节点, + + 判断整数序列是不是二元查找树的后序遍历结果 题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。 如果是返回true,否则返回false。 例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: - 8 - / / - 6 10 - / / / / + 8 + / \ + 6 10 + /\ / \ 5 7 9 11 因此返回true。 如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。 @@ -65,32 +68,27 @@ BinaryTreeNode *m_pRight; // right child of node -设计一个算法,找出二叉树上任意两个结点的最近共同父结点。 -复杂度如果是O(n2)则不得分。 - -一棵排序二叉树,令 f=(最大值+最小值)/2, -设计一个算法,找出距离f值最近、大于f值的结点。 +一棵排序二叉树,令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。 复杂度如果是O(n2)则不得分。 -题目:输入一颗二元查找树,将该树转换为它的镜像, -即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 -用递归和循环两种方法完成树的镜像转换。 +题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。 例如输入: - 8 - / / + 8 + / \ 6 10 - // // + /\ / \ 5 7 9 11 输出: - 8 - / / - 10 6 - // // + + 8 + / \ + 10 6 + /\ /\ 11 9 7 5 定义二元查找树的结点为: @@ -104,13 +102,12 @@ struct BSTreeNode // a node in the binary search tree (BST) -题目(微软): 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。 例如输入 - 8 - / / - 6 10 -/ / / / + 8 + /\ + 6 10 +/ \ /\ 5 7 9 11 输出8 6 10 5 7 9 11。 @@ -122,16 +119,13 @@ struct BSTreeNode // a node in the binary search tree (BST) 二元树的深度。 -题目:输入一棵二元树的根结点,求该树的深度。 - -从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 - +题目:输入一棵二元树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 例如:输入二元树: 10 - / / + / \ 6 14 - / / / + / / \ 4 12 16 输出该树的深度3。 @@ -154,7 +148,9 @@ struct SBinaryTreeNode // a node of the binary tree 怎样编写一个程序,把一个有序整数数组放到二叉树中? + 分析:本题考察二叉搜索树的建树方法,简单的递归结构。 + 关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。 而,学会把递归改称非递归也是一种必要的技术。 毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。 @@ -166,17 +162,15 @@ struct SBinaryTreeNode // a node of the binary tree 递归和非递归俩种方法实现二叉树的前序遍历。 -怎样编写一个程序,把一个有序整数数组放到二叉树中? - -怎样从顶部开始逐层打印二叉树结点数据?请编程。 -有一棵树(树上结点为字符串或者整数),请写代码将树的结构和数据写到一个文件中,并能通过读取该文件恢复树结构 。 +有一棵树(树上结点为字符串或者整数),请写代码将树的结构和数据写到一个文件中,并能通过读取该文件恢复树结构。 +设计一个算法,找出二叉树上任意两个结点的最近共同父结点。复杂度如果是O(n2)则不得分 二叉树两个结点的最低共同父结点 题目:二叉树的结点定义如下: diff --git a/算法问题选编/测试代码/20.c b/算法问题选编/测试代码/20.c new file mode 100644 index 0000000..4932833 --- /dev/null +++ b/算法问题选编/测试代码/20.c @@ -0,0 +1,13 @@ +#include +#include + #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) + int main() + { + int y = 100; + int *p; + p = malloc(sizeof(int)); + *p = 10; + y = y / *p; + PrintInt(y); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/21.c b/算法问题选编/测试代码/21.c new file mode 100644 index 0000000..bcc4be6 --- /dev/null +++ b/算法问题选编/测试代码/21.c @@ -0,0 +1,9 @@ +#include + int main() + { + int day,month,year; + printf("Enter the date (dd-mm-yyyy) format including -'s:"); + scanf("%d-%d-%d",&day,&month,&year); + printf("The date you have entered is %d-%d-%d\n",day,month,year); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/22.c b/算法问题选编/测试代码/22.c new file mode 100644 index 0000000..de42ef0 --- /dev/null +++ b/算法问题选编/测试代码/22.c @@ -0,0 +1,10 @@ +#include + int main() + { + int n; + printf("Enter a number:\n"); + scanf("%d",&n); // & + + printf("You entered %d \n",n); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/23.c b/算法问题选编/测试代码/23.c new file mode 100644 index 0000000..d51ce99 --- /dev/null +++ b/算法问题选编/测试代码/23.c @@ -0,0 +1,16 @@ +#include + int main() + { + int cnt = 5, a; + + do { + a /= cnt; + } while (cnt --); + + printf ("%d\n", a); + return 0; + } + + /// cnt==1 时,会发生除0错误 + + \ No newline at end of file diff --git a/算法问题选编/测试代码/24.c b/算法问题选编/测试代码/24.c new file mode 100644 index 0000000..08d5319 --- /dev/null +++ b/算法问题选编/测试代码/24.c @@ -0,0 +1,11 @@ + #include + int main() + { + int i = 6; + if( ((++i < 7) && ( i++/6)) || (++i <= 9)) + ; + printf("%d\n",i); + return 0; + } + + // && 优先级大于 || \ No newline at end of file diff --git a/算法问题选编/测试代码/25.c b/算法问题选编/测试代码/25.c new file mode 100644 index 0000000..c7569ad --- /dev/null +++ b/算法问题选编/测试代码/25.c @@ -0,0 +1,18 @@ +#include + #include + #define SIZE 15 + int main() + { + int *a, i; + + a = malloc(SIZE*sizeof(int)); + + for (i=0; i + int main() + { + char dummy[80]; + printf("Enter a string:\n"); + scanf("%[^a]",dummy); + printf("%s\n",dummy); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/27.c b/算法问题选编/测试代码/27.c new file mode 100644 index 0000000..31489d6 --- /dev/null +++ b/算法问题选编/测试代码/27.c @@ -0,0 +1,10 @@ + #include + int main() + { + int a=3, b = 5; + + printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]); + printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"], + 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/28.c b/算法问题选编/测试代码/28.c new file mode 100644 index 0000000..b87b406 --- /dev/null +++ b/算法问题选编/测试代码/28.c @@ -0,0 +1,10 @@ +#include +#include + int main() + { + int* ptr1,*ptr2; + ptr1 = malloc(sizeof(int)); + ptr2 = ptr1; + *ptr2 = 10; + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/bt1.c b/算法问题选编/测试代码/bt1.c new file mode 100644 index 0000000..8816c9e --- /dev/null +++ b/算法问题选编/测试代码/bt1.c @@ -0,0 +1,27 @@ + +/* +二叉查找树变成双向链表 +*/ + +strcut BSTreeNode{ + + int m_nValue; + BSTreeNode *m_pLeft,m_pRight; + +}BSTree; + +void convertDoubleLinks(BSTree *root){ + + +} + + +int main(int argc, char const *argv[]) +{ + + return 0; +} + + + + diff --git a/算法问题选编/测试代码/c1.c b/算法问题选编/测试代码/c1.c new file mode 100644 index 0000000..7bff897 --- /dev/null +++ b/算法问题选编/测试代码/c1.c @@ -0,0 +1,23 @@ +// FROM:http://www.gowrikumar.com/c/ + +#include + + #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) + int array[] = {23,34,12,17,204,99,16}; + + int main() + { + int d; + int n = TOTAL_ELEMENTS-2; + for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) + printf("%d\n",array[d+1]); + + return 0; + } + + + /* + +d 是有符号数,sizeof()计算为无符号数,比较时d会转为无符号数,变成一个大数 + + */ \ No newline at end of file diff --git a/算法问题选编/测试代码/c10.c b/算法问题选编/测试代码/c10.c new file mode 100644 index 0000000..7dfe9dd --- /dev/null +++ b/算法问题选编/测试代码/c10.c @@ -0,0 +1,10 @@ +#include +int main() +{ + int i=43; + printf("%d\n",printf("%d",printf("%d",i))); + return 0; +} + + +// printf 返回值是打印数据的长度 4321 diff --git a/算法问题选编/测试代码/c11-2.c b/算法问题选编/测试代码/c11-2.c new file mode 100644 index 0000000..fda7caf --- /dev/null +++ b/算法问题选编/测试代码/c11-2.c @@ -0,0 +1,20 @@ +#include + void foobar1(void) + { + printf("In foobar1\n"); + } + + void foobar2() + { + printf("In foobar2\n"); + } + + int main() + { + char ch = 'a'; + foobar1(33, ch); + foobar2(); + return 0; + } + + // compile error \ No newline at end of file diff --git a/算法问题选编/测试代码/c11.c b/算法问题选编/测试代码/c11.c new file mode 100644 index 0000000..4e23919 --- /dev/null +++ b/算法问题选编/测试代码/c11.c @@ -0,0 +1,18 @@ +#include + void foobar1(void) + { + printf("In foobar1\n"); + } + + void foobar2() + { + printf("In foobar2\n"); + } + + int main() + { + char ch = 'a'; + foobar1(); + foobar2(33, ch); + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/c12.c b/算法问题选编/测试代码/c12.c new file mode 100644 index 0000000..9e29c88 --- /dev/null +++ b/算法问题选编/测试代码/c12.c @@ -0,0 +1,16 @@ +// FROM:http://www.gowrikumar.com/c/ + #include + int main() + { + float a = 12.5; + printf("%d\n", a); + //printf("%d\n", *(int *)&a); + return 0; + } + +// 浮点数 转 整形 ,用强制转换(int)a + +/* +浮点数的存储 + +*/ diff --git a/算法问题选编/测试代码/c13-1.c b/算法问题选编/测试代码/c13-1.c new file mode 100644 index 0000000..9182704 --- /dev/null +++ b/算法问题选编/测试代码/c13-1.c @@ -0,0 +1 @@ +int arr[80]; \ No newline at end of file diff --git a/算法问题选编/测试代码/c13-2.c b/算法问题选编/测试代码/c13-2.c new file mode 100644 index 0000000..7b8ea97 --- /dev/null +++ b/算法问题选编/测试代码/c13-2.c @@ -0,0 +1,19 @@ + #include "c13-1.c" + + extern int *arr; + int main() + { + arr[1] = 100; + return 0; + } + + +/** +类型并不匹配,所以导致这里的arr并没有指向c13-1.c中声明的arr[80],应该修改为 extern int arr[] + +int *arr 和 int arr[80] 区别? + + + + +*/ diff --git a/算法问题选编/测试代码/c14.c b/算法问题选编/测试代码/c14.c new file mode 100644 index 0000000..8d68989 --- /dev/null +++ b/算法问题选编/测试代码/c14.c @@ -0,0 +1,16 @@ +#include + int main() + { + int a=1; + switch(a) + { int b=20; + case 1: printf("b is %d\n",b); + break; + default:printf("b is %d\n",b); + break; + } + return 0; + } + + + // In my computer ,output:b is 32767 switch中只会从case开始执行,b会随机输出 \ No newline at end of file diff --git a/算法问题选编/测试代码/c15.c b/算法问题选编/测试代码/c15.c new file mode 100644 index 0000000..d5dcc60 --- /dev/null +++ b/算法问题选编/测试代码/c15.c @@ -0,0 +1,17 @@ + +#include + #define SIZE 10 + void size(int arr[SIZE]) + { + printf("size of array is:%d\n",sizeof(arr)); + } + + int main() + { + int arr[SIZE]; + size(arr); + return 0; + } + + + // 32位机器 4, 64位机器 8 \ No newline at end of file diff --git a/算法问题选编/测试代码/c16.c b/算法问题选编/测试代码/c16.c new file mode 100644 index 0000000..a579f58 --- /dev/null +++ b/算法问题选编/测试代码/c16.c @@ -0,0 +1,38 @@ +// FROM:http://www.gowrikumar.com/c/ + +#include + #include + void Error(char* s) + { + printf(s); + return; + } + + int main() + { + int *p; + p = malloc(sizeof(int)); + if(p == NULL) + { + Error("Could not allocate the memory\n"); + Error("Quitting....\n"); + exit(1); + } + else + { + /*some stuff to use p*/ + Error("Could not allocate the memory\n"); + Error("Quitting....\n"); + Error(5); + } + return 0; + } + + /* + +潜在的问题是: + +Error函数中,如果输入的参数不是字符串,比如传一个整数5,5被转成一个内存地址,printf这时候访问就会出问题了。 +$1 = 0x5 + + */ \ No newline at end of file diff --git a/算法问题选编/测试代码/c17.c b/算法问题选编/测试代码/c17.c new file mode 100644 index 0000000..a19f4f0 --- /dev/null +++ b/算法问题选编/测试代码/c17.c @@ -0,0 +1,31 @@ + // FROM:http://www.gowrikumar.com/c/ + + #include + int main() + { + char str[80]; + scanf("%[^/n]",str); + printf("%s\n",str); + + return 0; + + char c; + scanf("%c",&c); + printf("%c\n",c); + + scanf("%c",&c); + printf("%c\n",c); + + return 0; + } + +/* + +scanf("%c",&c) 和 scanf(" %c",&c) 区别 + + + + + +*/ + diff --git a/算法问题选编/测试代码/c18.c b/算法问题选编/测试代码/c18.c new file mode 100644 index 0000000..fc2820c --- /dev/null +++ b/算法问题选编/测试代码/c18.c @@ -0,0 +1,12 @@ + #include + int main() + { + int i; + i = 10; + printf("i : %d\n",i); + printf("sizeof(i++) is: %d\n",sizeof(i++)); + printf("i : %d\n",i); + return 0; + } + +// sizeof()不会对传入的表达式计算 ,所以后面的输入还是i:10 diff --git a/算法问题选编/测试代码/c19.c b/算法问题选编/测试代码/c19.c new file mode 100644 index 0000000..f7924fc --- /dev/null +++ b/算法问题选编/测试代码/c19.c @@ -0,0 +1,23 @@ + #include + #include + + #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0])) + + #define PrintInt(expr) printf("%s:%d\n",#expr,(expr)) + int main() + { + /* The powers of 10 */ + int pot[] = { + 0001, + 0010, + 0100, + 1000 + }; + int i; + + for(i=0;i + +void OS_Solaris_print() +{ + printf("Solaris - Sun Microsystems\n"); +} + +void OS_Windows_print() +{ + printf("Windows - Microsoft\n"); + +} +void OS_HP_UX_print() +{ + printf("HP-UX - Hewlett Packard\n"); +} + +int main() +{ + int num; + printf("Enter the number (1-3):\n"); + scanf("%d",&num); + switch(num) + { + case 1: + OS_Solaris_print(); + break; + case 2: + OS_Windows_print(); + break; + case 3: + OS_HP_UX_print(); + break; + default: + printf("Hmm! only 1-3 :-)\n"); + break; + } + + return 0; +} \ No newline at end of file diff --git a/算法问题选编/测试代码/c3.c b/算法问题选编/测试代码/c3.c new file mode 100644 index 0000000..0e1eb89 --- /dev/null +++ b/算法问题选编/测试代码/c3.c @@ -0,0 +1,16 @@ +#include + +enum {false,true}; + +int main() +{ + int i=1; + do + { + printf("%d\n",i); + i++; + if(i < 15) + continue; + }while(false); + return 0; +} \ No newline at end of file diff --git a/算法问题选编/测试代码/c4.c b/算法问题选编/测试代码/c4.c new file mode 100644 index 0000000..c17bbe9 --- /dev/null +++ b/算法问题选编/测试代码/c4.c @@ -0,0 +1,24 @@ + #include + #include + int main() + { + while(1) + { + fprintf(stdout,"hello-out"); + fprintf(stderr,"hello-err"); + sleep(1); + } + return 0; + } + + + /* + +stdout 会缓冲输出 + +\n +fflush() + +setbuf(stdout,NULL) + + */ \ No newline at end of file diff --git a/算法问题选编/测试代码/c5.c b/算法问题选编/测试代码/c5.c new file mode 100644 index 0000000..6fdbfd6 --- /dev/null +++ b/算法问题选编/测试代码/c5.c @@ -0,0 +1,16 @@ + +// FROM:http://www.gowrikumar.com/c/ + #include + #define f(a,b) a##b + #define g(a) #a + #define h(a) g(a) + + int main() + { + + printf("%s\n",h(f(1,2))); + //printf("%s\n",g(f(1,2))); + return 0; + } + + diff --git a/算法问题选编/测试代码/c6.c b/算法问题选编/测试代码/c6.c new file mode 100644 index 0000000..e48f320 --- /dev/null +++ b/算法问题选编/测试代码/c6.c @@ -0,0 +1,20 @@ + #include + int main() + { + int a=10; + switch(a) + { + case '1': + printf("ONE\n"); + break; + case '2': + printf("TWO\n"); + break; + a: + printf("NONE\n"); + } + return 0; + } + + + //什么也不输出, “default”这里随便什么都没有编译错误 \ No newline at end of file diff --git a/算法问题选编/测试代码/c7.c b/算法问题选编/测试代码/c7.c new file mode 100644 index 0000000..ad7269a --- /dev/null +++ b/算法问题选编/测试代码/c7.c @@ -0,0 +1,8 @@ +#include +int main() + { + int* p; + p = (int*)malloc(sizeof(int)); + *p = 10; + return 0; + } \ No newline at end of file diff --git a/算法问题选编/测试代码/c8.c b/算法问题选编/测试代码/c8.c new file mode 100644 index 0000000..9ad56fb --- /dev/null +++ b/算法问题选编/测试代码/c8.c @@ -0,0 +1,20 @@ + +#include + +int main() +{ + float f=0.0f; + int i; + + for(i=0;i<10;i++) + f = f + 0.1f; + + if(f == 1.0f) + printf("f is 1.0 \n"); + else + printf("f is NOT 1.0\n"); + + return 0; +} + +// 浮点数判相等 diff --git a/算法问题选编/测试代码/c9.c b/算法问题选编/测试代码/c9.c new file mode 100644 index 0000000..020f101 --- /dev/null +++ b/算法问题选编/测试代码/c9.c @@ -0,0 +1,13 @@ + +#include + +int main() +{ + int a = (1,2); + printf("a : %d\n",a); + return 0; +} + +// ,运算符优先级 最低 ,括号不能少 +// +