星海's Blog

老头初学编程

IBM Informix ESQL/C Programmers Manual读书笔记(二)

/* Informix数据类型常量,均定义在sqltypes.h文件中。
 * 包括SQL数据类型常量。宿主C语言数据类型,例如
 * double CDOUBLETYPE 105
 * dec_t or decimal CDECIMALTYPE 107
 * fixchar CFIXCHARTYPE 108
 * string CSTRINGTYPE 109
 * date CDATETYPE 110
 * dec_t or decimal CMONEYTYPE 111
 * datetime or dtime_t CDTIMETYPE 112
 * ifx_loc_t or loc_t CLOCATORTYPE 113
*/

/*
当两个数据的类型不一致时,Informix会自动转换他们的类型。
1,比较。例如 CHAR 邮编与 INT整型比对。
2,Fetch or insert
3,算数运算。
*/

/* C TO N或 N TO C空间不足时,sqlca.sqlwarn.sqlwarn1值为W,且SQLSTATE值为
 * 01004。
 * BOOLEAN TO C时,BOOLEAN值'\01','\00'自动转换为FIXCHAR值 'T'或'F'。
 *
 * 算术转换:
 * Operand Type   Convert To
 * FLOAT          DECIMAL(17)
 * SMALLFLOAT     DECIMAL(9)
 * INTEGER        DECIMAL(10,0)
 * SMALLINT       DECIMAL(5,0)

 For example, for an arithmetic operation between a fixed-point decimal of
 DECIMAL(8,3) and a FLOAT value, Informix ESQL/C converts the FLOAT value to a
 floating-point decimal of DECIMAL(17). The arithmetic result has a data type
 of DECIMAL(17).

 Function Name      Description See
The risnull()       Checks whether a C variable is null B-191
The rsetnull()      Sets a C variable to null B-197
The rtypalign()     Aligns data on correct type boundaries B-206
The rtypmsize()     Gives the byte size of SQL data types B-209
The rtypname()      Returns the name of a specified SQL data type B-211
The rtypwidth()     Returns the minimum number of characters that a character
data type needs to avoid truncation */


/*
 * This section describes the characteristics of these data types and the differences
 *among them.

 * Table 4-1. ESQL/C Character Data Types
 * ESQL/C Character Data Type   Null Terminated     Contains Trailing Blanks
 * char                         Y                   Y
 * fixchar                      N                   Y
 * string                       Y                   Returns a trailing blank
                                                    only if the column contains an empty string.
 * varchar                      Y                   Y
 * lvarchar                     Y
 */

你可以将带有\0结束符的C字符串放入FIXCHAR变量(空间要够大)。但这不是一个好主意
,当数据库服务器将这个FIXCHAR放入列中时,终结符也被放入。
/*
 * When the IFX_PAD_VARCHAR environment variable is set to 1, the client sends
 * the VARCHAR data type with padded trailing spaces. When this environment is
 * not set (the default), the client sends the VARCHAR data type value without
 * trailing spaces. The IFX_PAD_VARCHAR environment variable must be set only at
 * the client side and is supported only with IBM Informix ESQL/C Version 9.53 and
 * 2.90 or later and IBM Informix Version 9.40 or later.
 *
 */
如果你不指定lvarchar宿主变量的大小,它等同于一字节的char类型。
如果指定size,等同于C语言同等大小的CHAR类型。
如果lvarchar宿主变量是一个指针,指针指向的数据最大可以2GB。
lvarchar类型设计为当你insert和select用户自定义或不明类型时,可以表现为字符串格
式。必须使用 ifx_var() 函数操作lvarchar指针。

/* An application can fetch data from a database column of type CHAR or
 * VARCHAR into a character (char, string, fixchar, varchar, or lvarchar) host
 * variable. If the column data does not fit into the character host variable,
 * Informix ESQL/C truncates the data. To notify the user of the truncation,
 * Informix ESQL/C performs the following actions: It sets the
 * sqlca.sqlwarn.sqlwarn1 warning flag to W and the SQLSTATE variable to
 * 01004.  It sets any indicator variable that is associated with the
 * character host variable to the size of the character data in the column.
 *
 *
 * Inserting CHAR Data An application can insert data from a character host
 * variable (char, string, fixchar, varchar, or lvarchar) into a database
 * column of type CHAR. If the value is shorter than the size of the database
 * column then the database server pads the value with blanks up to the size
 * of the column.  If the value is longer than the size of the column the
 * database server truncates the value if the database is non-ANSI. No warning
 * is generated when this truncation occurs. If the database is ANSI and the
 * value is longer than the column size then the insert fails and this error
 * is returned: -1279: Value exceeds string column length.
 *
 */

