士兵隊列訓練問題(鏈表)


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

介紹list實戰經驗

List容器是一種實現了雙向鏈表的數據結構,它的每個節點都含有前驅元素指針域、數據域、後繼元素指針域。不同於數組這樣的線性表,由於list元素的前驅和後繼都是靠指針來鏈接,因此在鏈表的任意位置進行元素的插入、刪除和查找操作速度是較快的。由於list對象的結點並不要求在一段連續的內存中,所以對於迭代器,只能通過“++”或者“–”的操作,不能對其進行+N或者-N的操作。

一)創建list對象:

(1)創建沒有任何元素的list對象:list l;

(2)創建具有n個元素的list對象:list l(10); //創建具有10個整形元素的list對象l 。

(二)插入元素:

(1)使用push_back()方法從尾部插入元素,鏈表自動擴張;

(2)使用push_front()方法從頭部插入元素,鏈表自動擴張;

(3)使用insert()方法向迭代器位置插入新元素,鏈表自動擴張。

(三)遍歷元素:

(1)前向遍歷:以前向迭代器的方式遍歷;

(2)反向遍歷:使用反向迭代器進行遍歷。

(四)刪除元素:

(1)remove(元素值),刪除鏈表中的元素,值相同的元素都會被刪除;

(2)pop_front()刪除鏈表首元素;

(3)pop_back()刪除鏈表尾元素;

(4)erase()刪除迭代器位置的元素;

(5)clear()清空鏈表容器。

(五)查找元素:需要添加#include

find(迭代器1,迭代器2,元素),函數返回一個迭代器值,若該值被找到則返回該值所在的迭代器值,若沒有找到則返回end()迭代器的位置。

(六)list排序:

使用list的成員函數sort()實現升序排序

#include<iostream>  
#include<list>  
#include<algorithm>  
using namespace std;  
  
int main()  
{  
	int i;
	list<int> l;
	list<int>::iterator pos;
	list<int>::reverse_iterator pos1;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
 
	//前向遍歷
	for(pos=l.begin();pos!=l.end();pos++)
		cout<<*pos<<" ";
	cout<<endl;
    
	//反向遍歷
	for(pos1=l.rbegin();pos1!=l.rend();pos1++)
		cout<<*pos1<<" ";
	cout<<endl;
 
    return 0;  
} 
輸出結果:
1 2 3 4
4 3 2 1

lst1.assign() // 給list賦值
lst1.front() // 返回第一個元素
lst1.back() // 返回最後一個元素
lst1.begin() // 返回指向第一個元素的迭代器
lst1.end() // 返回末尾的迭代器
lst1.insert() // 插入一個元素到list中
lst1.erase() // 刪除一個元素
lst1.pop_back() // 刪除最有一個元素
lst1.pop_front() // 刪除第一個元素
lst1.clear() // 刪除所有元素
lst1.remove(const T & val) // 刪除和val相等的元素
lst1.push_back() // 在list的末尾添加一個元素
lst1.push_front() // 在list的首部添加一個元素
lst1.empty() // 判斷,若list爲空返回true
lst1.max_size() // 返回list能容納的最大元素數量
lst1.sort() // 給list排序(順序)
list.reverse() // 把list中的元素倒轉
lst1.merge(lst2) // 合併lst2到lst1,並清空lst2
lst1.unique() // 刪除所有和前一個元素相等的元素
void splice(iterator i, list & x, iterator first, iterator last) // 在位置i前面插入鏈表x中的區間 [first, last), 並在鏈表x中刪除該區間(鏈表自身和鏈表x可以是用一個鏈表,只要i不在 [first, last) 中即可

#include <list>  // 使用 list 需要包含此頭文件
#include <algorithm>  // 使用 STL 中的算法需要包含此頭文件
#include <iostream>  
using namespace std;

class A
{
public:
	A(int n_):n(n_){}
	friend bool operator < (const A & a1, const A & a2);
	friend bool operator == (const A & a1, const A & a2);
	friend ostream & operator << (ostream & out, const A & a);
private:
	int n;
};

bool operator < (const A & a1, const A & a2){
	return a1.n < a2.n;
}

bool operator == (const A & a1, const A & a2){
	return a1.n == a2.n;
}

ostream & operator << (ostream & out, const A & a){
	out << a.n;
	return out;
}
template <class T>
void Print(T first, T last)
{
	for(; first != last; ++first)
		cout<<*first<<" ";
	cout<<endl;
}

