中兴通讯2015笔试应用题

题目1:

请用递归算法计算n的阶乘

特殊情况当n小于0时,阶乘是没有意义的。

当n=0时,阶乘为1。

#include <iostream>
using namespace std;
long long jiechengcore(long a)
{
	if(a==1)
		return 1;
	else
		return a*jiechengcore(a-1);
}
long long jiecheng(long a)
{
	if(a<0)
		return 0;
	else if(a==0)
		return 1;
	else
		return jiechengcore(a);
}
int main()
{
	cout<<jiecheng(-1)<<endl;
	cout<<jiecheng(0)<<endl;
	cout<<jiecheng(5)<<endl;
	cout<<jiecheng(14)<<endl;
}


2、已知一个链表的结点结构,并已知链表的头结点head,写一个函数把这个链表逆序(比如一个链表1->2->3->4->5通过反转后成为5->4->3->2->1,即为链表逆序)。链表的结点结构为:

struct Node{int data;Node *next;};

typedef struct Node Node;

struct Node
{
	int data;
	Node * next;
};
typedef struct Node Node;
void print(Node *head)
{
	while(head!=NULL)
	{
		cout<<head->data<<" ";
		head=head->next;
	}
	cout<<endl;
}
Node * createlist(int *arr,int len)
{
	if(arr==NULL||len<=0)
		return NULL;
	Node *head=new Node;
	Node *p=head;
	for(int ii=0;ii<len;ii++)
	{
		Node * temp=new Node;
		temp->data=arr[ii];
		temp->next=NULL;
		p->next=temp;
		p=temp;
	}
	p=head;
	head=head->next;
	delete p;
	return head;
}
void reverse(Node **head)
{
	if(head==NULL||*head==NULL)
		return;
	Node *p1=*head;
	Node *p2=p1->next;
	while(p2!=NULL)
	{
		Node *p3=p2->next;
		p2->next=p1;
		p1=p2;
		p2=p3;
	}
	(*head)->next=NULL;
	*head=p1;

}
int main()
{
	int arr[10]={};
	for(int ii=0;ii<10;ii++)
		arr[ii]=ii;
	Node *head=createlist(arr,10);
	print(head);
	reverse(&head);
	print(head);
	return 0;
}
测试结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值