/*
   2012年9月15日21:32:32
   了解队列
 */
#include <stdio.h>
#include <malloc.h>

typedef struct Queue
{
	int * pBase;
	int front;
	int rear;
}QUEUE;

void init(QUEUE *);               //初始化队列
bool full_queue(QUEUE *);         //判断是否满
bool en_queue(QUEUE *, int);      //入队
bool empty_queue(QUEUE *);        //判断是否为空
bool out_queue(QUEUE *, int *);   //出队
void traverse(QUEUE *);           //遍历队列

int main()
{
	QUEUE Q;
	int val;
	init(&Q);
	en_queue(&Q, 1);
	en_queue(&Q, 2);
	en_queue(&Q, 3);
	en_queue(&Q, 4);
	en_queue(&Q, 5);
	en_queue(&Q, 6);
	en_queue(&Q, 7);
	en_queue(&Q, 8);
	traverse(&Q);
	if (out_queue(&Q, &val))
		printf("出队成功,删除的元素是:%d\n", val);
	traverse(&Q);
	return 0;
}

void init(QUEUE * pQ)
{
	pQ->pBase = (int *)malloc(sizeof(int) * 6);
	pQ->front = pQ->rear = 0;
}
bool full_queue(QUEUE * pQ)
{
	if (((pQ->rear+1) % 6) == pQ->front)
		return true;
	else
		return false;
}
bool en_queue(QUEUE * pQ, int val)
{
	if (full_queue(pQ))
		return false;
	else
	{
		pQ->pBase[pQ->rear] = val;
		pQ->rear = (pQ->rear+1) % 6;
		return true;
	}
}
bool empty_queue(QUEUE * pQ)
{
	if (pQ->front == pQ->rear)
		return true;
	else
		return false;
}
bool out_queue(QUEUE * pQ, int * val)
{
	if (empty_queue(pQ))
		return false;
	else
	{
		*val = pQ->pBase[pQ->front];
		pQ->front = (pQ->front+1) % 6;
		return true;
	}
}
void traverse(QUEUE * pQ)
{
	if (empty_queue(pQ))
		printf("队列为空!\n");
	else
	{
		int i;
		for (i = pQ->front; i != pQ->rear; i = (i+1) % 6)
			printf("%d ", pQ->pBase[i]);
		printf("\n");
	}
}

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7983127

/*
   2012年9月11日11:45:30
   了解栈的构造方法
 */
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct Node
{
	int data;
	struct Node * pNext;
}NODE, * PNODE;
typedef struct Stack
{
	PNODE pTop;
	PNODE pBottom;
}STACK, * PSTACK;

void init(PSTACK);             //初始化栈
void push(PSTACK, int);        //压栈
bool empty(PSTACK);            //判断栈是否为空
void traverse(PSTACK);         //遍历栈
void pop(PSTACK);              //出栈
void clear(PSTACK);            //清空栈

int main()
{
	STACK S;
	int val;
	/*测试初始化栈及压栈函数*/
	init(&S);
	push(&S, 1);
	push(&S, 2);
	push(&S, 3);
	push(&S, 4);
	push(&S, 5);
	traverse(&S);
	/*测试出栈*/
	pop(&S);
	traverse(&S);
	/*测试清空栈*/
	clear(&S);
	traverse(&S);

	return 0;
}

