//合并两个有序链表,使合并后依然有序
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);
}
合并两个有序的链表和计算1+2+3+4....
最新推荐文章于 2022-04-30 22:56:22 发布