/* 以by函数开头的函数操作和返回fixed-length字符串。
 * 以rst和st(除了stchar)开头的函数,操作或返回终结符结尾的字符串。
 * rdownshit和rupshit操作有终结符的字符串,但是不返回值。
 */

Function Name   Description
bycmpr()        Compares two groups of contiguous bytes B-10
bycopy()        Copies bytes from one area to another B-12
byfill()        Fills an area you specify with a character B-13
byleng()        Counts the number of bytes in a string B-14
ldchar()        Copies a fixed-length string to a null-terminated string B-170
rdownshift()    Converts all letters to lowercase B-176
rstod()         Converts a string to a double value B-200
rstoi()         Converts a string to a short integer value B-201
rstol()         Converts a string to a 4-byte integer value B-202
rupshift()      Converts all letters to uppercase B-209
stcat()         Concatenates one string to another B-232
stchar()        Copies a null-terminated string to a fixed-length string B-233
stcmpr()        Compares two strings B-234
stcopy()        Copies one string to another string B-235
stleng()        Counts the number of bytes in a string B-235

IBM Informix ESQL/C Programmers Manual读书笔记(一)

/* 数据库系统安全主管 (DBSSO)
    控制审计子系统监视的对象以及数据库服务器可执行的操作
审计分析主管 (AAO)
    控制审计是否发生、维护审计日志文件和分析
    DBSSO要求审计的那些数据库活动a的审计记录。

必须选择定制安装设置才能启用角色分离。


安装时未选择“允许安装程序保护路径的安全",在安装之后的解决方法:
运行以下命令来生成 secure.sh 脚本:$INFORMIXIDIR/bin/onsecurity -r $INFORMIXDIR
运行 secure.sh 脚本来保护安装路径的安全:$INFORMIXDIR/tmp/secure.sh

 较新版本jre可能与ids不兼容,可以选择-javahome none来使用ids自带的jre。
sudo ./ids_install -javahome none */




/* /etc/profile.d/informix.sh中的变量设置:*/

INFORMIXDIR=/home/informix/informix
INFORMIXSERVER=ol_informix
ONCONFIG=onconfig.ol_informix
INFORMIXSQLHOSTS=/home/informix/informix/etc/sqlhosts.ol_informix
PATH=${INFORMIXDIR}/bin:${INFORMIXDIR}/extend/krakatoa/jre/bin:${PATH}
export INFORMIXDIR INFORMIXSERVER ONCONFIG INFORMIXSQLHOSTS PATH

DB_LOCALE=zh_cn.utf8
SERVER_LOCALE=zh_cn.utf8
CLIENT_LOCALE=zh_cn.utf8
GL_USEGLU=1
export DB_LOCALE SERVER_LOCALE CLIENT_LOCALE GL_USEGLU

GL_DATE="%iy/%m/%d"
DATETIME="%iY-%m-%d %H:%M:%S"
export GL_DATE DATETIME

/*
根据$INFORMIXDIR/etc/sqlhosts来修改/etc/services文件和/etc/hosts文件。

准备 Informix 配置文件:(本人是直接使用的安装过程中选择的demon_on服务器实例。

    复制 onconfig.std 模板文件。
    修改模板文件的副本。 DUMPDIR 参数的缺省值是 $INFORMIXDIR/tmp。如果在配置文件中更改此值,请确保指定计算机上的有效目录。
    将 ONCONFIG 环境变量设置为定制配置文件的名称。

要点: 只有用户 informix 或 root 用户才能初始化数据库服务器。
数据库服务器至少必须进行一次初始化。 oninit -i
覆盖现有数据库。不带-i选项则不覆盖。
*/

/* 因为esql与C都将'\'作为转义字符,且依次进过esql、c处理。所以如要查找字符串 "\"
 * (3字节)的话,应当使用 */
EXEC SQL SELECT col1 FROM tab1 WHERE col1 = ’\"\\\\\"’;