/*初始化栈——使pTop和pBottom指向同一节点*/
void init(PSTACK pS)
{
	pS->pBottom = (PNODE)malloc(sizeof(NODE));
	if (pS->pBottom == NULL)
	{
		printf("动态内存分配失败!\n");
		exit(-1);
	}
	pS->pBottom->pNext = NULL;
	pS->pTop = pS->pBottom;
	return;
}
/*压栈——创建一个新节点,连到定节点上面,把新节点标记为顶节点*/
void push(PSTACK pS, int val)
{
	PNODE pNew;
	pNew = (PNODE)malloc(sizeof(NODE));
	if (NULL == pNew)
	{
		printf("动态内存分配失败!\n");
		exit(-1);
	}
	pNew->data = val;
	pNew->pNext = pS->pTop;
	pS->pTop = pNew;
	return;
}
/*判断栈是否为空*/
bool empty(PSTACK pS)
{
	if (pS->pTop == pS->pBottom)
		return true;
	else
		return false;
}
/*遍历栈——定义临时变量temp,从pTop开始一直往下指*/
void traverse(PSTACK pS)
{
	if (empty(pS))
		printf("栈为空 !\n");
	else
	{
		PNODE temp;
		temp = pS->pTop;
		printf("遍历栈:");
		while (temp != pS->pBottom)
		{
			printf("%d ", temp->data);
			temp = temp->pNext;
		}
		printf("\n");
	}
	return;
}
/*出栈—— pTop下移,且把原pTop占用的空间释放*/
void pop(PSTACK pS)
{
	if (empty(pS))
		printf("栈为空!\n");
	else
	{
		PNODE temp = pS->pTop;
		pS->pTop = pS->pTop->pNext;
		printf("被删除的栈的数据域为:%d\n", temp->data);
		free(temp);
	}
	return;
}
/*清空栈——类似出栈*/
void clear(PSTACK pS)
{
	if (empty(pS))
		printf("栈为空!\n");
	else
		while (pS->pTop != pS->pBottom)
		{
			PNODE temp = pS->pTop;
			pS->pTop = pS->pTop->pNext;
			free(temp);
		}
	return;
}

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7966528

/*
   2012年9月5日10:36:48
   了解联系链表数据结构
 */
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct Node
{
	int data;                //数据域
	struct Node * pNext;     //指针域
}NODE, * PNODE;

PNODE create_list(void);   //创建链表
void traverse_list(PNODE); //遍历链表
bool is_empty(PNODE);      //判断链表是否为空
int length_list(PNODE);    //求链表长度
void sort_list(PNODE);     //链表排序
bool insert_list(PNODE pHead, int pos, int val);     //插入节点
bool delete_list(PNODE pHead, int pos, int * val);   //删除节点

int main()
{
	PNODE pHead = NULL;
	int val;

	pHead = create_list();
	traverse_list(pHead);
	insert_list(pHead, 3, 89);
	traverse_list(pHead);
	delete_list(pHead, 4, &val);
	traverse_list(pHead);



	//if (is_empty(pHead))
	// printf("链表为空\n");
	//else
	// printf("链表不为空\n");
	//printf("链表的长度为:%d\n", length_list(pHead));
	//sort_list(pHead);
	//traverse_list(pHead);

	return 0;
}

/*创建链表*/
PNODE create_list(void)
{
	int len;         //链表长度
	int val;         //临时存放数据域
	int i;

	/*创建头结点*/
	PNODE pHead;
	pHead = (PNODE)malloc(sizeof(NODE));
	if (NULL == pHead)
	{
		printf("动态分配内存失败\n");
		exit(-1);
	}

	/*创建尾节点*/
	PNODE pTail = pHead;

	/*追加节点*/
	printf("请输入您要创建节点的个数: len = ");
	scanf("%d", &len);
	for (i = 0; i < len; ++i)
	{
		printf("请输入第%d个数的数值:", i + 1);
		scanf("%d", &val);
		PNODE pNew = (PNODE)malloc(sizeof(NODE));
		if(NULL == pNew)
		{
			printf("动态分配内存失败\n");
			exit(-1);
		}
		pNew->data = val;
		pNew->pNext = NULL;
		pTail->pNext = pNew;
		pTail = pNew;
	}

	return pHead;
}
/*遍历链表*/
void traverse_list(PNODE pHead)
{

	PNODE p = pHead->pNext;

	printf("遍历链表结果为:\n");
	while(NULL != p)
	{
		printf("%d ", p->data);
		p = p->pNext;
	}

	printf("\n\n");
	return;
}
/*判断链表是否为空*/
bool is_empty(PNODE pHead)
{
	if (NULL == pHead->pNext)
		return true;
	else
		return false;
}
/*求链表长度*/
int length_list(PNODE pHead)
{
	PNODE p = pHead;
	int cnt = 0;

	while (NULL != p->pNext)
	{
		cnt++;
		p = p->pNext;
	}

	return cnt;
}

/*链表大小排序*/
void sort_list(PNODE pHead)
{
	int i, j, t;
	int len = length_list(pHead);
	PNODE p, q;
	/*理解泛型的概念,用p=p->pNext代替了p++*/
	for (i = 0, p = pHead->pNext; i < len-1; i++, p = p->pNext)   
	{
		for (j = i+1,  q = p->pNext; j < len; j++, q = q->pNext)
		{
			if (p->data > q->data)
			{
				t = p->data;
				p->data = q->data;
				q->data = t;
			}
		}
	}
}

