當前位置:編程學習大全網 - 編程語言 - c++編程修地鐵移走數目,求剩余樹的棵數

c++編程修地鐵移走數目,求剩余樹的棵數

調試通過:

#include<iostream>

using namespace std;

struct Note{

int begin;

int end;

Note *next;

};

int main()

{

int l,m;

int i;

Note *p,*q;

cin >> l >> m;

Note *head = new Note;

Note *tail;

head->next = NULL;

cin >> head->begin >> head->end;

tail = head;

for(i=1;i<m;i++)

{//依次讀入m個區域,並按照升序排列區域

p = new Note;

cin >> p->begin >> p->end ;

p->next = NULL;

q = head;

if( p->end < head->begin )

{//在鏈表頭插入節點

p->next = head;

head = p;

continue;

}

if( p->begin > tail->end )

{//在鏈表尾插入節點

tail->next = p;

tail = p;

continue;

}

while(q)

{

if( p->begin<=q->begin && p->end>=q->begin || p->begin<=q->end && p->end<q->next->begin )

{//兩個區域相交

q->begin = p->begin<q->begin ? p->begin : q->begin;

q->end = p->end < q->end ? q->end : p->end;

delete p;

break;

}

else if( p->begin<=q->end && p->end>=q->next->begin )

{//新增區域連接兩個相鄰區域

q->begin = p->begin<q->begin ? p->begin : q->begin;

q->end = q->next->end;

delete p;

q->next = q->next->next;

p = q->next;

delete p;

break;

}

else if( p->begin>q->end && p->end<q->next->begin )

{//在某個節點後插入新增區域

p->next = q->next;

q->next = p;

break;

}

q = q->next;

}

}

l++; //從0到l有l+1棵樹

q = head;

while(q)

{

//cout << q->begin << '\t' << q->end << endl;調試用

l -= q->end - q->begin + 1;

q = q->next;

}

cout << l << endl;

return 0;

}

  • 上一篇:永恒之塔天族魔道
  • 下一篇:0-3歲早期教育活動教案
  • copyright 2024編程學習大全網