11 2012 档案

摘要:方法一:#prama pack (n) // n取0, 1, 2, 4, 8, ... Struct S{ char c; int i; };#prama pack() //结束地址对齐方法二:struct S{ char c; int i;} __attribute__((packed));//GCC的__attribute__扩展机制告诉编译器取消特定结构体在编译过程中的优化对齐 阅读全文
posted @ 2012-11-25 14:52 wouldguan 阅读(647) 评论(0) 推荐(0)
摘要:参考地址:http://blog.sina.com.cn/s/blog_6d50a05801016092.html问题:一个循环有序数组(如:3,4,5,6,7,8,9,0,1,2),不知道其最小值的位置,要查找任一数值的位置。要求算法时间复杂度为log2(n)。问题分析:我们可以把循环有序数组分为左右两部分(以mid = (low+high)/ 2为界),由循环有序数组的特点知,左右两部分必有一部分是有序的,我们可以找出有序的这部分,然后看所查找元素是否在有序部分,若在,则直接对有序部分二分查找,若不在,对无序部分递归调用查找函数#include <iostream>#inclu 阅读全文
posted @ 2012-11-15 22:00 wouldguan 阅读(332) 评论(0) 推荐(0)
摘要:服务器:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#include <netdb.h>#include "DieWithMessage.h"#define BUFSIZE 512static const int MAXPENDING = 5;int SetupTCPServerSocket(con 阅读全文
posted @ 2012-11-14 23:18 wouldguan 阅读(3481) 评论(0) 推荐(0)
摘要:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <netdb.h>#include "DieWithMessage.h"void PrintSocketAddress(const struct sockaddr *address, FILE *stream);int main(int argc, char *argv[]){ if(argc != 3) DieWithUserMessage("Parameter(s)", &q 阅读全文
posted @ 2012-11-14 21:41 wouldguan 阅读(660) 评论(0) 推荐(0)
摘要:#ifndef ST_CLASS#define ST_CLASS#include <iostream>using namespace std;class ST{ public: ST(int maxN) { head=0;} int search(int v) { return searchR(head, v); } int insert(int x) { insertR(head, x); } void show() ... 阅读全文
posted @ 2012-11-13 11:08 wouldguan 阅读(313) 评论(0) 推荐(0)
摘要:#include <stdio.h>//not find will return -1//find will return the indexint searchR(int a[], int l, int r, int key){ if(l>r) return -1; int m=(l+r)/2; if(a[m] == key) return m; if(l==r) return -1; if(key < a[m]) return searchR(a, l, m-1, key); else return searchR(a, m+... 阅读全文
posted @ 2012-11-13 10:21 wouldguan 阅读(178) 评论(0) 推荐(0)
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(j<N && a[j]<a[j+1]) j++; if(!(a[k]<a[j])) break; exch(a[k], a[j]); k = j; }}void heapsort(int a[], int l... 阅读全文
posted @ 2012-11-12 12:34 wouldguan 阅读(724) 评论(0) 推荐(0)
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixup(int a[], int k){ while(k>1 && a[k/2]<a[k]) { exch(a[k/2], a[k]); k/=2; }}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(j<N && a[j]<a[j+1]) j... 阅读全文
posted @ 2012-11-12 10:49 wouldguan 阅读(215) 评论(0) 推荐(0)
摘要:void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixup(int a[], int k){ while(k>1 && a[k/2]<a[k]) { exch(a[k/2], a[k]); k/=2; }}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(k<N && a[j]<a[j+1]) j++; if(!(a[... 阅读全文
posted @ 2012-11-12 09:50 wouldguan 阅读(204) 评论(0) 推荐(0)
摘要:#include <stdio.h>#include <stack>using namespace std;void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) brea... 阅读全文
posted @ 2012-11-11 21:13 wouldguan 阅读(654) 评论(0) 推荐(0)
摘要:#include <stdio.h>#include <stack>using namespace std;void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) brea... 阅读全文
posted @ 2012-11-11 20:37 wouldguan 阅读(318) 评论(0) 推荐(0)
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) break; if(i >= j) break; e... 阅读全文
posted @ 2012-11-11 20:08 wouldguan 阅读(307) 评论(0) 推荐(0)
摘要:#include <stdio.h>#include <time.h>void shellsort(int a[], int l, int r){ int h; //step 1 4 13 40 121 ... for(h=1; h<=(r-l)/9; h=h*3+1); for(; h>0; h=h/3) { for(int i=l+h; i<=r; i++) { int j=i; int v = a[i]; while(j>=l+h && v<a[j-h]) ... 阅读全文
posted @ 2012-11-11 14:39 wouldguan 阅读(660) 评论(0) 推荐(0)
摘要:#include <stdio.h>int max(const int a[], int l, int r){ if(l == r) return a[l]; int mid = (l+r) / 2; int u = max(a, l, mid); int v = max(a, mid+1, r); return u>v? u : v;}int main(){ int a[5] = {4,2,6,4,8}; printf("%d", max(a, 0, 4)); return 0;} 阅读全文
posted @ 2012-11-11 11:44 wouldguan 阅读(159) 评论(0) 推荐(0)
摘要:#include <stdio.h>int main(){ printf("file:%s\n", __FILE__); printf("date:%s\n", __DATE__); printf("time:%s\n", __TIME__); printf("line:%d\n", __LINE__); printf("function:%s\n", __FUNCTION__); printf("func:%s\n", __func__); return 0;} 阅读全文
posted @ 2012-11-04 23:01 wouldguan 阅读(120) 评论(0) 推荐(0)