/*插入节点*/
bool insert_list(PNODE pHead, int pos, int val)
{
	PNODE p = pHead;
	int i = 0;
	/*把p的指针移到pos的前一个节点*/
	while (i<pos-1 && NULL!=p)
	{
		p = p->pNext;
		i++;
	}
	/*增强程序健壮性,避免【pos为-1】 或者 【pos大于length】的情况,程序崩溃*/
	if (i>pos-1 || NULL==p)
		return false;
	PNODE pNew = (PNODE)malloc(sizeof(NODE));
	if (NULL == pNew)
	{
		printf("动态内存分配失败\n");
		exit(-1);
	}
	pNew->data = val;
	pNew->pNext = p->pNext;
	p->pNext = pNew;
	return true;
}
/*删除节点*/
bool delete_list(PNODE pHead, int pos, int * val)
{
	int i = 0;
	PNODE p = pHead;
	while (NULL!=p->pNext && i<pos-1)
	{
		p = p->pNext;
		i++;
	}
	if (NULL==p->pNext || i>pos-1)
		return false;
	PNODE q = p->pNext;
	p->pNext = p->pNext->pNext;
	free(q);
	q = NULL;
	return true;
}

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7945231

posted in 读书笔记 

**********************************************************************

自律,不能随大环境一起堕落

    在这样的学习环境下,除非主动自学,要么多多上网搜集信息、泡图书馆,

能有什么选择呢?但话说回来我没完全做到,最终问题还出在自己:不够坚定,

有时会随着大环境一起堕落,浪费了很多时间。

**********************************************************************

英语很重要,包括口语

    如果想做技术支持,或者项目经理一类的管理人员

**********************************************************************

实践很重要,不要过分关注文化课成绩

    "计算机专业到底要学什么",这个问题经过大一一年的探索渐渐地清晰起

来。我从网络及图书馆里学到了很多的知识,看到了一个新的天地,我也深深地

为这个世界着迷。此时,我发现学校里的课程教育和我对计算机科学与技术专业

的理解有很大的差别。课堂上多数老师都在重复着幻灯片里的文字,一部分希望

上进的同学耐心地接受着这种方式的"科普"教育,考试之前花费大把的时间对老

师划的"范围"里的知识进行机械地记忆,之后考出一个令人瞠目结舌的高分(信

不信由你,我们班有平均分95以上的);另一部分同学则将自己的大好时光寄托

于网络游戏,或是花天酒地终日穿梭于声色场所…… 

    这段时间我觉得自己是那么的孤独,我试图融入周围学习比较认真的同学当

中,我希望他们和我一起去探索那个未知的天地。但是,我发现没有人可以与我

一起去探讨计算机问题,也没有人和我一起去做程序。我不断问自己,为什么这

种教育方式得到了这么多老师以及身边这么多同学的认可,难道是我错了?杂志

上、访谈中那些名人的学习经历难道仅仅是天才的专利?是不是要成为高手一开

始都要去背概念、去考高分,去经历这个"乏味的"阶段,至于动手编程要等把这

些都做好了以后再练…… 

    直到大二上学期结束,我终于确定,学校的教育并不合理,多数老师讲的是

"垃圾",照本宣科的很有可能是他们自己都没学明白的东西。而计算机科学与技

术也不是仅仅学学概念就可以成为高手的,况且按照学校的这种方式学下去,似

乎概念都学不好,充其量不过是个记忆高手罢了。

    这里插一句,事实也证明了我的这种说法。我的考试成绩虽然不是非常突出,

却也还算过得去,每个学期基本上都能拿到院里的二等或三等奖学金。但是在我

后来求职的时候,这些靠背概念就能拿到的奖学金证书并没有起到什么作用。以

我的面试经历来看,我简历当中最打动面试官的地方恰恰是我利用课余时间及逃

课所写的程序,还有利用背考试题的时间所读的课外书。这里请大一大二的学弟

学妹们好好考虑一下:时间有限的情况下,在高高的GPA和实际编程能力的训

练之间我们应该如何权衡。

**********************************************************************

做事情的顺序

