合并两个有序的链表和计算1+2+3+4....

//合并两个有序链表,使合并后依然有序
PNode MergeList(PNode pHead1, PNode pHead2)   //递归实现
{
	if (pHead1 == NULL)
		return pHead2;
	else if (pHead2 == NULL)
		return pHead1;
	PNode newhead = NULL;
	if (pHead1->data < pHead2->data)
	{
		newhead = pHead1;
		newhead->_next = MergeList(pHead1->_next, pHead2);
	}
	else
	{
		newhead = pHead2;
		newhead->_next = MergeList(pHead1, pHead2->_next);
	}
	return newhead;
}



/*##############################################################
1+2+3+4+5+........+n
###############################################################*/
n*(n - 2) / 2
num = 0;
while (n--)
{
	mun = num + n;
}
//利用构造函数  我们可以先定义一个类型,然后创建n个该类型的实例,
//这样构造函数会被调用n次,可以把累加代码放在构造函数中
class GetAddnum
{
public:
	GetAddnum()
	{
		N++;
		sum = sum + N;
	}
	static void Reset() //初始化
	{
		N = 0;
		sum = 0;
	}
	static unsigned int printfnum()
	{
		return sum;
	}
private:
	static unsigned int N;    //静态变量函数
	static unsigned int sum;
};



unsigned int GetAddnum_solution(int n)
{
	GetAddnum::Reset();  //初始化
	GetAddnum* p = new GetAddnum[n];
	delete[] p;
	p = NULL;
	return GetAddnum::printfnum();
}
int main()
{
	GetAddnum_solution(7);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值