|
很簡短,有點trick的感覺,就像小學里寫的那些(n-1) mod x + 1。進了大學后很少寫類似風格的代碼了,這些用在java項目中就要因為可讀性被bs了。 1. 計算串長度 strlen(a) for (i = 0; a[i] != 0; i++); return i;
2. 復制 strcpy(a, b) for (i = 0; (a[i] = b[i]) != 0; i++);
3. 比較 strcmp(a, b) for (i = 0; a[i] == b[i]; i++) if (a[i] == 0) return 0; return a[i] - b[i]; 注意適用于不同長度的字符串
指針版本 1. strlen(a) b = a; while (*b++); return b - a - 1;
2. strcpy(a, b) while (*a++ = *b++);
3. strcmp(a, b) while (*a++ = *b++) if (*(a-1) == 0) return 0; return *(a-1) - *(b-1);
wrong answer了幾次,總算過了 gdb還是不怎么會用,調試dfs時不知道怎么跳出當前層(finish和up貌似都不管用),以及如何step over
/* PROG: lamps ID: 06301031 LANG: C++ */
#include <iostream> #include <fstream> #include <vector> #include <algorithm>
using namespace std;
struct StateArray { bool value[100]; };
int n, c; int methodCount = 0; vector<int> reqOn, reqOff; vector<StateArray> result; int method[4]; bool state[100]; // On - true, Off - false
bool equalState(const StateArray& s1, const StateArray& s2) { for (int i = 0; i < n; i++) if (s1.value[i] != s2.value[i]) return false; return true; }
bool compareState(const StateArray& s1, const StateArray& s2) { for (int i = 0; i < n; i++) if (s1.value[i] != s2.value[i]) return !s1.value[i]; return false; }
void changeState(int m) // m[0,4] refers to the method { switch (m) { case 0: for (int i = 0; i < n; i++) state[i] = !state[i]; break; case 1: for (int i = 0; i < n; i += 2) state[i] = !state[i]; break; case 2: for (int i = 1; i < n; i += 2) state[i] = !state[i]; break; case 3: for (int i = 0; i < n; i += 3) state[i] = !state[i]; break; } }
bool validState() { for (int i = 0; i < reqOn.size(); i++) if (!state[reqOn[i]]) return false; for (int i = 0; i < reqOff.size(); i++) if (state[reqOff[i]]) return false; return true; }
void DFS(int t) // t: method number { if ((methodCount > c) || (t >= 4)) return; for (int i = 0; i < 2; i++) { if (i == 1) { methodCount++; changeState(t); if (((c - methodCount) % 2 == 0) && validState()) { StateArray tempState; for (int j = 0; j < n; j++) { tempState.value[j] = state[j]; } result.push_back(tempState); } DFS(t + 1); methodCount--; changeState(t); } else { if (((c - methodCount) % 2 == 0) && validState()) { StateArray tempState; for (int j = 0; j < n; j++) { tempState.value[j] = state[j]; } result.push_back(tempState); } DFS(t + 1); } } }
int main() { ifstream fin("lamps.in"); ofstream fout("lamps.out"); fin >> n >> c; int x; while (fin >> x) if (x == -1) break; else reqOn.push_back(x - 1); while (fin >> x) if (x == -1) break; else reqOff.push_back(x - 1); for (int i = 0; i < n; i++) state[i] = true; DFS(0); if (result.size() < 1) fout << "IMPOSSIBLE\n"; sort(result.begin(), result.end(), compareState); vector<StateArray>::iterator iter; vector<StateArray> uni; for (iter = result.begin(); iter != result.end(); iter++) if (uni.empty() || !equalState(uni[uni.size() - 1], *iter)) uni.push_back(*iter); for (int i = 0; i < uni.size(); i++) { for (int j = 0; j < n; j++) { fout << uni[i].value[j]; } fout << endl; } return 0; }
Wrong Answer了半天發現原來是m - 1打成m + 1了。。。
/* PROG: castle ID: 06301031 LANG: C++ */
#include <iostream> #include <fstream>
using namespace std;
enum Direction {WEST = 0, NORTH = 1, EAST = 2, SOUTH = 3};
// The directions corresponding to the following steps are // West(0), North(1), East(2) and South(3). const int dx[] = {0, -1, 0, 1}; const int dy[] = {-1, 0, 1, 0};
int region[50][50]; int colors[50 * 50]; int maze[50][50];
bool canGo(int value, int dirx) { int base = 1; for (int i = 0; i < dirx; i++) base *= 2; return ((base & value) == 0); }
void floodfill(int x, int y, int color) { if (region[x][y] < 0) { region[x][y] = color; colors[color]++; } //cout << "now paint " << x << ',' << y << endl; for (int i = 0; i < 4; i++) // dirction { if (canGo(maze[x][y], i) && region[x + dx[i]][y + dy[i]] < 0) { floodfill(x + dx[i], y + dy[i], color); } } }
int main() { ifstream fin("castle.in"); ofstream fout("castle.out"); int m, n; fin >> n >> m; for (int i = 0; i < m; i++) for (int j= 0; j < n; j++) { fin >> maze[i][j]; region[i][j] = -1; }
int colorCount = 0; int maxCount = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (region[i][j] < 0) { colors[colorCount] = 0; floodfill(i, j, colorCount); if (maxCount < colors[colorCount]) maxCount = colors[colorCount]; // for (int ii = 0; ii < m; ii++) { // for (int jj = 0; jj < n; jj++) // cout << (region[ii][jj] >= 0 ? region[ii][jj] : ' '); // cout << endl; // } // cout << i << ',' << j << ':' << colorCount << ' ' << colors[colorCount] << endl; colorCount++; } fout << colorCount << endl; fout << maxCount << endl; int maxCombine = 0, maxD, maxI, maxJ; for (int j = 0; j < n; j++) for (int i = m - 1; i >= 0; i--) for (int d = 1; d < 3; d++) { int nx = i + dx[d]; int ny = j + dy[d]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue; if (region[nx][ny] != region[i][j]) { if (maxCombine < colors[region[nx][ny]] + colors[region[i][j]]) { maxCombine = colors[region[nx][ny]] + colors[region[i][j]]; maxD = d; maxI = i; maxJ = j; } } } fout << maxCombine << endl; fout << maxI + 1 << ' ' << maxJ + 1 << ' '; switch (maxD) { case 0: fout << 'W' << endl; break; case 1: fout << 'N' << endl; break; case 2: fout << 'E' << endl; break; case 3: fout << 'S' << endl; break; } return 0; }
在水源上看到的,大概的做法是 求前n項和序列S1, S2, ..., Sn,問題即轉化為求i, j,使得Si - Sj = X 把{Sk}和{Sk + X}序列中的數都放入hash表中,查找沖突項。
BBS上peter大牛的問題:
char *s = "string1"; strcpy(s, "string2"); 這樣為什么會segmentation fault?
后面的解答:
char *s="string1" //此時"string1"在常量區 s是指向常量區的一個指針 你不能對 常量區的內容進行修改
char s[]="string2" //此時"string2" 在棧區 此時可以對里面的內容進行修改
所以你可以寫成
char s[]="string1"; strcpy(s,"string2");
http://www.aurora-x.net/blog/oasis/?p=92 by opengl@rygh
現在市面上系統的,由淺及深的講Ruby的書鳳毛麟角,這本是分量最重的一本。原書第二版出版到現在也過去兩年了,國內剛剛在這個月由博文引進電工發行了譯本。
800+頁的大部頭,一半是核心庫和標準庫的參考,另外一半分為三個部分——基礎、環境、高級。這其中我覺得比較有價值的部分在于“高級這一塊”有助于讓你從一個更高的層次來理解和掌握這門語言,這一部分也是需要經常參考的部分。
就國內目前引進的唯一一本算是講Ruby的書,要從這本書開始學習Ruby估計會嚇跑不少潛在用戶。作者是大牛沒錯,不過教學則是另一回事了(大家都應有體會,本科上課的時候課講的最好的老師通常都不是學術最牛的老師)。缺點有兩處很明顯:
第一是自頂向下,它的順序是這樣的:
對象和類–>容器、集合–>標準類型–>表達式–>異常和模塊–>基本輸入輸出
一個從上降到低又陡然上升的過程。作者自己也說在第一版里這樣的效果并不好,因此在第二版里特意增加了一個介紹性章節,粗略的先把所有東西列給讀者看一遍。雖然起到些作用,但是實際效果我想對于初學者來說仍然不會太好。C++/Java的書我都讀過不少,也沒見哪個是一上來就把Class/Object這些東西甩給讀者的,總是從基本類型、控制語句過渡。除非你已經對OO這套相當熟悉了,否則上來這個門檻就能把一堆新手擋在門外。
接下來的,談不上十分晦澀,但也不是什么讀來輕快的內容,關鍵是作者給的例子較少,使得象塊、迭代這些特色難以掌握。再有一點,ruby的語法風格有相當部分還保留有Perl的痕跡,而Perl是出了名的以奇怪符號著稱于世,這些符號在新手眼里不外天書,而高手們則愛不釋手。
所以,要能比較順利的通過這本書的入門之路,你得事先具有OO的基礎,至少一門腳本語言的經驗(Perl最佳),函數式編程的一些概念(否則當你看到塊的一些用法時會很迷惑)。然后,可以用Ruby笨拙的寫一些小程序了。
Ruby作為動態語言之一,它的最大特點自然是“動態”兩個字,其著名的“duck typing”就是一大體現(在我看來,就像是基于接口的調用,但卻并不用一個給定的接口去事先限制)。這些在高級部分里都有專門講述,是應該重點學習的部分。
那么入門究竟用什么書更好?我推薦《Everyday Scripting with Ruby》這本。和《Programming Ruby》同一個出版社,今年一月份剛出了原版。它是以相當循序漸進的方式帶領讀者進入Ruby的世界,尤其是作者精心設計的幾個Project是全書亮點(學習一門語言最好的方式還是要動手寫程序)。
最后總結如下,首先看《Everyday》這本書,跟著書中的指導擺弄過所有的Project,并完成相應的練習。然后再看《Programmin》一書的Crystallized部分。再之后就主要是當作API參考手冊了。花錢去買中文版我個人覺得不是很有必要,總共四部分中,前兩部分我覺得蠻雞肋的,第四部分在電腦上查更方便,只有第三部分有較高的價值。
1. Array#reject 方法遍歷一個集合的每個元素,并把符合條件的元素刪去。 例如去掉數組中的所有素數 nums = nums.reject do | num | prime?(num) end puts nums
2. String#chomp 方法 str.chomp(separator=$/) => new_str Returns a new +String+ with the given record separator removed from the end of _str_ (if present). If +$/+ has not been changed from the default Ruby record separator, then +chomp+ also removes carriage return characters (that is it will remove +\n+, +\r+, and +\r\n+).
"hello".chomp #=> "hello" "hello\n".chomp #=> "hello" "hello\r\n".chomp #=> "hello" "hello\n\r".chomp #=> "hello\n" "hello\r".chomp #=> "hello" "hello \n there".chomp #=> "hello \n there" "hello".chomp("llo") #=> "he"
3. 判斷是否在命令行運行腳本 if $0 == __FILE__ check_usage compare_inventory_files(ARGV[0], ARGV[1]) end 類似于Java類的main方法,在被其他類導入時不會運行其中的代碼。
4. Enumerable#any? 方法查找一個集合中是否有滿足條件的元素 irb(main):004:0> deposits = [1, 0, 10000] irb(main):005:0> deposits.any? do | deposit | irb(main):006:1* deposit > 9999 irb(main):007:1> end => true
5. 關于測試 這本書(Everyday Scripting with Ruby)的很多程序都是依循測試驅動開發的思想寫出來的,測試單元中的方法通常有兩種目的。 一種是direct test,需要測試那個函數就直接調用那個函數,傳遞的參數都是直接寫出來的。 另一種是bootstrapping test,被測試函數的參數也是通過生成這些參數的函數生成的,即一個方法測試了多個對象。 Everyone finds their own balance between testing directly and testing indirectly. You will too.
6. Time#strftime 方法 t = Time.now t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003" t.strftime("at %I:%M%p") #=> "at 08:56AM"
先把各種熱門的東西都走馬觀花地看一遍,呵呵。 看的是Everyday Scripting with Ruby,風格和In Action系列差不多,大量的實例。 現在學Ruby的主要目的也是everyday scripting,方便數據處理、生成,文件批處理等,RoR之類的暫時不考慮。
1. String.inspect 方法 文檔中的說法是 str.inspect => string
Returns a printable version of _str_, with special characters escaped.
str = "hello" str[3] = 8 str.inspect #=> "hel\010o"
具體情況試試 myString.inspect.inspect....就能了解一點了
2. Arrays.each 和 Arrays.collect for_each方法
irb(main):007:0> [1, 2, 3].each do | element | irb(main):008:1* puts element irb(main):009:1> end 1 2 3 => [1, 2, 3]
后者與前者的不同之處在于,在處理數據的同時,每次處理的返回結果都會保存到一個新的數組中返回。 irb(main):036:0> newarray = ["aBC", "B"].collect do |e| irb(main):037:1* e.downcase irb(main):038:1> end => ["abc", "b"]
3. Messages and Methods It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it. 呃,還是沒有感性認識。
4. Delimiting Blocks 塊的兩種表示方式: array.each do | element | puts element end array.each { | element | puts element } 通常使用第一種,但可以用一行寫成的情況也可以使用第二種: array.each { | element | puts element }
摘要: For using taglist plugin,you must install ctags plugin first.1.ctags
(1)到http://ctags.sourceforge.net/下載ctags源碼ctags-5.6.tar.gz
http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gzwindows user ... 閱讀全文
Java跨平臺的優勢啊。。。
1. 把WinXP下的所有項目文件復制到Ubuntu下 2. 通過EMS導出數據庫的sql文件,在Ubuntu下導入 3. 在Ubuntu下用MyEclipse加入該項目,重新設置引用的包的路徑(包括數據庫的配置) 4. 把所有包含中文的java源程序設置編碼方式為GBK 5. 注意linux下的mysql是對大小寫敏感的,因此原來windows下配置正確的Hibernate文件可能在linux下需要修改 6. 部署,enjoy
|