A.重要且紧急    B.重要不紧急     C.紧急不重要    D.不重要不紧急

**********************************************************************

善于利用互联网

诸如 CSDN等

**********************************************************************

要有自己的理想,并坚持自己的理想

Your time is limited, so don't waste it living in someone else's life. Don't be 

trapped by dogma which is living with the results of other people's thinking. Don't let 

the noise of others' opinions drown out your own inner voice. And the most important, 

have the courage to your heart and intuition. They somehow already know what you 

truly want to become. Everything else is secondary. 

 

 

**************************************************************************

***********************************************************************

动手时间很重要,读书要读透

我的方法还是一样,敲例子。记得《The TeXbook》

上有一个程序,Knuth让大家自己照着敲入计算机,然后还很幽默地说,实验证

明,只有很少的人会按照他说的敲入这个程序,而这部分人,却是学TeX学得

最好的人。看到这里我会心一笑,觉得自己的方法原来也不算笨。从此,一字不

漏敲入一本书的程序成了我推荐别人学习语言的最好办法。后来大四时我又敲了

《A Byte of Python》,前段时间又敲完了《The Awk Book》,都是不到一个月立即

从初学者成长为细节很熟、代码顺手拈来的熟练使用者。顺着这个方法,大二我

把《组合数学引论》和上海交通大学出版社出版的一本《离散数学》上的题目都

做一题不漏地做完了。当时选这两本书也没有特别的目的,就觉得这东西应该是

计算机的数学基础。这些积累,在大四全部都显现了出来。

***********************************************************************

学着看好的英文书——学好英语

我个人认为,《Thinking in Java》和《The TeXbook》都算得上是理论和实践

结合的精品书,是经典的英文原版书。我一上来就读了这两本书,阅读品位就上

升了不少,而且变得"崇洋媚外"了,任何时候都以英文原版书为第一选项了。也

正因为此,虽然我自学的过程中没有高人指点,但自学最重要的一个环节--选书

--的盲目性就大大减少了。我记得当时我看得最多的书是华章引进的书,黑封面

的,我们图书馆里有将近半书架。这些书如果一一细读,穷尽四年都看不完。但

华章的书也并非本本经典,我开始注意选择,细读开头十几页后,基本能决定一

本书该不该看。所以即使当时没人指点,全靠自学,读的书还算过得去。那时候

南大计算机系的教材,有的我看,有的我觉得不适合自己,就找替代品了。我觉

得在选书这个事情上,因为有前两本书的标杆,我少走了不少弯路。从这两本书

开始,我疯狂的读书生涯就开始了。那时候南大浦口校区的硬件条件并不好,唯

一有空调且可以上自习的地方恰好是图书馆。

 

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7909804

posted in 读书笔记 

卢梭的《西方哲学史》,朱光潜的《西方美学史》

底蕴的厚度决定了你未来生命的高度 

  现在大学生的人文学识相对来说比较少,现在的大学生读书太急功进利了,他们只读《穷爸爸富爸爸》,都想快速致富,有一句话叫做“底蕴的厚度决定了你未来生命的高度”而这个厚度绝对不是读《穷爸爸富爸爸》能读厚的,你要读卢梭的《西方哲学史》,朱光潜的《西方美学史》这样的书,甚至背背唐书,跟你未来做的事情可能一点关系都没有,但她奠定了你这个人的基础,奠定了你这个人的厚度,奠定了你这个人后来发挥的余地有多大。

  你不能忍受的事情,但是你不得不忍受,而不忍受就不能成功我们未来生活最重要的一个能力,叫做忍辱负重的能力,你不能忍受的事情,但是你不得不忍受而不忍受就不可能成功,当我们自己的生命要想为一个伟大的目标而奋斗大的时候,你必须排除你生命中一切琐碎的干扰,你设想一个场景,假如你在路上被一辆自行车轧了一下,你奋起反击,结果一不小心你的鼻梁骨被打断了,最后在医院躺了一个月,你的生命就被医院消灭了一个月,但是如果倒过来说,这是小事,我还有更重要的事情呢,你就告诉他对不起把路给你挡了,对方再蛮横无理,他还能揍你吗?还能打你吗?他马上就会说说我不好,对不起,于是生命中的一件小事就化解掉了,韩信就是因为钻人家的裤裆,最后才帮刘邦打下了天下,你要知道,这个世界上,你只有忍辱负重才能发展,你得为自己争取时间,争取未来,而争取时间空间未来最重要的一个前提就是你有理想,但是你敢于忍受生活中出现的一切对你的不公平或者是一切你认为受不了的事情。

     任何一件伟大的事情,你把它分配到日常的每一天去,都是很小的事情,甚至是很无聊的事情,日复一日你背着书包去上课,日复一日你处理新东方内部员工们琐碎的事情,这是需要强大的现实主义精神你才能做成的。伟大与平凡人之间的不同之处是一个平凡人每天过着琐碎的生活,他把琐碎堆砌出来,还是一堆琐碎的生命。所谓伟大的人,是把一堆琐碎的事情通过一个伟大的目标每天积累起来,最后变成一个伟大的事业。 

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7909800

