2013届华为校园招聘机试题

题目一:子串分离 
题目描述:   
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。 
如果输入“abc def gh i        d”,结果将是abc,def,gh,i,d, 
 
要求实现函数:   
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr); 
 
【输入】  pInputStr:  输入字符串 
          lInputLen:  输入字符串长度                   
【输出】  pOutputStr:  输出字符串,空间已经开辟好,与输入字符串等长; 
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出 
示例   
输入:“abc def gh i        d”

输出:“abc,def,gh,i,d,”

方案1代码:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cassert>
using namespace std;
#define MAX 20
void DivideString(const char *pInputStr,long Inputlen,char *pOutputStr)
{
	int i,k;
	int j=0;
	for(k=0;k<Inputlen;k++)
	{
		if(pInputStr[k]!=' ')
			break;
	}
	for(i=k;i<Inputlen;)
	{
		if(pInputStr[i]==' ')
		{
			pOutputStr[j]=',';
			while(pInputStr[++i]==' ');
		}
		else
		{
		    pOutputStr[j]=pInputStr[i];
			i++;
		}
		j++;
	}
	pOutputStr[j]=',';
	pOutputStr[j+1]='\0';
}
int main(void)
{
	char *inputstr=(char *)malloc(sizeof(char )*MAX);
	char *outputstr=(char *)malloc(sizeof(char)*MAX);
	assert(inputstr!=NULL && outputstr!=NULL);
	gets(inputstr);
	int Len=strlen(inputstr);
	DivideString(inputstr,Len,outputstr);
	puts(outputstr);
	free(inputstr);
	free(outputstr);
	inputstr=outputstr=NULL;
}
方案2代码:

<span style="font-size:12px;">#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cassert>
using namespace std;
#define MAX 20
void DivideString(const char *pInputStr,long Inputlen,char *pOutputStr)
{
	int flag;  //flag=true 时为空格,false时为字母
	int j=0;
	int k;
	for(k=0;k<Inputlen;k++)//跳过前面的空格
		if(pInputStr[k]!=' ')
			break;
	for(int i=k;i<Inputlen;i++)
	{
		if(pInputStr[i]==' ')
		{
			if(flag==false)
			{
				pOutputStr[j]=',';
				flag=true;
			}
			else
				continue;
		}
		else
		{
			pOutputStr[j]=pInputStr[i];
			flag=false;
		}
		j++;
	}
	pOutputStr[j]=',';
	pOutputStr[j+1]='\0';
}
int main(void)
{
	char *inputstr=(char *)malloc(sizeof(char )*MAX);
	char *outputstr=(char *)malloc(sizeof(char)*MAX);
	assert(inputstr!=NULL && outputstr!=NULL);
	gets(inputstr);
	int Len=strlen(inputstr);
	DivideString(inputstr,Len,outputstr);
	puts(outputstr);
	puts(outputstr);
	free(inputstr);
	free(outputstr);
	inputstr=outputstr=NULL;
}
题目二:逆序链表输出。 
题目描述:   
将输入的一个单向链表,逆序后输出链表中的值。链表定义如下: 
typedef struct tagListNode 

      int value; 
      struct tagListNode *next; 
}ListNode; 
要求实现函数:   
void converse(ListNode **head); 
【输入】head:    链表头节点,空间已经开辟好 
【输出】head:    逆序后的链表头节点
【返回】无 
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出 

方案1代码:

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

typedef struct tagListNode
{
	int value;
	struct tagListNode *next;
}ListNode;

ListNode *converse(ListNode **head)
{
	if(*head==NULL)
		return NULL ;
	ListNode *pre_node=NULL;
	ListNode *cur_node=*head;
        ListNode *reverse_head;
	while(cur_node->next!=NULL)
	{
		ListNode *temp=cur_node->next;
		cur_node->next=pre_node;
		pre_node=cur_node;
		cur_node=temp;
	}
	cur_node->next = pre_node;
	reverse_head=cur_node;
	return reverse_head;
}

void Insert(ListNode **head,int i)
{
	ListNode *newnode=(ListNode *)malloc(sizeof(ListNode));
	assert(newnode);
	newnode->next=NULL;
	newnode->value=i;
	if((*head)==NULL)
	{
		*head=newnode;
	}
	else
	{
		newnode->next=*head;
		*head=newnode;
	}	
}
void PrintList(ListNode **head)
{
	for(ListNode *p=*head;p!=NULL;p=p->next)
	{
		cout<<p->value<<" ";
	}
	cout<<endl;
}

int main(void)
{
	ListNode *head=NULL;
	ListNode *reverse_head=NULL;
	for(int i=1;i<5;i++)
		Insert(&head,i);
	PrintList(&head);
	cout<<"output the end!"<<endl;
		
	reverse_head=converse(&head);
	PrintList(&reverse_head);
	
}
方案2代码:

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

typedef struct tagListNode
{
	int value;
	struct tagListNode *next;
}ListNode;

ListNode *reverse_head=NULL;

void converse(ListNode **head)
{
	if(*head==NULL)
		return ;
	ListNode *pre_node=NULL;
	ListNode *cur_node=*head;
	while(cur_node->next!=NULL)
	{
		ListNode *temp=cur_node->next;
		cur_node->next=pre_node;
		pre_node=cur_node;
		cur_node=temp;
	}
	cur_node->next = pre_node;
	reverse_head=cur_node;
}

void Insert(ListNode **head,int i)
{
	ListNode *newnode=(ListNode *)malloc(sizeof(ListNode));
	assert(newnode);
	newnode->next=NULL;
	newnode->value=i;
	if((*head)==NULL)
	{
		*head=newnode;
	}
	else
	{
		newnode->next=*head;
		*head=newnode;
	}	
}
void PrintList(ListNode **head)
{
	for(ListNode *p=*head;p!=NULL;p=p->next)
	{
		cout<<p->value<<" ";
	}
	cout<<endl;
}

int main(void)
{
	ListNode *head=NULL;
	for(int i=1;i<5;i++)
		Insert(&head,i);
	PrintList(&head);
	converse(&head);
	PrintList(&reverse_head);
	return 0;
	
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值