摘要: 1.掌握@property方法的四類參數設置,現在重點掌握與setter方法內存管理相關的參數。 閱讀全文
置頂隨筆 #
摘要: 掌握自定義構造方法需要注意的規范。 閱讀全文
摘要: 1.總結了@property與@synthesize和最新的@property新特性。 閱讀全文
鐵軌
PopPush城市有一座著名的火車站。這個國家到處都是丘陵。而這個火車站是建于上一個世紀。不幸的是,那時的資金有限。所以只能建立起一條路面鐵軌。而且,這導致這個火車站在同一個時刻只能一個軌道投入使用,因為它缺少空間,兩列火車將無路可走。具體看下圖。
當地的慣例是每一列火車從A方向駛向B方向時候,會用某種方式將車廂重組。假設火車將要到達A方向,擁有N個車廂(N<=1000),這些車廂按照遞增順序標記為1到N。負責從組車廂的領導,必須知道是否能從組車廂讓它駛出B,而這個重組的序列就是a1\a2\a3...aN.幫組他并且寫一個程序來判斷是否可能按照所要求的車廂順序。你可以假設,單個的車廂可以從列車上分離出來,在他們進入站臺之前。并且他們可以自由移動,知道它們上了B軌道。你也可以假設在任意時候站臺可以放下無數的車廂。但是只要一個車廂進入站臺,它就不能返回A軌道,同時如果它離開了站臺駛向B軌道,它就不能返回站臺。