int main()
{
	A a[5] = {1, 3, 2, 4, 2};
	A b[7] = {10, 30, 20, 30, 30, 40, 40};
	list<A> lst1(a, a+5), lst2(b, b+7);
	lst1.sort();    // 順序排序
	cout<<"1. "; Print(lst1.begin(), lst1.end());
	lst1.remove(2);  // 刪除所有和A(2)相等的元素
	cout<<"2. "; Print(lst1.begin(), lst1.end());
	lst2.pop_front();  // 刪除第一個元素
	cout<<"3. "; Print(lst2.begin(), lst2.end());
	lst2.unique();  // 刪除所有和前一個元素相等的元素
	cout<<"4. "; Print(lst2.begin(), lst2.end());
	lst2.sort();  // 順序排序
	lst1.merge(lst2);  // 合併 lst2 到 lst1 並清空 lst2
	cout<<"5. "; Print(lst1.begin(), lst1.end());
	cout<<"6. "; Print(lst2.begin(), lst2.end());  // lst2 是空的
	lst1.reverse();  // 將 lst1 倒置

	cout<<"7. "; Print(lst1.begin(), lst1.end());
	lst2.insert(lst2.begin(), a + 1, a + 4);  // 在 lst2 中插入 3,2,4 三個元素
	list<A>::iterator p1, p2, p3;
	p1 = find(lst1.begin(), lst1.end(), 30);  // 查找元素
	p2 = find(lst2.begin(), lst2.end(), 2);
	p3 = find(lst2.begin(), lst2.end(), 4);
	lst1.splice(p1, lst2, p2, p3);  // 將 [p2, p3) 插入p1之前,並從lst2中刪除 [p2, p3)
	cout<<"8. "; Print(lst1.begin(), lst1.end());
	cout<<"9. "; Print(lst2.begin(), lst2.end());
	return 0;  
}

題目:

Total Submission(s): 20237 Accepted Submission(s): 8517

Problem Description
某部隊進行新兵隊列訓練,將新兵從一開始按順序依次編號,並排成一行橫隊,訓練的規則如下:從頭開始一至二報數,凡報到二的出列,剩下的向小序號方向靠攏,再從頭開始進行一至三報數,凡報到三的出列,剩下的向小序號方向靠攏,繼續從頭開始進行一至二報數。。。,以後從頭開始輪流進行一至二報數、一至三報數直到剩下的人數不超過三人爲止。

Input
本題有多個測試數據組,第一行爲組數N,接着爲N行新兵人數,新兵人數不超過5000。

Output
共有N行,分別對應輸入的新兵人數,每行輸出剩下的新兵最初的編號,編號之間有一個空格。

Sample Input
2
20
40

Sample Output
1 7 19
1 19 37

我的代碼:

這段代碼是個痛苦的故事,我的賦值語句竟然寫成了 == 怎麼找怎麼想邏輯上都好久找不到爲啥出不來循環。
簡單介紹一下代碼:
定義迭代器:list::iterator it;
題目要求最後留下的不多於3人,我就把while條件寫作mylist.size()>3則每輪交替刪掉2的倍數或3的倍數,遍歷時如果不是其倍數,則保留它在列表裏,不做任何操作,it++繼續遍歷下一個;如果是它的倍數,則刪掉它it = mylist.erase(it);,而且刪掉後我們知道it位置不變,刪除一個元素後,重新指向的是相對原來的下一個節點的數據。不管it動不動,我們定義的num(暫存當前人的編碼)總是不停的+1,所以實際上我們起到遍歷作用的是這裏的num。it起到的作用是動態處理列表中的元素。
每輪(每行人結束後)替換2或3是通過k==2?k=3:k=2。

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int k = 2;
        list < int > mylist;  //定義
        list < int >::iterator  it;
        for(int i=1; i<=n; i++)
            mylist.push_back(i);  //賦值
        while(mylist.size() > 3)
        {
            int num = 1;
            for(it = mylist.begin(); it != mylist.end();)
            {
                if(num++ % k == 0)
                    it = mylist.erase(it);
                else
                    it++;
            }
            k == 2 ? k = 3:k = 2;
        }
        for(it = mylist.begin(); it != mylist.end(); it++)
        {
            if(it != mylist.begin())  //輸出數據間的空格
                cout<<" ";
            cout<<*it;  //輸出it所指向的數據
        }
        cout<<endl;
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章