/* 默认不允许在字符串类型中加入换行符
 * 方法1:在ONCONFIG文件中配置ALLOW_NEWLINE
 * 方法2:如下: */
EXEC SQL EXECUTE procedure ifx_allow_newline(’t’);

EXEC SQL BEGIN DECLARE SECTION;
-- put host variable declarations here
EXEC SQL END DECLARE SECTION;

/* 当 DELIMIDENT 设置 YES 时,双引号字符串识别 SQL 标识符。当 DELIMIDENT
 * 没有设置 YES 时,双引号字符串被当作字符串字面值。
 *
 * 在ESQL/C中,变量类型,宿主变量区分大小写。单SQL关键字、语句表示符和游标名
 * 不区分大小写。
 *
 * 定义宿主变量作为参数。
 * 宿主变量是你在嵌入式SQL中使用的ESQL/C和C变量。SELECT 。。INTO :hostvar1,
 * :hostvar2,如果省略掉hostvar1和hostvar2之间的逗号,hostvar2就作为hostvar1的指
 * 示变量。
 *
 * ANSI C风格参数类型声明如下: 注意此时因为在行参括号之内,所以用逗号。
*/
int * foo(
EXEC SQL BEGIN DECLARE SECTION;
PARAMETER char s[20],
PARAMETER int id,
PARAMETER int (*f) (double)
EXEC SQL END DECLARE SECTION;
)
{
select fname into :s from customer
where customer_num = :id;

/* 老C K&R风格 */
f(s, id, s_size)
EXEC SQL BEGIN DECLARE SECTION;
PARAMETER char s[20];
PARAMETER int id;
EXEC SQL END DECLARE SECTION;
int s_size;
{
select fname into :s from customer
where customer_num = :id;

/* 指示变量不能使用DATETIME型或INTERVAL型。*/
:hostvar INDICATOR :indvar
:hostvar :indvar
$hostvar $indvar
/* 如果ESQL/C语句对宿主变量返回NULL值,指示变量值设为 -1。
 *
 * 如果指示变量值并没有事先定义,使用esql -icheck选项时,ESQL会设置
 * sqlca.sqlcode值。
 *
 * 如果宿主变量使用成功,指示变量设为0。
 * 如果值被截短,则指示变量值设为SQL列值的实际字节大小,且设置SQLSTATE
 * 为"01004"。同时设置SQLWARN1为W
 */
/* Make sure to terminate the line of code with a semicolon. Some additional
examples follow: ESQL/C头文件不必以.h结尾。*/
EXEC SQL include varchar.h;
EXEC SQL include sqlda;
$include sqlstype;
EXEC SQL include "C:\apps\finances\credits.h"; /*fullpath头文件用双引号括起来 */


猎头朋友的面试建议。。。。

开始面试程序员,一个坛子的猎头朋友给了几点建议,很真诚和坦率的建议噢。

1.像邮件通知面试这种东西,就国内的企业来说,大可不必理会,因为它很有可能是系统自动发送的;

2.如果一个公司迫切需要某个职位的人,他们也不会选择仅用邮件通知的方式,因为有很多人只是把简历挂在了网上而已,有时候自己的私人邮箱N久才打开一次,一般来讲HR方面会用电话通知候选人来面试,再发邮件介绍详细情况;

3.不知名的公司可以先百度或者GOOGLE查查,看有没有相关信息,起码要查查他们的官网地址啊什么的,自己可以判断一下这家公司的价值;

4.如果你怕错失机会,想去这家公司,而对方只发了个邮件的话,你可以自己打电话去该公司的HR那,了解职位情况,看看跟自己是否匹配;(虽然 HR很多时候在技术层面可能不够你专业,但他至少知道这个职位最看重哪些技能,至少有几个keyword。PS:有很多HR也是从技术转过来的,HW就有 不少这样的人)

5.要记住,你跟用人单位是平等的,在他们要求了解你的各项综合技能的同时,你也有权知道职位的详细情况。就面试时间来讲,要在你和他们都OK的时间,当然,也不是要你摆谱,只是这个东西可以去协调的;电话确认很重要,省得浪费时间。

 

她之前的另外几点建议:

1、建议大家注册一个中国人才热线的帐号,看了那么多的人才网站,我就觉得CJOL的简历模板是最简洁明了且内容全面的;

2、写简历的时候不要一味的想强调自己的工作经验丰富而多多的列几家公司,其实这个是职场大忌,你的就职公司越多,就越代表你这个人不稳定,特别 是一家集团公司下面的几家子公司,或者一家公司不同阶段的不同职位,你不需要分类列出来,直接放在一个工作经验里汇总就好了。因为筛选简历的初级人员,每 天会看N多份简历,没有人会仔细去研究的,有时候一眼扫过去,看到你的跳槽频率太频繁就直接PASS掉了;

3、作简历工作经验那块,一定要把你的工作职责、工作内容写清楚,特别是有项目经验的,也要把项目大概描述下,并且要把你在项目中所处的位置,所 做的工作,以及这个项目的成就和你在项目中所起的作用写出来。如果你的工作涉及到跟不同部门人打交道,或者用到各种软硬件工具,也请罗列出来。因为按正常 流程来走,是先由初级人员筛选简历——HR——部门主管,而前两者都不是专业技术人员,不能对他们要求太高,他们只能通过部门主管给的要求按key point来找人,如果你的简历没有这些,将又失去一次机会;(建议工作经验这块言简意赅的1、2、3、4。。这样列出来,让别人清楚你是干什么的、会什 么同时也不要太罗嗦)

4、如果你还在职,建议你找工作的时候,在网站上的名字用化名,或者X先生、X小姐代替(因为很有可能你们公司的HR会搜到你的简历),如果公司或者项目内容涉及到竞业禁止协议或者不方便说的,也可以用“XXXXXXX”代替;

5、面试这块,我想说的是,平常心对待。不用紧张,你们是平等的,他们需要你这种人才,你是可以为他们创造价值的人,否则他们不会浪费时间来见 你。沉着冷静对待,原则是,他们问什么你答什么,多说多错,在你不了解这个公司的公司文化和氛围的前提下,表现出稳重总是错不了的。

latex中的moderncv简历模板升级了,与相关问题解决方案

新版本有中文moderncv模板的例子文件。

自己在实际使用中遇到以下几个问题:

1,最新版本是2月6号的,如果安装了完整的texlive2011,直接升级所有包即可

sudo tlmgr update --self

sudo tlmgr update --all
 

2,左边框过窄,导致中文日期自动折行。

解决方法:修改简历文件 xx.tex中的内容,
\setlength{\hintscolumnwidth}{3cm}           % 如果你希望改变日期栏的宽度
 

将hintscolumnwidth后面的数字,改成适合你的宽度。

 

3,\xelatex \latex等特殊字符无法显示,提示 underfull \hbox等内容。

解决方法:是因为设置的默认字体为DejaVu Sans Mono,不支持tex logo。

\usefont{OT1}{cmr}{b}{n}\XeLaTeX{}

b为粗体,可改为m为平常粗细。。。。

 

4,笔记本是texlive2011版本,家中台式机是2010.对2010使用 sudo tlmgr update --self --all升级后,编译无法通过,提示

xparse Error: Support package l3kernel too old..

经查找,是系统中有老版本包所致,需要删除 collection-latex3 、expl3、xpackages 这三个包(注意有倚赖关系)

删除后就可以了。。。。。

单链表,双向链表,栈ADT

/*
 * =====================================================================================
 *       Filename:  list.h
 *    Description:  链表ADT
 *
 *        Version:  1.0
 *        Created:  2011年03月19日 20时29分27秒
 *         Author:  hohosd44 (), sd44sd44@yeah.net
 *        Company:  
 * =====================================================================================
 */

typedef int elem_t;

#ifndef _LIST_H_
#define _LIST_H_
#include    <stdio.h>
#include    <stdlib.h>
#include    <stdbool.h>
 
typedef struct node {
    elem_t element;
    struct node *next;
} Node;
 
typedef Node *List;
typedef Node *Position;
 
List initialization(void);
bool isempty(List);
bool islast(Position, List);
Position find(elem_t, List);
Position findprev(elem_t, List);
void deletes(elem_t, List);
void insert(elem_t, List, Position);
void deletelist(List);
bool additem(elem_t, List);
 
#endif
 
List initialization(void)
{
    Position l;
    l = (List) malloc(sizeof(Node));
    if (l == NULL)
        fprintf(stderr, "%s\n", "Out of memory!");
    l->next = NULL;
    return l;
}
 
bool isempty(List l)
{
    return l->next == NULL;
}
 
bool islast(Position y, List l)
{
    return y->next == NULL;
}
 
Position find(elem_t x, List l)
{
    Position tmp;
    tmp = l->next;
    while (tmp != NULL && tmp->element != x)
        tmp = tmp->next;
 
    return tmp;
}
 
Position findprev(elem_t x, List l)
{
    Position tmp;
    tmp = l;
    while (tmp->next != NULL && tmp->next->element != x)
        tmp = tmp->next;
 
    return tmp;
}
 
void deletes(elem_t x, List l)
{
    Position prevpos, tmp;
    prevpos = findprev(x, l);
 
    if (!islast(prevpos, l)) {
        tmp = prevpos->next;
        prevpos->next = tmp->next;
        free(tmp);
    }
}
 
void insert(elem_t x, List l, Position y)
{
    Position tmppos;
    tmppos = (Position) malloc(sizeof(Node));
    if (tmppos == NULL)
        fprintf(stderr, "%s\n", "out of space!!!");
    tmppos->element = x;
    tmppos->next = y->next;
    y->next = tmppos;
}
 
void deletelist(List l)
{
    Position tmppos, tmp2pos;
 
    tmppos = l->next;
    while (tmppos != NULL) {
        tmp2pos = tmppos->next;
        free(tmppos);
        tmppos = tmp2pos;
    }
    free(l);
}
 
bool additem(elem_t x, List l)
{
    Position tmppos = l->next;
    Position newpos;
 
    newpos = (Position) malloc(sizeof(Node));
    if (newpos == NULL) {
        fprintf(stderr, "%s\n", "Out of space!!!");
        return false;
    }
 
    newpos->element = x;
    newpos->next = NULL;
 
    if (tmppos == NULL)
        l->next = newpos;
    else {
        while (tmppos->next != NULL) {
            tmppos = tmppos->next;
        }
        tmppos->next = newpos;
    }
    return true;
 
}

// 双链表ADT
typedef int elem_t;
#ifndef _LIST_H_
#define _LIST_H_
 
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
    elem_t element;
    struct node *prev;
    struct node *next;
} Node;
 
typedef Node *List;
typedef Node *Position;
 
List initialization(void);
bool isempty(List);
bool islast(Position, List);
Position find(int , List);
Position findprev(int, List);
void deletes(elem_t, List);
void insert(elem_t, List, Position);
void deletelist(List);
bool additem(elem_t, List);
 
#endif
 
List initialization(void)
{
    Position l;
    l = (List) malloc(sizeof(Node));
    if (l == NULL)
        fprintf(stderr, "%s\n", "Out of memory!");
    l->prev = NULL;
    l->next = NULL;
    return l;
}
 
bool isempty(List l)
{
    return l->next == NULL;
}
 
bool islast(Position y, List l)
{
    return y->next == NULL;
}
 
Position find(elem_t x, List l)
{
    Position tmp;
    tmp = l->next;
    while (tmp != NULL && tmp->element != x)
        tmp = tmp->next;
 
    return tmp;
}
 
Position findprev(elem_t x, List l)
{
    Position tmp;
    tmp = l;
    while (tmp->next != NULL && tmp->next->element != x)
        tmp = tmp->next;
 
    return tmp;
}
 
void deletes(elem_t x, List l)
{
    Position prevpos, tmp;
    prevpos = findprev(x, l);
 
    if (!islast(prevpos, l)) {
        tmp = prevpos->next;
        prevpos->next = tmp->next;
        tmp->next->prev = prevpos;
        free(tmp);
    }
}
 
void insert(elem_t x, List l, Position y)
{
    Position tmppos;
    tmppos = (Position) malloc(sizeof(Node));
    if (tmppos == NULL)
        fprintf(stderr, "%s\n", "out of space!!!");
    tmppos->element = x;
    tmppos->prev = y;
    tmppos->next = y->next;
    y->next->prev = tmppos;
    y->next = tmppos;
}
 
void deletelist(List l)
{
    Position tmppos, tmp2pos;
 
    tmppos = l->next;
    while (tmppos != NULL) {
        tmp2pos = tmppos->next;
        free(tmppos);
        tmppos = tmp2pos;
    }
    free(l);
}
 
bool additem(elem_t x, List l)
{
    Position tmppos = l->next;
    Position newpos;
 
    newpos = (Position) malloc(sizeof(Node));
    if (newpos == NULL) {
        fprintf(stderr, "%s\n", "Out of space!!!");
        return false;
    }
 
    newpos->element = x;
    newpos->next = NULL;
 
    if (tmppos == NULL) {
        l->next = newpos;
        newpos->prev = l;
    } else {
        while (tmppos->next != NULL) {
            tmppos = tmppos->next;
        }
        tmppos->next = newpos;
        newpos->prev = tmppos;
    }
    return true;
}

/*
 * =====================================================================================
 *       Filename:  stack.c
 *    Description:  栈链表ADT
 *
 *        Version:  1.0
 *        Created:  2011年03月20日 14时17分49秒
 *         Author:  hohosd44 (), sd44sd44@yeah.net
 *        Company:  
 * =====================================================================================
 */

#include	<stdbool.h>
#include	<stdlib.h>
#include	<stdio.h>

#ifndef  _stack_h__INC
#define  _stack_h__INC

typedef int elementtype;

struct node;
typedef struct node Node;
typedef Node *Stack;
typedef Node *Position;

bool isempty(Stack s);
Stack creatstack(void);
void freestack(Stack s);
void makeempty(Stack s);
void push(elementtype x, Stack s);
elementtype pop(Stack s);

#endif /* ----- #ifndef _stack_h__INC  ----- */

struct node {
	elementtype x;
	Position next;
};

bool isempty(Stack s)
{
	return s->next == NULL;
}

Stack creatstack(void)
{
	Stack s;
	s = (Position) malloc(sizeof(Node));

	if (s == NULL) {
		fprintf(stderr, "Out of space!!!");
		exit(1);
	}

	s->next = NULL;
	makeempty(s);
	return s;
}

void freestack(Stack s)
{
	makeempty(s);
	free(s);
}

void makeempty(Stack s)
{
	if (s == NULL) {
		fprintf(stderr, "Out of space!!!");
		exit(1);
	}
	while (!isempty(s))
		pop(s);
}

void push(elementtype x, Stack s)
{
	Position tmp;
	tmp = (Position) malloc(sizeof(Node));
	tmp->x = x;
	tmp->next = s->next;
	s->next = tmp;
}

elementtype pop(Stack s)
{
	Position tmp1;
	elementtype tmpdata;

	if (!isempty(s)) {
		tmpdata = s->next->x;
		tmp1 = s->next;
		s->next = s->next->next;
		free(tmp1);
		return tmpdata;
	} else {
		fprintf(stderr, "Empty Stack!!");
		exit(1);
	}
}

 

一些小题

#include <stdio.h>

int majority(int [], int, int);                 /* 确认候选值是不是主要元素 */
int primarykey(int arr[], int n)                /* 找出候选值 */
{
    int gal = 1;
    int key = arr[0];
    int i;
    for (i = 1; i < n; i++) {
        if (key == arr[i]) {
            gal++;
        }
        else
            gal--;
        if (gal == 0) {
            gal = 1;
            key = arr[i];
        }
    }
    return majority(arr, n, key);
}

int majority(int a[], int n, int key)
{
    int i;
    int sumkey = 0;
    for (i = 0; i < n; i++)
        if (a[i] == key)
            sumkey++;
    if (sumkey > n / 2)
        return key;
    else
        return 0;
}
int main(void)
{
    int a[] = {3, 3, 4, 3, 3, 4, 2, 4, 3};
    printf("%d\n", primarykey(a, 8));
    printf("%d\n", primarykey(a, 9));
    return 0;
}

 

啊啊

子序列问题

#include <stdlib.h>
#include <stdio.h>
/*
 * ===  FUNCTION  ======================================================================
 *         Name:  maxsubsum
 *  Description:  用分治法找出数组中的最长公共子序列
 * =====================================================================================
 */

int max3(int a, int b, int c)
{
    int max;
    if (a > b) {
        max = a;
    } else {
        max = b;
    }
    return max > c ? max : c;
}
int maxsubsum(const int a[], int left, int right)
{
    int maxleft, maxright;                      /* 左右最大子序列和 */
    int maxleftborder = 0, maxrightborder = 0;  /* 包含中间元素的左右两部份的和 */
    int leftbordersum = 0;
    int rightbordersum = 0;
    int mid, i;

    if (left == right) {
        if (a[left] > 0) {
            return left;
        } else {
            return 0;
        }
    }

    mid = (left + right) / 2;
    maxleft = maxsubsum(a, left, mid);
    maxright = maxsubsum(a, mid + 1 , right);

    for ( i = mid; i >= left; i--) {
        leftbordersum += a[i];
        if (leftbordersum > maxleftborder) {
            maxleftborder = leftbordersum;
        }
    }
    for ( i = mid + 1; i <= right; i++) {
        rightbordersum += a[i];
        if (rightbordersum > maxrightborder) {
            maxrightborder = rightbordersum;
        }
    }

    return max3(maxleft, maxright, maxleftborder + maxrightborder);
}

int main(void)
{
    int a[] = {4, -3, 5, -2, -1, 2, 6, -2};
    printf("%d\n", maxsubsum(a, 0, 7));
    return 0;
}
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  maxsubmulti
 *  Description:  采用分治法求解最大子序列乘积。
 *  因为负负得正,所以设置了 minleftborder,minrightborder
 * =====================================================================================
 */
#include <stdio.h>
double max4(double, double, double, double);
double maxone(const double [], int);            /* 如果maxsubmulti返回结果为1.0,则可能是都为小于1的小数且至多有一个负元素 */
double maxsubmulti(const double [], int, int);

double maxsub(const double a[], int n)
{
    double maxsubm = maxsubmulti(a, 0, n-1);
    if (maxsubm == 1.0)
        return (maxone(a, n));
    else
        return maxsubm;
}
double maxsubmulti(const double a[], int left, int right)
{
    double maxleft, maxright;
    double maxleftborder = 1, maxrightborder = 1;
    double minleftborder = 1, minrightborder = 1;
    double leftborder = 1, rightborder =1;
    int mid, i;

    if (left == right) {
        if (a[left] > 0)
            return a[left];
        else
            return 0;
    }

    mid = (left + right) / 2;
    maxleft = maxsubmulti(a, left, mid);
    maxright = maxsubmulti(a, mid + 1, right);

    for (i = mid; i >= left; i--) {
        leftborder *= a[i];
        if (leftborder > maxleftborder)
            maxleftborder = leftborder;
        if (leftborder < minleftborder)
            minleftborder = leftborder;
    }

    for (i = mid + 1; i <= right; i++) {
        rightborder *= a[i];
        if (rightborder > maxrightborder)
            maxrightborder = rightborder;
        if (rightborder < minrightborder)
            minrightborder = rightborder;
    }

    return max4(maxleft, maxright, maxleftborder * maxrightborder, minleftborder * minrightborder);
}

int main(void)
{
    double arr[6] = {-1.0, 1000.0, 0.001, 1.5, -80, 10};
    printf("%f\n",maxsub(arr, 6));
    double arb[6] = {-3, 0.02, 0.001, 0.3, 0.11, 0.210};
    printf("%f\n",maxsub(arb, 6));
    return 0;
}

double max4(double a, double b, double c, double d)
{
    double max = a;
    if (max < b)
        max = b;
    
    if (max < c)
        max = c;
    if (max < d)
        max = d;
    return max;
}

double maxone(const double a[], int n)
{
    double maxd = a[0];
    int i;
    for (i = 1; i < n; i++)
        if (a[i] > maxd)
            maxd = a[i];
    return maxd;
}
#include <stdio.h>
#include <stdlib.h>
 
// max1num返回数组中最大值
int max1num(int a[], int n)
{
    int i,max;
    max = a[0];
    for (i = 1; i < n; i++)
        if (a[i] > max)
            max = a[i];
    return max;
}
 
//求数组a的最大子序列和
int maxsub(int a[], int n)
{
    int i,maxsum,linesum;
 
    maxsum = max1num(a, n);
 
    if (maxsum <= 0)
        return maxsum;
 
    linesum = 0;
    for (i = 0; i < n; i++) {
        linesum += a[i];
 
        if (linesum < 0)
            linesum = 0;
        if (linesum > maxsum)
            maxsum = linesum;
    }
    return maxsum;
}
 
 
// min1num返回数组中最小值
int min1num(int a[], int n)
{
    int i, min;
    min = a[0];
    for (i = 1; i < n; i++)
        if (a[i] < min)
            min = a[i];
    return min;
}
 
//求数组a的最小子序列和
int minsub(int a[], int n)
{
    int i;
    int minsum, linesum;
 
    minsum = min1num(a,n);
 
    if (minsum >= 0)    //如果minsum >= 0的话,数组中没有负数,minsum即是最小子序列(只有一个元素minsum)
        return minsum;
 
    linesum = 0;
    for (i = 0; i < n; i++) {
        linesum += a[i];
        if (linesum > 0 && linesum > minsum)
            linesum = 0;
        if (linesum < minsum)
            minsum = linesum;
    }
    return minsum;
}
 
int main(void)
{
    int a[]={-324,23,23,111,23232,222,1111,555,-222,22,-88888};
    printf("%d\n",minsub(a,11));
 
    int b[]={324,23,-300,111,-121,2,-456,555,222,22,88888};
    printf("%d\n",minsub(b,11));
 
    int c[]={-324,23,23,111,23232,222,1111,555,-222,22,-88888};
    printf("%d\n",maxsub(c,11));
 
    int d[]={324,23,-300,111,-121,2,-456,555,222,22,88888};
    printf("%d\n",maxsub(d,11));
 
 
    return 0;
}

sasf

数据结构与算法分析数学和文本部份

数据结构与算法分析 第一章 TEX PDF笔记


一,归纳法证明:
1,证明基准情形(base case)。就是确定定理对于某个小的(通常是退化的)值的正确性。
2,归纳假设(inductive hypothesis)。假设定理对直到某个有限数k的所有情况都是成立的,然后使用这个假设证明定理对下一个值(k + 1)也是成立的。

二,递归:
1,基准情形(base case)。
2,不断推进(make progress)。对于需要递归求解的情形,递归调用必须总能够朝着产生基准情形的方向推进。
3,设计法则(design rule)。假设所有的递归调用都能运行。
4,合成效益法则(compound interest rule)。在求解一个问题的同一实例时,切勿在不同的递归调用中做重复性的工作。

 

三,

O(N)的一些排序算法:

基数排序(radix sort),也称卡式排序(card sort)

桶式排序(bucket sort),该排序运行时间为O(P(N+B)),P是排序趟数,N是元素个数,B是桶数。

我所阅读的书籍及笔记(持续更新)

总是誊抄笔记发在BLOG上的效率太低,不如直接在实体书或电子书上做标记。
基本上,以后除了算法书、难题、陷阱 以外不会再做BLOG上的笔记了。。。
下面是我所看书的书目,

电子版的笔记是用xournal所做,请用xournal配合同名PDF文件打开。

(如果有xournal笔记的,会提供原版高清电子书PDF格式下载以配合笔记使用

    如果侵权了,请告知 -_____-)

 

一,C/C++类

  1. K&R《C程序设计语言》

笔记在本站BLOG

   2.  《C PRIMER PLUS》      

笔记在本站BLOG(分散)

   3.  《Linux编程一站式学习》

笔记在本站BLOG(分散)

   4.  《C++ PRIMER》4th Edition

xournal笔记下载

 

二,操作系统

   1. 《深入理解计算机系统》第二版

xournal笔记下载

   2. 《UNIX环境高级编程》(APUE)

笔记在本站BLOG

 

三,GUI

   1. 《Linux窗口程序设计——Qt4精彩实例分析》

xournal笔记下载(只做到了第二章,继续中)

 

google style guide(C++, Python)最新中文版PDF。。

根据项目主页
http://code.google.com/p/zh-google-styleguide/
最新版本于今天凌晨转的。

 

Google C++ Style Guide:

因个人能力有限,对tex不熟悉,所以这个PDF还有不少瑕疵。主要是:
1,一些地方没有断行,造成无法显示。 不过缺失的行不多,不影响阅读理解。
2,小节索引号重复,应是原始文档的问题,水平有限,暂时无法解决 -__-。。。



HTML格式倒是挺完美-__-,有需要的话可以回帖。有人要的话我再放出来。。

下载地址如下:
http://ubuntuone.com/5Va1HOUaVLi7SYgCC2F4C6

 

Google Python Style Guide:

最后的表格被撑破了,开头的书目挤在了一起 -____-

http://ubuntuone.com/3ecpLiBbg0gB3OrPl4bhgT