posted in OJ题解 

Identity Card

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1969 Accepted Submission(s): 627
 
Problem Description
Do you own an ID card?You must have a identity card number in your family's Household Register. From the ID card you can get specific personal information of everyone. The number has 18 bits,the first 17 bits contain special specially meanings:the first 6 bits represent the region you come from,then comes the next 8 bits which stand for your birthday.What do other 4 bits represent?You can Baidu or Google it.
Here is the codes which represent the region you are in.

However,in your card,maybe only 33 appears,0000 is replaced by other numbers.
Here is Samuel's ID number 331004198910120036 can you tell where he is from?The first 2 numbers tell that he is from Zhengjiang Province,number 19891012 is his birthday date (yy/mm/dd).
Input
Input will contain 2 parts:
A number n in the first line,n here means there is n test cases. For each of the test cases,there is a string of the ID card number.
Output
Based on the table output where he is from and when is his birthday. The format you can refer to the Sample Output.
Sample Input
1
330000198910120036
Sample Output
He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.
Author
Samuel
Source
灰色橙子 专场
Recommend
zty
 
 
代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
 int n;
 double temp;
 string place;
 char st[18];

 cin >> n;
 getchar();                                 //getchar()来吃掉enter,要不然enter会被gets函数读入
 while (n--)
 {
  gets(st);
  cout << "He/She is from ";
  if (st[0] == '3' && st[1] == '3')     //3要用''括起来,因为st是字符数组
   cout << "Zhejiang";
  else if (st[0] == '1' && st[1] == '1')
   cout << "Beijing";
  else if (st[0] == '7' && st[1] == '1')
   cout << "Taiwan";
  else if (st[0] == '8' && st[1] == '1')
   cout << "Hong Kong";
  else if (st[0] == '8' && st[1] == '2')
   cout << "Macao";
  else if (st[0] == '5' && st[1] == '4')
   cout << "Tibet";
  else if (st[0] == '2' && st[1] == '1')
   cout << "Liaoning";
  else if (st[0] == '3' && st[1] == '1')
   cout << "Shanghai";

  cout << ",and his/her birthday is on "
    << st[10] << st[11] << ','
    << st[12] << st[13] << ','
    << st[6] << st[7] << st[8] << st[9];
  cout << " based on the table."
    << endl;
 }

 return 0;
}

 

总结:

一开始想用 int temp来存放ID Card Number,运用'/'和'%'来提取号码,但是没那么大的整形变量,
而用double的话,不能用'%',而且会发生溢出错误。
所以运用了字符数组。

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7648866

posted in OJ题解 

IBM Minus One

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1467 Accepted Submission(s): 627
 
Problem Description
You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)

After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.
Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.
Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.

Print a blank line after each test case.
Sample Input
2
HAL
SWERC
Sample Output
String #1
IBM

String #2
TXFSD



 
Source
Southwestern Europe 1997, Practice
 
我的代码:
 

#include <iostream>
using namespace std;

int main()
{
 int n, i, k, len;
 char st[100];

 cin >> n;
 getchar();
 for (i = 1; i <= n; i++)
 {
  cout << "String #" << i << endl;

  gets(st);
  len = strlen(st);
  for (k = 0; k < len; k++)
  {
   st[k] ++;
   if (st[k] == 'Z' + 1)
    st[k] = 'A';
  }
  puts(st);
  cout << endl;
 }

 return 0;
}

 
总结:
一开始范2了,把第二个循环变量想成i了,导致半天没AC。。反省反省。。。
 

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/7647879

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();