posts - 16, comments - 0, trackbacks - 0, articles - 0

C/C++习题

Posted on 2008-11-10 15:01 dengbo 阅读(193) 评论(0)  编辑 收藏 引用
递归算法判断数a[N]是否一个增数
#include "stdafx.h"
#include 
<iostream>

using namespace std;


bool Compare(int ar[] ,int n)
{
    
if(n==1)
        
return true;
    
if(n==2)
        
return ar[n-1]>=ar[n-2];
    
return Compare(ar, n-1&& ar[n-1]>=ar[n-2];
}

void main()
{
    
int a[7]={1,2,3,4,5,6,5};
    cout
<<Compare(a,7);
    
    system(
"pause");
}


线形表ab两个有序升序的线形表,写一程序,使两个有序线形表合并成一个有序升序线形表h
LinkList L, L2,L3;
    Link p,q, tmp;
    CreateList(
&L);
    CreateList(
&L2);
    CreateList(
&L3);
    
for(int i=0;i<10;i++)
    
{
        
if(i==0)
        
{
            MakeNode(
&q,i);
            p
=q;
            L.head
->next=p;
            L.tail
=p;
            
++L.len;
        }

        
else
        
{
            MakeNode(
&q,i);
            L.tail
=q;
            p
->next=q;
            p
=q;
            
++L.len;
        }

    }


    
for(int i=10;i<20;i++)
    
{
        
if(i==10)
        
{
            MakeNode(
&q,i);
            p
=q;
            L2.head
->next=p;
            L2.tail
=p;
            
++L2.len;
        }

        
else
        
{
            MakeNode(
&q,i);
            L2.tail
=q;
            p
->next=q;
            p
=q;
            
++L2.len;
        }

    }



    p
=L2.head->next;
    q
=L.head->next;
    
if(p->data<q->data)
    
{
        tmp
=p;
        L.head
->next=tmp;
        p
=p->next;
    }

    
else
    
{
        tmp
=q;
        L.head
->next=q;
        q
=q->next;
    }

    L3.head
->next=tmp; 
    Link tmp1;
    
while(p!=NULL&&q!=NULL)
    
{
        
if(p->data<q->data)
        
{
            tmp
->next=p;
            tmp
=tmp->next;
            p
=p->next;
        }

        
else
        
{
            tmp
->next=q;
            tmp
=tmp->next;
            q
=q->next;
        }

    }


    
while(p!=NULL)
    
{
        tmp
->next=p;
        tmp
=tmp->next;
        p
=p->next;
    }

    
while(q!=NULL)
    
{
        tmp
->next=q;
        tmp
=tmp1;
        q
=q->next;
    }

    L3.len
=L.len+L2.len;
    L3.tail
=tmp;
    tmp
->next=NULL;
    p
=L3.head->next; 
    
while(p)
    
{
        cout
<<p->data<<endl;
        p
=p->next;
    }


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理