精选面试算法的实现之一

注明:本文的题目来自http://blog.csdn.net/v_july_v/article/details/5934051 感谢作者的搜集。

本人在此用C语言进行编程实现,有些题目的实现跟原贴有些不同,提供另一种思路。

水平有限,读者仅供参考。


1.把二元查找树转变成排序的双向链表
 题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
   
  10
  / /
 6 14
 / / / /
4 8 12 16
   
 转换成双向链表
4=6=8=10=12=14=16。

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4.   
  5. typedef struct _node{  
  6.     int val;  
  7.     struct _node *left;  
  8.     struct _node *right;  
  9. }node;  
  10.   
  11. typedef struct _Stack{  
  12.     node *p;  
  13.     struct _Stack *next;  
  14. }Stack;  
  15.   
  16. static Stack *top=NULL;  
  17. void push(node *n){  
  18.     Stack *tmp=(Stack *)malloc(sizeof(Stack));  
  19.     tmp->p=n;  
  20.     tmp->next=top;  
  21.     top=tmp;  
  22. }  
  23. node *pop(){  
  24.     if(top==NULL) {  
  25.         printf("the stack is empty ");  
  26.         return NULL;  
  27.     }  
  28.     Stack *tmp=top;  
  29.     top=top->next;  
  30.     node *mynode=tmp->p;  
  31.     free(tmp);  
  32.     return mynode;  
  33. }  
  34. /* add node to a exist tree */  
  35. int addnode(int val,node *root){  
  36.     assert(root!=NULL);  
  37.     if(root->val==val){  
  38.         printf("exist the val\n");  
  39.         return -1;  
  40.     }  
  41.     else if (root->val>val){  
  42.         if(root->left==NULL){  
  43.             node *tmp=(node *)malloc(sizeof(node));  
  44.             tmp->val=val;  
  45.             tmp->left=tmp->right=NULL;  
  46.             root->left=tmp;  
  47.             return 1;  
  48.         }     
  49.         else{  
  50.             addnode(val,root->left);  
  51.         }  
  52.     }  
  53.     else{  
  54.         if(root->right==NULL){  
  55.             node *tmp=(node *)malloc(sizeof(node));  
  56.             tmp->val=val;  
  57.             tmp->left=tmp->right=NULL;  
  58.             root->right=tmp;  
  59.             return 1;  
  60.         }  
  61.         else{  
  62.             addnode(val,root->right);  
  63.         }  
  64.     }  
  65. }  
  66. /* for test ,traverse the link by inorder */  
  67. void midwalk(node *root){  
  68.     if(root!=NULL){  
  69.         midwalk(root->left);  
  70.         printf("%d\t",root->val);  
  71.         midwalk(root->right);  
  72.     }  
  73. }  
  74. /* change the tree to link list */  
  75. node *changeToLink(node *root){  
  76.     node *mynode=root;  
  77.     node *head=NULL;  
  78.     node *tail=NULL;  
  79.     while((mynode!=NULL)||(top!=NULL)){  
  80.         while(mynode!=NULL){  
  81.             push(mynode);  
  82.             mynode=mynode->left;  
  83.         }  
  84.         if(top!=NULL){  
  85.             node *tmp=pop();  
  86.             if(head==NULL){  
  87.                 head=tmp;  
  88.                 tail=tmp;  
  89.             }  
  90.             else{  
  91.                 tail->right=tmp;  
  92.                 tmp->left=tail;  
  93.                 tail=tmp;  
  94.   
  95.             }  
  96.             //printf("%d\t",tmp->val);   
  97.             mynode=tmp->right;  
  98.         }  
  99.     }  
  100.     return head;  
  101. }  
  102. /* print the link list */  
  103. void printLink(node *p){  
  104.     while(p!=NULL){  
  105.         printf("%d\t",p->val);  
  106.         p=p->right;  
  107.     }  
  108. }  
  109. int main(){  
  110.     node *tree=(node *)malloc(sizeof(node));  
  111.     tree->val=10;  
  112.     addnode(6,tree);  
  113.     addnode(14,tree);  
  114.     addnode(4,tree);  
  115.     addnode(8,tree);  
  116.     addnode(12,tree);  
  117.     addnode(16,tree);  
  118.     node *test=changeToLink(tree);  
  119.     printLink(test);  
  120.     printf("\n");  
  121.     return 0;  
  122. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef struct _node{
	int val;
	struct _node *left;
	struct _node *right;
}node;

typedef struct _Stack{
	node *p;
	struct _Stack *next;
}Stack;

static Stack *top=NULL;
void push(node *n){
	Stack *tmp=(Stack *)malloc(sizeof(Stack));
	tmp->p=n;
	tmp->next=top;
	top=tmp;
}
node *pop(){
	if(top==NULL) {
		printf("the stack is empty ");
		return NULL;
	}
	Stack *tmp=top;
	top=top->next;
	node *mynode=tmp->p;
	free(tmp);
	return mynode;
}
/* add node to a exist tree */
int addnode(int val,node *root){
	assert(root!=NULL);
	if(root->val==val){
		printf("exist the val\n");
		return -1;
	}
	else if (root->val>val){
		if(root->left==NULL){
			node *tmp=(node *)malloc(sizeof(node));
			tmp->val=val;
			tmp->left=tmp->right=NULL;
			root->left=tmp;
			return 1;
		}	
		else{
			addnode(val,root->left);
		}
	}
	else{
		if(root->right==NULL){
			node *tmp=(node *)malloc(sizeof(node));
			tmp->val=val;
			tmp->left=tmp->right=NULL;
			root->right=tmp;
			return 1;
		}
		else{
			addnode(val,root->right);
		}
	}
}
/* for test ,traverse the link by inorder */
void midwalk(node *root){
	if(root!=NULL){
		midwalk(root->left);
		printf("%d\t",root->val);
		midwalk(root->right);
	}
}
/* change the tree to link list */
node *changeToLink(node *root){
	node *mynode=root;
	node *head=NULL;
	node *tail=NULL;
	while((mynode!=NULL)||(top!=NULL)){
		while(mynode!=NULL){
			push(mynode);
			mynode=mynode->left;
		}
		if(top!=NULL){
			node *tmp=pop();
			if(head==NULL){
				head=tmp;
				tail=tmp;
			}
			else{
				tail->right=tmp;
				tmp->left=tail;
				tail=tmp;

			}
			//printf("%d\t",tmp->val);
			mynode=tmp->right;
		}
	}
	return head;
}
/* print the link list */
void printLink(node *p){
	while(p!=NULL){
		printf("%d\t",p->val);
		p=p->right;
	}
}
int main(){
	node *tree=(node *)malloc(sizeof(node));
	tree->val=10;
	addnode(6,tree);
	addnode(14,tree);
	addnode(4,tree);
	addnode(8,tree);
	addnode(12,tree);
	addnode(16,tree);
	node *test=changeToLink(tree);
	printLink(test);
	printf("\n");
	return 0;
}


2.设计包含min函数的栈。
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。
要求函数min、push以及pop的时间复杂度都是O(1)。

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4.   
  5. typedef struct _Ele{  
  6.     int val;  
  7.     struct _Ele *next;  
  8.     struct _Ele *currentMin;  
  9. }Ele;  
  10.   
  11. struct Stack{  
  12.     Ele *top;  
  13.       
  14. };  
  15.   
  16. struct Stack *initStack(){  
  17.     struct Stack *s;  
  18.     s=(struct Stack *)malloc(sizeof(struct Stack));  
  19.     s->top=NULL;  
  20.   
  21.     return s;  
  22. }  
  23. int empty(struct Stack *s){  
  24.     if(s->top==NULL){  
  25.         return 1;  
  26.     }  
  27.     else{  
  28.         return 0;  
  29.     }  
  30. }  
  31. void push(struct Stack *s,int val){  
  32.     /* init the new node */  
  33.     Ele *e=(Ele *)malloc(sizeof(Ele));  
  34.     e->val=val;  
  35.     e->next=NULL;  
  36.     e->currentMin=e;  
  37.       
  38.     if(s->top!=NULL){      
  39.         int min=s->top->currentMin->val;  
  40.         if(val<min){  
  41.             e->currentMin=e;  
  42.         }  
  43.         else{  
  44.             e->currentMin=s->top->currentMin;  
  45.         }  
  46.     }  
  47.   
  48.     e->next=s->top;  
  49.     s->top=e;  
  50. }  
  51. int min(struct Stack *s){  
  52.     if(s==NULL){  
  53.         printf("the stack is empty\n");  
  54.         exit(-1);  
  55.     }  
  56.     return s->top->currentMin->val;  
  57. }  
  58. int pop(struct Stack *s){  
  59.     if(s->top==NULL){  
  60.         printf("the stack is empty\n");  
  61.         exit(-1);  
  62.     }  
  63.       
  64.     Ele *newtop=s->top->next;  
  65.     int retval=s->top->val;  
  66.     free(s->top);  
  67.     s->top=newtop;  
  68.     return retval;  
  69.   
  70. }  
  71. int main(){  
  72.   
  73.     struct Stack *mysta=NULL;  
  74.     mysta=initStack();  
  75.     push(mysta,3);  
  76.     push(mysta,2);  
  77.     push(mysta,4);  
  78.     push(mysta,5);  
  79.     push(mysta,1);  
  80.     push(mysta,9);  
  81.     int p=pop(mysta);  
  82.     printf("pop out %d\n",p);  
  83.     int d=min(mysta);  
  84.     printf("the min is %d\n",d);  
  85.     int p2=pop(mysta);  
  86.     printf("pop out %d\n",p2);  
  87.     int d2=min(mysta);  
  88.     printf("the new min is %d\n",d2);  
  89.     return  0;  
  90. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef struct _Ele{
	int val;
	struct _Ele *next;
	struct _Ele *currentMin;
}Ele;

struct Stack{
	Ele *top;
	
};

struct Stack *initStack(){
	struct Stack *s;
	s=(struct Stack *)malloc(sizeof(struct Stack));
	s->top=NULL;

	return s;
}
int empty(struct Stack *s){
	if(s->top==NULL){
		return 1;
	}
	else{
		return 0;
	}
}
void push(struct Stack *s,int val){
	/* init the new node */
	Ele *e=(Ele *)malloc(sizeof(Ele));
	e->val=val;
	e->next=NULL;
	e->currentMin=e;
	
	if(s->top!=NULL){	
		int min=s->top->currentMin->val;
		if(val<min){
			e->currentMin=e;
		}
		else{
			e->currentMin=s->top->currentMin;
		}
	}

	e->next=s->top;
	s->top=e;
}
int min(struct Stack *s){
	if(s==NULL){
		printf("the stack is empty\n");
		exit(-1);
	}
	return s->top->currentMin->val;
}
int pop(struct Stack *s){
	if(s->top==NULL){
		printf("the stack is empty\n");
		exit(-1);
	}
	
	Ele *newtop=s->top->next;
	int retval=s->top->val;
	free(s->top);
	s->top=newtop;
	return retval;

}
int main(){

	struct Stack *mysta=NULL;
	mysta=initStack();
	push(mysta,3);
	push(mysta,2);
	push(mysta,4);
	push(mysta,5);
	push(mysta,1);
	push(mysta,9);
	int p=pop(mysta);
	printf("pop out %d\n",p);
	int d=min(mysta);
	printf("the min is %d\n",d);
	int p2=pop(mysta);
	printf("pop out %d\n",p2);
	int d2=min(mysta);
	printf("the new min is %d\n",d2);
	return  0;
}


3.求子数组的最大和
题目:
输入一个整形数组,数组里有正数也有负数。
数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
求所有子数组的和的最大值。要求时间复杂度为O(n)。

例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,
因此输出为该子数组的和18。

(最大子段和问题)

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4.   
  5. int maxSeq(int *array,int len){  
  6.     int max=0;  
  7.     int b=0;  
  8.     int i;  
  9.     for(i=0;i<len;i++){  
  10.         if(b>0){  
  11.             b+=array[i];  
  12.         }  
  13.         else{  
  14.             b=array[i];  
  15.         }  
  16.   
  17.         if(b>max)  
  18.             max=b;  
  19.     }  
  20.     return max;  
  21. }  
  22. int main(){  
  23.     int values[]={1,-2,3,10,-4,7,2,-5};  
  24.     int res=maxSeq(values,8);  
  25.     printf("max is %d\n",res);  
  26.     return 0;  
  27. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

int maxSeq(int *array,int len){
	int max=0;
	int b=0;
	int i;
	for(i=0;i<len;i++){
		if(b>0){
			b+=array[i];
		}
		else{
			b=array[i];
		}

		if(b>max)
			max=b;
	}
	return max;
}
int main(){
	int values[]={1,-2,3,10,-4,7,2,-5};
	int res=maxSeq(values,8);
	printf("max is %d\n",res);
	return 0;
}

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
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node

};

思路:这里给出的节点定义已经定义好了,试想,如果我们在每个节点处增加一个数据项,这样的话,每次遍历这棵树,如果遇到叶子结点,就顺着父亲结点一直搜索到根结点,如果满足指定的和,则打印之。

这是最为直观的解题思路了,是吧?你可能会说题目给出的结点就这样了,但我也可以说我再根据输入的树自定义一棵副本树呢,呵呵。这是题目问题,在没有限制的情况下,添加一个指向父亲结点的指针就搞定了。

具体代码就省略啦

5.查找最小的k个元素
题目:输入n个整数,输出其中最小的k个。
例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4.   
  5.   
  6. void findMink(int *a,int len,int k){  
  7.     assert(k<len);  
  8.     int i;  
  9.     for(i=0;i<k;i++){  
  10.         int j;  
  11.         for(j=len-1;j>i;j--){  
  12.             if(a[j]<a[j-1]){  
  13.                 int tmp=a[j];  
  14.                 a[j]=a[j-1];  
  15.                 a[j-1]=tmp;  
  16.             }             
  17.         }  
  18.     }  
  19.     for(i=0;i<k;i++){  
  20.         printf("%d\n",a[i]);  
  21.     }  
  22.       
  23. }  
  24. int main(){  
  25.     int val[]={8,7,6,5,4,3,2,1};  
  26.     findMink(val,8,4);  
  27.     return 0;  
  28. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


void findMink(int *a,int len,int k){
	assert(k<len);
	int i;
	for(i=0;i<k;i++){
		int j;
		for(j=len-1;j>i;j--){
			if(a[j]<a[j-1]){
				int tmp=a[j];
				a[j]=a[j-1];
				a[j-1]=tmp;
			}			
		}
	}
	for(i=0;i<k;i++){
		printf("%d\n",a[i]);
	}
	
}
int main(){
	int val[]={8,7,6,5,4,3,2,1};
	findMink(val,8,4);
	return 0;
}


第8题

非算法题目,略,有兴趣读者参考原贴。

第9题

判断整数序列是不是二元查找树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
如果是返回true,否则返回false。
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
  8
  / /
  6 10
  / / / /
  5 7 9 11
因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。


(下面的解法是参考原作者给出的答案,对原作者的解法表示赞)

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3.   
  4. int assertTree(int *val,int len){  
  5.     if(val==NULL||len<=0) return 0;  
  6.     int root=val[len-1];  
  7.   
  8.     int i=0;  
  9.     for(;i<len-1;i++){  
  10.         if(val[i]>root)  
  11.             break;  
  12.     }  
  13.   
  14.     int j=i;  
  15.     for(;j<len-1;++j){  
  16.         if(val[j]<root)  
  17.             return 0;  
  18.     }  
  19.     int left=1;  
  20.     if(i>0)  
  21.         left=assertTree(val,i);  
  22.     int right=1;  
  23.     if(i<len-1)  
  24.         right=assertTree(val+i,len-i-1);  
  25.     return (left&&right);  
  26. }  
  27. int main(){  
  28.     int val[]={5,7,6,9,11,10,8};  
  29.     int res=assertTree(val,7);  
  30.     printf("%d\n",res);  
  31.     return 0;  
  32. }  
#include <stdio.h>
#include <assert.h>

int assertTree(int *val,int len){
	if(val==NULL||len<=0) return 0;
	int root=val[len-1];

	int i=0;
	for(;i<len-1;i++){
		if(val[i]>root)
			break;
	}

	int j=i;
	for(;j<len-1;++j){
		if(val[j]<root)
			return 0;
	}
	int left=1;
	if(i>0)
		left=assertTree(val,i);
	int right=1;
	if(i<len-1)
		right=assertTree(val+i,len-i-1);
	return (left&&right);
}
int main(){
	int val[]={5,7,6,9,11,10,8};
	int res=assertTree(val,7);
	printf("%d\n",res);
	return 0;
}

2.设计包含min函数的栈。
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。

要求函数min、push以及pop的时间复杂度都是O(1)。

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4.   
  5. typedef struct _Ele{  
  6.     int val;  
  7.     struct _Ele *next;  
  8.     struct _Ele *currentMin;  
  9. }Ele;  
  10.   
  11. struct Stack{  
  12.     Ele *top;  
  13.       
  14. };  
  15.   
  16. struct Stack *initStack(){  
  17.     struct Stack *s;  
  18.     s=(struct Stack *)malloc(sizeof(struct Stack));  
  19.     s->top=NULL;  
  20.   
  21.     return s;  
  22. }  
  23. int empty(struct Stack *s){  
  24.     if(s->top==NULL){  
  25.         return 1;  
  26.     }  
  27.     else{  
  28.         return 0;  
  29.     }  
  30. }  
  31. void push(struct Stack *s,int val){  
  32.     /* init the new node */  
  33.     Ele *e=(Ele *)malloc(sizeof(Ele));  
  34.     e->val=val;  
  35.     e->next=NULL;  
  36.     e->currentMin=e;  
  37.       
  38.     if(s->top!=NULL){      
  39.         int min=s->top->currentMin->val;  
  40.         if(val<min){  
  41.             e->currentMin=e;  
  42.         }  
  43.         else{  
  44.             e->currentMin=s->top->currentMin;  
  45.         }  
  46.     }  
  47.   
  48.     e->next=s->top;  
  49.     s->top=e;  
  50. }  
  51. int min(struct Stack *s){  
  52.     if(s==NULL){  
  53.         printf("the stack is empty\n");  
  54.         exit(-1);  
  55.     }  
  56.     return s->top->currentMin->val;  
  57. }  
  58. int pop(struct Stack *s){  
  59.     if(s->top==NULL){  
  60.         printf("the stack is empty\n");  
  61.         exit(-1);  
  62.     }  
  63.       
  64.     Ele *newtop=s->top->next;  
  65.     int retval=s->top->val;  
  66.     free(s->top);  
  67.     s->top=newtop;  
  68.     return retval;  
  69.   
  70. }  
  71. int main(){  
  72.   
  73.     struct Stack *mysta=NULL;  
  74.     mysta=initStack();  
  75.     push(mysta,3);  
  76.     push(mysta,2);  
  77.     push(mysta,4);  
  78.     push(mysta,5);  
  79.     push(mysta,1);  
  80.     push(mysta,9);  
  81.     int p=pop(mysta);  
  82.     printf("pop out %d\n",p);  
  83.     int d=min(mysta);  
  84.     printf("the min is %d\n",d);  
  85.     int p2=pop(mysta);  
  86.     printf("pop out %d\n",p2);  
  87.     int d2=min(mysta);  
  88.     printf("the new min is %d\n",d2);  
  89.     return  0;  
  90. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef struct _Ele{
	int val;
	struct _Ele *next;
	struct _Ele *currentMin;
}Ele;

struct Stack{
	Ele *top;
	
};

struct Stack *initStack(){
	struct Stack *s;
	s=(struct Stack *)malloc(sizeof(struct Stack));
	s->top=NULL;

	return s;
}
int empty(struct Stack *s){
	if(s->top==NULL){
		return 1;
	}
	else{
		return 0;
	}
}
void push(struct Stack *s,int val){
	/* init the new node */
	Ele *e=(Ele *)malloc(sizeof(Ele));
	e->val=val;
	e->next=NULL;
	e->currentMin=e;
	
	if(s->top!=NULL){	
		int min=s->top->currentMin->val;
		if(val<min){
			e->currentMin=e;
		}
		else{
			e->currentMin=s->top->currentMin;
		}
	}

	e->next=s->top;
	s->top=e;
}
int min(struct Stack *s){
	if(s==NULL){
		printf("the stack is empty\n");
		exit(-1);
	}
	return s->top->currentMin->val;
}
int pop(struct Stack *s){
	if(s->top==NULL){
		printf("the stack is empty\n");
		exit(-1);
	}
	
	Ele *newtop=s->top->next;
	int retval=s->top->val;
	free(s->top);
	s->top=newtop;
	return retval;

}
int main(){

	struct Stack *mysta=NULL;
	mysta=initStack();
	push(mysta,3);
	push(mysta,2);
	push(mysta,4);
	push(mysta,5);
	push(mysta,1);
	push(mysta,9);
	int p=pop(mysta);
	printf("pop out %d\n",p);
	int d=min(mysta);
	printf("the min is %d\n",d);
	int p2=pop(mysta);
	printf("pop out %d\n",p2);
	int d2=min(mysta);
	printf("the new min is %d\n",d2);
	return  0;
}



第10题
翻转句子中单词的顺序。
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。
为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。


  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <assert.h>   
  4. #include <string.h>   
  5. void swap(char *first,char *last){  
  6.     assert(first!=NULL&&last!=NULL);  
  7.     char tmp;  
  8.     while(first<last){  
  9.         tmp=*first;  
  10.         *first=*last;  
  11.         *last=tmp;  
  12.         first++;  
  13.         last--;  
  14.     }  
  15. }  
  16. void reverseWord(char *str,int len){  
  17.     assert(str!=NULL&&len>0);  
  18.     char *head=str,*tail=str+len-1;  
  19.     char tmp;  
  20.     while(head<tail){  
  21.         tmp=*head;  
  22.         *head=*tail;  
  23.         *tail=tmp;  
  24.         head++;  
  25.         tail--;  
  26.     }  
  27.     head=str;  
  28.     tail=str;  
  29.     for(tail=str;*tail!='\0';tail++){  
  30.         if(*tail==' '){  
  31.             tail--;  
  32.             swap(head,tail);  
  33.             tail++;  
  34.             head=tail+1;  
  35.         }  
  36.     }  
  37.     if(*tail=='\0'){  
  38.         tail--;  
  39.         swap(head,tail);  
  40.     }  
  41.     return ;  
  42.       
  43. }  
  44. int main(){  
  45.     char str[]="I am a student.";  
  46.     printf("%s\n",str);  
  47.     reverseWord(str,strlen(str));  
  48.     printf("%s\n",str);  
  49.     return 0;  
  50. }  
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
void swap(char *first,char *last){
	assert(first!=NULL&&last!=NULL);
	char tmp;
	while(first<last){
		tmp=*first;
		*first=*last;
		*last=tmp;
		first++;
		last--;
	}
}
void reverseWord(char *str,int len){
	assert(str!=NULL&&len>0);
	char *head=str,*tail=str+len-1;
	char tmp;
	while(head<tail){
		tmp=*head;
		*head=*tail;
		*tail=tmp;
		head++;
		tail--;
	}
	head=str;
	tail=str;
	for(tail=str;*tail!='\0';tail++){
		if(*tail==' '){
			tail--;
			swap(head,tail);
			tail++;
			head=tail+1;
		}
	}
	if(*tail=='\0'){
		tail--;
		swap(head,tail);
	}
	return ;
	
}
int main(){
	char str[]="I am a student.";
	printf("%s\n",str);
	reverseWord(str,strlen(str));
	printf("%s\n",str);
	return 0;
}


第11题
求二叉树中节点的最大距离...

如果我们把二叉树看成一个图,
父子节点之间的连线看成是双向的,
我们姑且定义"距离"为两节点之间边的个数。
写一个程序,
求一棵二叉树中相距最远的两个节点之间的距离。


这个本人暂时没想到什么比较好的办法,有兴趣的读者自己参考http://blog.csdn.net/v_july_v/article/details/5990750

第12题
题目:求1+2+…+n,
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。

这个题目俺想了好久,原作者给的答案用C++的虚函数去实现的好像,比较复杂。

俺这里给出一个简单实现版。

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. int add1ton(int n,int *res){  
  4.     *res=1;  
  5.     int ret;  
  6.     int *mid=(int *)malloc(sizeof(int));  
  7.     return (n-1==0)||(add1ton(n-1,mid),*res=*mid+n);  
  8.   
  9. }  
  10. int main(){  
  11.     int *p=(int *)malloc(sizeof(int));  
  12.     add1ton(100,p);  
  13.     printf("result is %d\n",*p);  
  14.     return 0;  
  15. }  
#include <stdio.h>
#include <stdlib.h>
int add1ton(int n,int *res){
	*res=1;
	int ret;
	int *mid=(int *)malloc(sizeof(int));
	return (n-1==0)||(add1ton(n-1,mid),*res=*mid+n);

}
int main(){
	int *p=(int *)malloc(sizeof(int));
	add1ton(100,p);
	printf("result is %d\n",*p);
	return 0;
}


第13题:
题目:输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。
链表结点定义如下:   
struct ListNode
{
  int m_nKey;
  ListNode* m_pNext;
};

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <assert.h>   
  4.   
  5. typedef struct _linknode{  
  6.     int key;  
  7.     struct _linknode *next;  
  8. }linknode;  
  9.   
  10. void locateRK(linknode *first,int k){  
  11.     assert(k>=0&&first!=NULL);  
  12.     linknode *p=first;  
  13.     linknode *prek=p;  
  14.     while(k>0){  
  15.         assert(p!=NULL);  
  16.         p=p->next;  
  17.         k--;  
  18.     }  
  19.     while(p!=NULL){  
  20.         p=p->next;  
  21.         prek=prek->next;  
  22.     }  
  23.     printf("the last k val is %c\n",prek->key);  
  24. }  
  25. int main(){  
  26.     char c;  
  27.     linknode *head=NULL;  
  28.     linknode *tail=head;  
  29.     while(c=getchar())  
  30.     {  
  31.         if(c=='#'break;  
  32.         linknode *p=(linknode *)malloc(sizeof(linknode));  
  33.         p->key=c;  
  34.         p->next=NULL;  
  35.         if(head==NULL){  
  36.             head=p;  
  37.             tail=p;  
  38.         }  
  39.         else{  
  40.             tail->next=p;  
  41.             tail=p;  
  42.         }  
  43.     }  
  44.     locateRK(head,3);  
  45. }  
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct _linknode{
	int key;
	struct _linknode *next;
}linknode;

void locateRK(linknode *first,int k){
	assert(k>=0&&first!=NULL);
	linknode *p=first;
	linknode *prek=p;
	while(k>0){
		assert(p!=NULL);
		p=p->next;
		k--;
	}
	while(p!=NULL){
		p=p->next;
		prek=prek->next;
	}
	printf("the last k val is %c\n",prek->key);
}
int main(){
	char c;
	linknode *head=NULL;
	linknode *tail=head;
	while(c=getchar())
	{
		if(c=='#') break;
		linknode *p=(linknode *)malloc(sizeof(linknode));
		p->key=c;
		p->next=NULL;
		if(head==NULL){
			head=p;
			tail=p;
		}
		else{
			tail->next=p;
			tail=p;
		}
	}
	locateRK(head,3);
}



第14题:题目:输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4. #include <string.h>   
  5. /** 
  6.  *in a given array,to find two number 
  7.  ,the sum of which is sum 
  8.  * assume the max of sum is 100 
  9.  * 
  10.  */  
  11.   
  12. typedef unsigned int uint;  
  13. void findAddElem(uint *num,int len,uint sum){  
  14.     int MAX=100;  
  15.     int MASK=0x01;  
  16.     int bytelen=8;  
  17.     char *bitmap=(char *)malloc(1+MAX/bytelen);  
  18.     memset(bitmap,0,1+MAX/bytelen);  
  19.     int i;  
  20.     /* to prepare ,make the bitmap */  
  21.     for(i=0;i<len;i++){  
  22.         uint val=num[i];  
  23.         uint outeroffset=(int)(val/bytelen);  
  24.         uint inneroffset=val%bytelen;  
  25.         bitmap[outeroffset]|=MASK<<inneroffset;  
  26.     }  
  27.     /*this time find the needed bit is set or not */  
  28.     for(i=0;i<len;i++){  
  29.         uint val=num[i];  
  30.         uint left=sum-val;  
  31.         uint outeroffset=(int)(left/bytelen);  
  32.         uint inneroffset=left%bytelen;  
  33.   
  34.         uint flag=bitmap[outeroffset]&(MASK<<inneroffset);  
  35.         if(flag!=0){  
  36.             printf("%d+%d=%d\n",val,left,sum);  
  37.             break;  
  38.         }  
  39.     }  
  40.     free(bitmap);  
  41. }  
  42. int main(){  
  43.     uint numbers[5]={20,35,23,45,27};  
  44.     findAddElem(numbers,5,50);  
  45.     return 0;  
  46. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/**
 *in a given array,to find two number
 ,the sum of which is sum
 * assume the max of sum is 100
 *
 */

typedef unsigned int uint;
void findAddElem(uint *num,int len,uint sum){
	int MAX=100;
	int MASK=0x01;
	int bytelen=8;
	char *bitmap=(char *)malloc(1+MAX/bytelen);
	memset(bitmap,0,1+MAX/bytelen);
	int i;
	/* to prepare ,make the bitmap */
	for(i=0;i<len;i++){
		uint val=num[i];
		uint outeroffset=(int)(val/bytelen);
		uint inneroffset=val%bytelen;
		bitmap[outeroffset]|=MASK<<inneroffset;
	}
	/*this time find the needed bit is set or not */
	for(i=0;i<len;i++){
		uint val=num[i];
		uint left=sum-val;
		uint outeroffset=(int)(left/bytelen);
		uint inneroffset=left%bytelen;

		uint flag=bitmap[outeroffset]&(MASK<<inneroffset);
		if(flag!=0){
			printf("%d+%d=%d\n",val,left,sum);
			break;
		}
	}
	free(bitmap);
}
int main(){
	uint numbers[5]={20,35,23,45,27};
	findAddElem(numbers,5,50);
	return 0;
}


第15题:
题目:输入一颗二元查找树,将该树转换为它的镜像,
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
用递归和循环两种方法完成树的镜像转换。   

第16题:
题目(微软):
输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。   
例如输入

              a

      b               e

c         d       f          g

将输出abecdfg

15,16 类似,在一起实现了

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <assert.h>   
  4. struct _node {  
  5.     char val;  
  6.     struct _node *left;  
  7.     struct _node *right;  
  8. };  
  9. typedef struct _node node;  
  10.   
  11. typedef struct _stackE{  
  12.     struct _stackE *next;  
  13.     node *data;  
  14. }stackE;  
  15. static stackE *head=NULL;  
  16. static stackE *tail=NULL;  
  17.   
  18. static void enqueue(node *p){  
  19.     /* ignored the overflow */  
  20.     stackE *ele=(stackE *)malloc(sizeof(stackE));  
  21.     ele->data=p;  
  22.     ele->next=NULL;  
  23.     if(head==NULL||tail==NULL){  
  24.         head=tail=ele;  
  25.     }  
  26.     else{  
  27.         assert(tail!=NULL);  
  28.         tail->next=ele;  
  29.         tail=ele;  
  30.     }  
  31. }  
  32. static node *dequeue(){  
  33.     node *ret=NULL;  
  34.     if(head==NULL||tail==NULL){  
  35.         return ret;  
  36.     }  
  37.     stackE *p=head;  
  38.     ret=p->data;  
  39.     head=p->next;  
  40.     if(head==NULL){  
  41.         tail=NULL;  
  42.     }  
  43.     free(p);  
  44.     return ret;  
  45. }  
  46. static int isEmpty(){  
  47.     if(head==NULL||tail==NULL){  
  48.         return 1;  
  49.     }  
  50.     else{  
  51.         return 0;  
  52.     }  
  53. }  
  54. void levelWalk(node *tree){  
  55.     if(tree==NULL){  
  56.         printf("the tree is null\n");  
  57.         return ;  
  58.     }  
  59.     else{  
  60.         enqueue(tree);  
  61.         while(!isEmpty()){  
  62.             node *p=dequeue();  
  63.             printf("%c\t",p->val);  
  64.             if(p->left!=NULL){  
  65.                 enqueue(p->left);  
  66.             }  
  67.             if(p->right!=NULL){  
  68.                 enqueue(p->right);  
  69.             }  
  70.         }  
  71.     }  
  72. }  
  73. void formMirror(node *tree){  
  74.     if(tree==NULL){  
  75.         return;  
  76.     }  
  77.     else{  
  78.         enqueue(tree);  
  79.         while(!isEmpty()){  
  80.             node *p=dequeue();  
  81.             if(p->left!=NULL){  
  82.                 enqueue(p->left);  
  83.             }  
  84.             if(p->right!=NULL){  
  85.                 enqueue(p->right);  
  86.             }  
  87.             node *tmp=p->left;  
  88.             p->left=p->right;  
  89.             p->right=tmp;  
  90.         }  
  91.     }  
  92. }  
  93. /** 
  94.   * form mirror tree,recursive algorithm 
  95.   * 
  96.  */  
  97. void makeMirror(node *tree){  
  98.     if(tree==NULL){  
  99.         return;  
  100.     }  
  101.     else{  
  102.         if(tree->left!=NULL){  
  103.             makeMirror(tree->left);  
  104.         }  
  105.         if(tree->right!=NULL){  
  106.             makeMirror(tree->right);  
  107.         }  
  108.         node *tmp=tree->left;  
  109.         tree->left=tree->right;  
  110.         tree->right=tmp;  
  111.     }  
  112. }  
  113. /* 
  114.  * to create a tree ,sample input string:abc##d##ef##g## 
  115.  */  
  116. node *createTree(){  
  117.     char val;  
  118.     val=getchar();  
  119.     if(val=='#'){  
  120.         return NULL;  
  121.     }  
  122.     else{  
  123.           
  124.         node *root=(node *)malloc(sizeof(node));  
  125.         root->val=val;  
  126.         root->left=createTree();  
  127.         root->right=createTree();  
  128.         return root;  
  129.     }  
  130. }  
  131. void midWalk(node *p){  
  132.     if(p!=NULL){  
  133.         midWalk(p->left);  
  134.         printf("%c\t",p->val);  
  135.         midWalk(p->right);  
  136.     }  
  137. }  
  138. int main(){  
  139.     node *tree=createTree();  
  140.     levelWalk(tree);  
  141.     printf("\n");  
  142.     makeMirror(tree);  
  143.     levelWalk(tree);  
  144.     printf("\n");  
  145.     formMirror(tree);  
  146.     levelWalk(tree);  
  147.     printf("\n");  
  148.     return 0;  
  149. }  
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct _node {
	char val;
	struct _node *left;
	struct _node *right;
};
typedef struct _node node;

typedef struct _stackE{
	struct _stackE *next;
	node *data;
}stackE;
static stackE *head=NULL;
static stackE *tail=NULL;

static void enqueue(node *p){
	/* ignored the overflow */
	stackE *ele=(stackE *)malloc(sizeof(stackE));
	ele->data=p;
	ele->next=NULL;
	if(head==NULL||tail==NULL){
		head=tail=ele;
	}
	else{
		assert(tail!=NULL);
		tail->next=ele;
		tail=ele;
	}
}
static node *dequeue(){
	node *ret=NULL;
	if(head==NULL||tail==NULL){
		return ret;
	}
	stackE *p=head;
	ret=p->data;
	head=p->next;
	if(head==NULL){
		tail=NULL;
	}
	free(p);
	return ret;
}
static int isEmpty(){
	if(head==NULL||tail==NULL){
		return 1;
	}
	else{
		return 0;
	}
}
void levelWalk(node *tree){
	if(tree==NULL){
		printf("the tree is null\n");
		return ;
	}
	else{
		enqueue(tree);
		while(!isEmpty()){
			node *p=dequeue();
			printf("%c\t",p->val);
			if(p->left!=NULL){
				enqueue(p->left);
			}
			if(p->right!=NULL){
				enqueue(p->right);
			}
		}
	}
}
void formMirror(node *tree){
	if(tree==NULL){
		return;
	}
	else{
		enqueue(tree);
		while(!isEmpty()){
			node *p=dequeue();
			if(p->left!=NULL){
				enqueue(p->left);
			}
			if(p->right!=NULL){
				enqueue(p->right);
			}
			node *tmp=p->left;
			p->left=p->right;
			p->right=tmp;
		}
	}
}
/**
  * form mirror tree,recursive algorithm
  *
 */
void makeMirror(node *tree){
	if(tree==NULL){
		return;
	}
	else{
		if(tree->left!=NULL){
			makeMirror(tree->left);
		}
		if(tree->right!=NULL){
			makeMirror(tree->right);
		}
		node *tmp=tree->left;
		tree->left=tree->right;
		tree->right=tmp;
	}
}
/*
 * to create a tree ,sample input string:abc##d##ef##g##
 */
node *createTree(){
	char val;
	val=getchar();
	if(val=='#'){
		return NULL;
	}
	else{
		
		node *root=(node *)malloc(sizeof(node));
		root->val=val;
		root->left=createTree();
		root->right=createTree();
		return root;
	}
}
void midWalk(node *p){
	if(p!=NULL){
		midWalk(p->left);
		printf("%c\t",p->val);
		midWalk(p->right);
	}
}
int main(){
	node *tree=createTree();
	levelWalk(tree);
	printf("\n");
	makeMirror(tree);
	levelWalk(tree);
	printf("\n");
	formMirror(tree);
	levelWalk(tree);
	printf("\n");
	return 0;
}


第17题:
题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。   

  1. #include <stdio.h>   
  2. #include <assert.h>   
  3. #include <stdlib.h>   
  4. #include <string.h>   
  5.   
  6. void findTheFirstSingle(char *str,int len){  
  7.     if(str==NULL){  
  8.         printf("argument error\n");  
  9.         exit(-1);  
  10.     }  
  11.     assert(len>0);  
  12.     char flag[256];  
  13.     memset(flag,0,256);  
  14.     char *p=str;  
  15.     int i;  
  16.     int index;  
  17.     for(i=0;i<len;i++){  
  18.         index=(int)(*(p+i));  
  19.         flag[index]+=1;  
  20.     }  
  21.     for(i=0;i<256;i++){  
  22.         if(flag[i]==1){  
  23.             printf("the char is %c\n",i);  
  24.             break;  
  25.         }  
  26.     }  
  27. }  
  28. int main(){  
  29.     char *str="abaccdef";  
  30.     findTheFirstSingle(str,strlen(str));  
  31.     return 0;  
  32. }  
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

void findTheFirstSingle(char *str,int len){
	if(str==NULL){
		printf("argument error\n");
		exit(-1);
	}
	assert(len>0);
	char flag[256];
	memset(flag,0,256);
	char *p=str;
	int i;
	int index;
	for(i=0;i<len;i++){
		index=(int)(*(p+i));
		flag[index]+=1;
	}
	for(i=0;i<256;i++){
		if(flag[i]==1){
			printf("the char is %c\n",i);
			break;
		}
	}
}
int main(){
	char *str="abaccdef";
	findTheFirstSingle(str,strlen(str));
	return 0;
}


第18题:
题目:n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始,
每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字)。
当一个数字删除后,从被删除数字的下一个继续删除第m个数字。
求出在这个圆圈中剩下的最后一个数字。

  1. /** 
  2.  *  function : int delNum(int n,int m); 
  3.  *  description: there are n persons,marked as 0,1,...n-1; 
  4.  *  and they form a circle,now from 0,to n-1,then back to 0 ; 
  5.  *  now from 0,begin count,if someone meet m ,then that person is removed. 
  6.  *  the next person begin from 0,and keep removing the nth person. 
  7.  *  remove n-1 times,then find the last person. 
  8.  *  this algorithm is some kind of  complex. 
  9.  * 
  10.  */   
  11. #include <stdio.h>   
  12. #include <stdlib.h>   
  13. #include <assert.h>   
  14. struct _node{  
  15.     int data;  
  16.     char flag;  
  17.     struct _node *next;  
  18. };  
  19. typedef struct _node  node;  
  20. int delNum(int n,int m){  
  21.     assert(n>0);  
  22.     assert(m>0);  
  23.     node *header=NULL;  
  24.     node *tail=NULL;  
  25.     int i;  
  26.     for(i=0;i<n;i++){//init   
  27.         node *tmp=(node *)malloc(sizeof(node));  
  28.         tmp->data=i;  
  29.         tmp->flag=1;  
  30.         if(i==0){  
  31.             header=tmp;  
  32.             tail=tmp;  
  33.             tmp->next=tmp;  
  34.         }  
  35.         tail->next=tmp;  
  36.         tail=tmp;  
  37.         tmp->next=header;  
  38.     }  
  39.     node *cycle=header;  
  40.     node *pre=NULL;  
  41.     for(i=0;i<n-1;i++){ //do n-1 times delete   
  42.         int counter=0;  
  43.         while(counter<=m){  
  44.             if(counter==m){  
  45.                 printf("delete val %d\n",pre->data);  
  46.                 pre->flag=0;  
  47.                 break;  
  48.             }  
  49.             if(cycle->flag==1){  
  50.                 counter++;  
  51.                 pre=cycle;  
  52.                 cycle=cycle->next;  
  53.             }  
  54.             else{  
  55.                 pre=cycle;  
  56.                 cycle=cycle->next;  
  57.             }  
  58.         }  
  59.     }  
  60.     node *p=header;  
  61.     while(p!=tail){  
  62.         if(p->flag==1){  
  63.             printf("left value is %d\n",p->data);  
  64.         }  
  65.         node *next=p->next;  
  66.         free(p);  
  67.         p=next;  
  68.     }  
  69.     if(tail->flag==1){  
  70.         printf("left value is %d\n",p->data);  
  71.     }  
  72.     free(tail);  
  73.     return 0;  
  74. }  
  75. int main(){  
  76.     delNum(5,3);  
  77.     return 0;  
  78. }  
/**
 *	function : int delNum(int n,int m);
 *	description: there are n persons,marked as 0,1,...n-1;
 *	and they form a circle,now from 0,to n-1,then back to 0 ;
 *	now from 0,begin count,if someone meet m ,then that person is removed.
 *	the next person begin from 0,and keep removing the nth person.
 *	remove n-1 times,then find the last person.
 *	this algorithm is some kind of  complex.
 *
 */ 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct _node{
	int data;
	char flag;
	struct _node *next;
};
typedef struct _node  node;
int delNum(int n,int m){
	assert(n>0);
	assert(m>0);
	node *header=NULL;
	node *tail=NULL;
	int i;
	for(i=0;i<n;i++){//init
		node *tmp=(node *)malloc(sizeof(node));
		tmp->data=i;
		tmp->flag=1;
		if(i==0){
			header=tmp;
			tail=tmp;
			tmp->next=tmp;
		}
		tail->next=tmp;
		tail=tmp;
		tmp->next=header;
	}
	node *cycle=header;
	node *pre=NULL;
	for(i=0;i<n-1;i++){ //do n-1 times delete
		int counter=0;
		while(counter<=m){
			if(counter==m){
				printf("delete val %d\n",pre->data);
				pre->flag=0;
				break;
			}
			if(cycle->flag==1){
				counter++;
				pre=cycle;
				cycle=cycle->next;
			}
			else{
				pre=cycle;
				cycle=cycle->next;
			}
		}
	}
	node *p=header;
	while(p!=tail){
		if(p->flag==1){
			printf("left value is %d\n",p->data);
		}
		node *next=p->next;
		free(p);
		p=next;
	}
	if(tail->flag==1){
		printf("left value is %d\n",p->data);
	}
	free(tail);
	return 0;
}
int main(){
	delNum(5,3);
	return 0;
}


第19题:
题目:定义Fibonacci数列如下:   
  / 0 n=0
f(n)= 1 n=1
  / f(n-1)+f(n-2) n=2

输入n,用最快的方法求该数列的第n项。

  1. /* 
  2.  *  this algorithm compute the fibonacci values 
  3.  *  function prototype 
  4.  *  int fib(int n); 
  5.  */  
  6. #include <stdio.h>   
  7. #include <stdlib.h>   
  8. #include <assert.h>   
  9. #include <string.h>   
  10. int fib(int n){  
  11.     assert(n>=0);  
  12.     int *p=(int *)malloc(4*(n+1));  
  13.     memset(p,0,4*(n+1));  
  14.     p[0]=0;  
  15.     p[1]=1;  
  16.     int i;  
  17.     for(i=2;i<=n;i++){  
  18.         p[i]=p[i-1]+p[i-2];  
  19.         printf("p[%d]=%d\n",i,p[i]);  
  20.     }  
  21.     return p[n];  
  22.     free(p);  
  23. }  
  24. int main(){  
  25.     fib(10);  
  26. }  
/*
 *	this algorithm compute the fibonacci values
 *	function prototype
 *	int fib(int n);
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int fib(int n){
	assert(n>=0);
	int *p=(int *)malloc(4*(n+1));
	memset(p,0,4*(n+1));
	p[0]=0;
	p[1]=1;
	int i;
	for(i=2;i<=n;i++){
		p[i]=p[i-1]+p[i-2];
		printf("p[%d]=%d\n",i,p[i]);
	}
	return p[n];
	free(p);
}
int main(){
	fib(10);
}



第20题:
题目:输入一个表示整数的字符串,把该字符串转换成整数并输出。
例如输入字符串"345",则输出整数345。

  1. /*description: 
  2.  *  this algorithm change digital numbers to integer 
  3.  *  such as ,turn "345" int the integer 345 
  4.  *  attention here ignored some of the type check 
  5. */  
  6. #include <stdio.h>   
  7. #include <stdlib.h>   
  8. int strToInt(char *str,int len){  
  9.     //simple argument check   
  10.     if((!str)||(len<=0)){  
  11.         printf("argument error\n");  
  12.         exit(-1);  
  13.     }  
  14.     //here ignored to check the character 0-9   
  15.     if(len==1){  
  16.         int val=(*str)-'0';  
  17.         return val;  
  18.     }  
  19.     else{  
  20.         int val=strToInt(str,len-1);  
  21.         char *p=str+len-1;  
  22.         int ret=10*val+(*p)-'0';  
  23.         return ret;  
  24.     }  
  25. }  
  26. int main(int argc,char **argv){  
  27.     char *test="5234";  
  28.     int ret=strToInt(test,4);  
  29.     printf("the value is %d\n",ret);  
  30.     return 0;  
  31. }  
/*description:
 *	this algorithm change digital numbers to integer
 *	such as ,turn "345" int the integer 345
 *  attention here ignored some of the type check
*/
#include <stdio.h>
#include <stdlib.h>
int strToInt(char *str,int len){
	//simple argument check
	if((!str)||(len<=0)){
		printf("argument error\n");
		exit(-1);
	}
	//here ignored to check the character 0-9
	if(len==1){
		int val=(*str)-'0';
		return val;
	}
	else{
		int val=strToInt(str,len-1);
		char *p=str+len-1;
		int ret=10*val+(*p)-'0';
		return ret;
	}
}
int main(int argc,char **argv){
	char *test="5234";
	int ret=strToInt(test,4);
	printf("the value is %d\n",ret);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值