演算法競賽入門經典 5 2 STL 初步 木塊問題

2021-09-09 07:30:59 字數 1215 閱讀 3960

題目大意:

輸入n,得到編號為0~n-1的木塊,分別擺放在順序排列編號為0~n-1的位置。現對這些木塊進行操作,操作分為四種。

1、move a onto b:把木塊a、b上的木塊放回各自的原位,再把a放到b上;

2、move a over b:把a上的木塊放回各自的原位,再把a發到含b的堆上;

3、pile a onto b:把b上的木塊放回各自的原位,再把a連同a上的木塊移到b上;

4、pile a over b:把a連同a上木塊移到含b的堆上。

當輸入quit時,結束操作並輸出0~n-1的位置上的木塊情況

sample input

10move 9 onto 1

move 8 over 1

move 7 over 1

move 6 over 1

pile 8 over 6

pile 8 over 5

move 2 over 1

move 4 over 9

quit

sample output

0: 0

1: 1 9 2 4

2:3: 3

4:5: 5 8 7 6

6:7:

8:9:

不定長陣列(vector):vector是個不定長陣列,也是乙個模板類,需要用vectora或vectorb這樣的方式來宣告乙個vector。vector是乙個類似於int a[ ]的整型陣列,而vector是乙個類似於string a[ ]的字串陣列。

它把一些常用操作「封裝」在了vector型別內部。

例如a是乙個vector:

1.a.size()讀取它的大小。

2.a.resize()改變它的大小。

3.a.push_back()向尾部新增元素。

4.a.pop_back()刪除最後乙個元素。

#include#include#include#includeusing namespace std;

const int maxn=30;

int n;

vectorpile[maxn];

void find_block(int a,int &p,int &h)//找塊的位置

print();

return 0;

}

演算法競賽入門(1)STL初步

sort 如希望使用sort排序,應該將待排列型別定義 小於 運算子 或在排序時插入乙個 小於 函式。排序物件可以放在普通陣列中,也可以放在vector中。前者用sort a,a n 後者用sort v.begin v.end 呼叫。lower bound作用是查詢 大於等於x的第乙個位置 示例 i...

演算法競賽入門經典筆記(二) STL

演算法競賽入門經典 第2版 學習筆記 1.排序 include include using namespace std int main sort a,a n 排序 int p lower bound a,a 10,2 在已排序的陣列中找2的位置sort可以對任意物件進行排序,可以使用sort a,...

演算法競賽入門經典 C 入門

include 提供輸入輸出流 include 提供常用演算法 using namespace std const int maxn 100 10 int a maxn int main return0 宣告陣列時,陣列大小可以用const宣告的常數。在c 中,這樣的寫法更加推薦。include i...