輸入:
這個輸入文件由多個行塊組成。每一個塊描述的是多個要求的重組車廂的序列。在這每個塊中的第一行是一個整數N,被用來說明上面每行的車廂個數。這個快的最后一行僅僅是一個數字0要來標記該快的結束
最后一個塊僅僅是一個0獨占一行。
輸出:
這個輸出文件包含多行,這些行和排列車廂的行數一一對應。日過該排列可行,則輸出Yes,否則輸出No。另外存在一個空行在每個相對應的塊后面。輸出文件中不存在于最后一個什么數據都沒有的響應輸出。
輸出
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Output
Yes
No
Yes
1 /*
2 由于station符合后進先出規則,所以可以用一個stack<int>來表示station。
3 然后采用模擬的規則來寫,因為進入station是按照編號遞增進入,所以可以用aId變量表示。
4 接下來就是模擬時候應該注意的條件,我們知道有以下兩種情況,一種是一進來station,就出station
5 也就是 aId == coaches[lenB]; 一種是進來時還不出站,那么這時候就要s.push(aId),但是這一種的條件呢?
6 我們如果可以排除掉第一種,那么無非就是第二種了。但是我們要知道,在進站之前,如果站臺里有車它是可以先出站的
7 ,這種狀態就條件就是 !s.empty() && s.top() == coaches[lenB].所以只要按照順序判斷這幾個條件
8 就可以寫出這個模擬程序了。
9
10
11 總結:
12 解決問題的關鍵點,在于構建問題的模型、大部分都是可以用現有的基礎數據結構。構造完對應的數據結構,特別是對于模擬題
13 無非就是狀態見的轉移處理,這是就要在基于模型的基礎上、先寫出映射到個個狀態的唯一條件,然后按照問題邏輯一一先后判斷
14 即可。
15 */
16 #include <iostream>
17 #include <stack>
18
19 using namespace std;
20 const int len = 1024;
21 int coaches[len];
22
23 int main() {
24
25 int n;
26 while (cin >> n, n) {
27 stack<int> s;
28 // read the required permutaion
29
30 while ( cin >> coaches[0], coaches[0]) {
31
32 for (int i = 1; i < n; i++) {
33 cin >> coaches[i];
34 }
35 int lenB = 0, aId = 1;
36 bool ok = true;
37 while (lenB < n) {
38
39 if (aId == coaches[lenB]) { aId++; lenB++; }
40 else if(!s.empty() && s.top() == coaches[lenB]) { s.pop(); lenB++; }
41 else if(aId <= n) s.push(aId++);
42 else { ok = false; break; }
43
44 }
45
46 cout << (ok ? "Yes" : "No") << endl;
47 }
48 cout << endl;
49 }
50
51 return 0;
52 }
2015/3/30下午3:03:522 由于station符合后進先出規則,所以可以用一個stack<int>來表示station。
3 然后采用模擬的規則來寫,因為進入station是按照編號遞增進入,所以可以用aId變量表示。
4 接下來就是模擬時候應該注意的條件,我們知道有以下兩種情況,一種是一進來station,就出station
5 也就是 aId == coaches[lenB]; 一種是進來時還不出站,那么這時候就要s.push(aId),但是這一種的條件呢?
6 我們如果可以排除掉第一種,那么無非就是第二種了。但是我們要知道,在進站之前,如果站臺里有車它是可以先出站的
7 ,這種狀態就條件就是 !s.empty() && s.top() == coaches[lenB].所以只要按照順序判斷這幾個條件
8 就可以寫出這個模擬程序了。
9
10
11 總結:
12 解決問題的關鍵點,在于構建問題的模型、大部分都是可以用現有的基礎數據結構。構造完對應的數據結構,特別是對于模擬題
13 無非就是狀態見的轉移處理,這是就要在基于模型的基礎上、先寫出映射到個個狀態的唯一條件,然后按照問題邏輯一一先后判斷
14 即可。
15 */
16 #include <iostream>
17 #include <stack>
18
19 using namespace std;
20 const int len = 1024;
21 int coaches[len];
22
23 int main() {
24
25 int n;
26 while (cin >> n, n) {
27 stack<int> s;
28 // read the required permutaion
29
30 while ( cin >> coaches[0], coaches[0]) {
31
32 for (int i = 1; i < n; i++) {
33 cin >> coaches[i];
34 }
35 int lenB = 0, aId = 1;
36 bool ok = true;
37 while (lenB < n) {
38
39 if (aId == coaches[lenB]) { aId++; lenB++; }
40 else if(!s.empty() && s.top() == coaches[lenB]) { s.pop(); lenB++; }
41 else if(aId <= n) s.push(aId++);
42 else { ok = false; break; }
43
44 }
45
46 cout << (ok ? "Yes" : "No") << endl;
47 }
48 cout << endl;
49 }
50
51 return 0;
52 }
這是一道很好的模擬題,用vector<int> p[maxn],建立模型,映射為maxn個堆。主要要掌握vector模擬堆操作的簡單方法。
接下來得思路是自頂向下的方式,逐步完善程序。首先根據提議列出下表。
1.move a onto b
clear_above(a) && clear_above(b);
insert a above b;
2.move a over b
clear(a)
insert a above bs
3.pile a onto b
clear(b)
insert as above b
4.pile a over b
insert as above bs
觀察可以提取出move的話必有clear_above(a)、onto必有clear_above(b).
而insert 動作不管b是在p[b]的頂部還是外部。都是p[b].push_back(a或a以上的木塊)
所以可以抽取成pile_into(pa, ha, pb);
考慮完這些,開始寫框架。如下
接下來,完成程序其它部分,按照框架的意思,逐步完善。如下
接下來得思路是自頂向下的方式,逐步完善程序。首先根據提議列出下表。
1.move a onto b
clear_above(a) && clear_above(b);
insert a above b;
2.move a over b
clear(a)
insert a above bs
3.pile a onto b
clear(b)
insert as above b
4.pile a over b
insert as above bs
觀察可以提取出move的話必有clear_above(a)、onto必有clear_above(b).
而insert 動作不管b是在p[b]的頂部還是外部。都是p[b].push_back(a或a以上的木塊)
所以可以抽取成pile_into(pa, ha, pb);
考慮完這些,開始寫框架。如下
1
2 #include <cstdio>
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 using namespace std;
7
8 const int maxn = 64;
9 int n;
10 vector<int> a[maxn];
11 int find_block(int a, int& h);
12 void clear_above(int p, int h);
13 void pile_into(int pa, int ha, int pb);
14 void print();
15 int main() {
16
17
18
19 string s1, s2;
20 scanf("%d", &n);
21 for (int i = 0; i < n ; i++) {
22 a[i].push_back(i);
23 }
24
25 while (cin >> s1, s1 != "quit") {
26 int ba,bb;
27 cin >> ba >> s2 >> bb;
28 int ha = 0,hb = 0;
29 int pa = find_block(ba, ha);
30 int pb = find_block(bb, hb);
31 if (pa == pb) continue;
32
33 if (s1 == "move") clear_above(pa, ha);
34 if (s2 == "onto") clear_above(pb, hb);
35 pile_into(pa, ha, pb);
36 }
37
38 print();
39 return 0;
40 }
2 #include <cstdio>
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 using namespace std;
7
8 const int maxn = 64;
9 int n;
10 vector<int> a[maxn];
11 int find_block(int a, int& h);
12 void clear_above(int p, int h);
13 void pile_into(int pa, int ha, int pb);
14 void print();
15 int main() {
16
17
18
19 string s1, s2;
20 scanf("%d", &n);
21 for (int i = 0; i < n ; i++) {
22 a[i].push_back(i);
23 }
24
25 while (cin >> s1, s1 != "quit") {
26 int ba,bb;
27 cin >> ba >> s2 >> bb;
28 int ha = 0,hb = 0;
29 int pa = find_block(ba, ha);
30 int pb = find_block(bb, hb);
31 if (pa == pb) continue;
32
33 if (s1 == "move") clear_above(pa, ha);
34 if (s2 == "onto") clear_above(pb, hb);
35 pile_into(pa, ha, pb);
36 }
37
38 print();
39 return 0;
40 }
接下來,完成程序其它部分,按照框架的意思,逐步完善。如下
1 void print() {
2
3 for (int i = 0; i < n; i++) {
4 printf("%d:",i);
5 for (int h = 0; h < a[i].size(); h++) {
6 printf(" %d", a[i][h]);
7 }
8 printf("\n");
9 }
10 }
11
12 int find_block(int ba, int& h) {
13
14 for (int i = 0; i < n; i++) {
15
16 int vec_size = a[i].size();
17 for (h = 0; h < vec_size; h++) {
18 if (ba == a[i][h]) return i;
19 }
20
21 }
22 return -1;
23 }
24
25 void clear_above(int p, int h) {
26 int vec_size = a[p].size();
27 for (int i = h + 1; i < vec_size; i++) {
28
29 int b = a[p][i];
30 a[b].push_back(b);
31
32 }
33 a[p].resize(h + 1);
34 }
35
36 void pile_into(int pa, int ha, int pb) {
37
38 int vec_size = a[pa].size();
39
40 for (int i = ha; i < vec_size; i++) {
41 a[pb].push_back(a[pa][i]);
42 }
43 a[pa].resize(ha);
44 }
2
3 for (int i = 0; i < n; i++) {
4 printf("%d:",i);
5 for (int h = 0; h < a[i].size(); h++) {
6 printf(" %d", a[i][h]);
7 }
8 printf("\n");
9 }
10 }
11
12 int find_block(int ba, int& h) {
13
14 for (int i = 0; i < n; i++) {
15
16 int vec_size = a[i].size();
17 for (h = 0; h < vec_size; h++) {
18 if (ba == a[i][h]) return i;
19 }
20
21 }
22 return -1;
23 }
24
25 void clear_above(int p, int h) {
26 int vec_size = a[p].size();
27 for (int i = h + 1; i < vec_size; i++) {
28
29 int b = a[p][i];
30 a[b].push_back(b);
31
32 }
33 a[p].resize(h + 1);
34 }
35
36 void pile_into(int pa, int ha, int pb) {
37
38 int vec_size = a[pa].size();
39
40 for (int i = ha; i < vec_size; i++) {
41 a[pb].push_back(a[pa][i]);
42 }
43 a[pa].resize(ha);
44 }
1 #include <stdio.h>
2 #include <string.h>
3
4
5 int readChar();
6 int readInt(int c);
7 int readCodes();
8
9 /*
10 1.讀取字符時候如何過濾掉換行符號,兼容類unix和windows操作系統 readChar()
11 2.熟練掌握將二進制字符串映射到數組的數據結構,這個數據結構是個二元組,我們可以用該二元組唯一確定一個長度為len的二進制字符串
12 (len, value) ---- > code[len][value]
13
14 */
15
16 int main() {
17
18
19 while ( readCodes() ) { // 讀編碼頭
20
21 for (;;) { // 讀信息
22
23 int len = readInt(3);
24 if (0 == len) break; // 全0該信息結束
25
26 for (;;) { // 讀信息段
27
28 int v = readInt(len);
29
30 if (v == (1 << len) - 1) break; // 全1一個信息段結束
31 putchar(code[len][v]);
32
33 }
34
35 }
36
37 putchar('\n');
38 }
39 }
40
41
42 int readInt(int l) {
43
44 int v = 0;
45 //已經讀取了3 - l個字符
46 while (l--) {
47
48 v = v * 2 + readChar() - '0';
49
50 }
51 return v;
52
53 }
54
55
56 char readChar() {
57
58 char ch;
59 do {
60
61 ch = getchar();
62
63 }while ( '\n' != ch || '\r' != ch )
64 return ch;
65 }
66
67
68 int readCodes() {
69
70
71 memset(code, 0, sizeof(code));
72
73 // 因為可能讀取編碼頭獨占一行,所以我們可能讀取編碼頭時候會讀取到上一次的回車換行,所以要使用readChar函數
74 code[1][0] = readChar();
75
76 for (int len = 2; len < 8; len++) {
77
78 for (int v = 0; v < (1 << len) - 1; v++) {
79
80 char ch = getchar();
81 if ( EOF == ch ) return 0;
82 if ( '\n' == ch || '\r' == ch) return 1;
83 code[len][v] = ch;
84
85 }
86
87 }
88 return 1;
89
90
91 }
2 #include <string.h>
3
4
5 int readChar();
6 int readInt(int c);
7 int readCodes();
8
9 /*
10 1.讀取字符時候如何過濾掉換行符號,兼容類unix和windows操作系統 readChar()
11 2.熟練掌握將二進制字符串映射到數組的數據結構,這個數據結構是個二元組,我們可以用該二元組唯一確定一個長度為len的二進制字符串
12 (len, value) ---- > code[len][value]
13
14 */
15
16 int main() {
17
18
19 while ( readCodes() ) { // 讀編碼頭
20
21 for (;;) { // 讀信息
22
23 int len = readInt(3);
24 if (0 == len) break; // 全0該信息結束
25
26 for (;;) { // 讀信息段
27
28 int v = readInt(len);
29
30 if (v == (1 << len) - 1) break; // 全1一個信息段結束
31 putchar(code[len][v]);
32
33 }
34
35 }
36
37 putchar('\n');
38 }
39 }
40
41
42 int readInt(int l) {
43
44 int v = 0;
45 //已經讀取了3 - l個字符
46 while (l--) {
47
48 v = v * 2 + readChar() - '0';
49
50 }
51 return v;
52
53 }
54
55
56 char readChar() {
57
58 char ch;
59 do {
60
61 ch = getchar();
62
63 }while ( '\n' != ch || '\r' != ch )
64 return ch;
65 }
66
67
68 int readCodes() {
69
70
71 memset(code, 0, sizeof(code));
72
73 // 因為可能讀取編碼頭獨占一行,所以我們可能讀取編碼頭時候會讀取到上一次的回車換行,所以要使用readChar函數
74 code[1][0] = readChar();
75
76 for (int len = 2; len < 8; len++) {
77
78 for (int v = 0; v < (1 << len) - 1; v++) {
79
80 char ch = getchar();
81 if ( EOF == ch ) return 0;
82 if ( '\n' == ch || '\r' == ch) return 1;
83 code[len][v] = ch;
84
85 }
86
87 }
88 return 1;
89
90
91 }
2015年5月25日 #
摘要: 1.解決數字鍵盤不出現問題 閱讀全文
2015年5月15日 #
摘要: 1.NSDate與NSDateFormatter的創建 2.日期轉為字符串、字符串轉日期的方式 3.日期格式化的參數 閱讀全文
摘要: 1.NSNumber、NSValue分別用于包裝什么 2.如何解包裝和注意事項 3.NSNumber與NSValue的關系 閱讀全文
2015年5月14日 #
摘要: 1.NSString的創建 2.NSString的讀與寫 3.NSMutableString的創建 4.NSMutable與NSString的區別 閱讀全文
2015年5月13日 #
摘要: 1.各結構體的含義(四個) 2.結構體的常用創建方式 3.快速打印結構體的方法 4.CGFloat的本質 閱讀全文
摘要: 1.代理設的概念 2.代理設計模式的使用場合 3.代理設計模式的實現 閱讀全文
摘要: 1.protocol的作用 2.protocol相關語法 3.基協議 4.限制對象類型 5.協議前置聲明 6.協議也能遵守協議 閱讀全文
2015年5月12日 #
摘要: 1.全面總結循環引用的解決方案 2.如何將工程轉換成ARC項目。 3.如何讓ARC與非ARC混用 閱讀全文
摘要: 1.ARC釋放對象的判斷準則 2.ARC的4個特點 3.強/弱指針的概念 4.常見錯誤 閱讀全文