|
很簡短,有點(diǎn)trick的感覺,就像小學(xué)里寫的那些(n-1) mod x + 1。進(jìn)了大學(xué)后很少寫類似風(fēng)格的代碼了,這些用在java項(xiàng)目中就要因?yàn)榭勺x性被bs了。 1. 計(jì)算串長度 strlen(a) for (i = 0; a[i] != 0; i++); return i;
2. 復(fù)制 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還是不怎么會用,調(diào)試dfs時(shí)不知道怎么跳出當(dāng)前層(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了半天發(fā)現(xiàn)原來是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項(xiàng)和序列S1, S2, ..., Sn,問題即轉(zhuǎn)化為求i, j,使得Si - Sj = X 把{Sk}和{Sk + X}序列中的數(shù)都放入hash表中,查找沖突項(xiàng)。
BBS上peter大牛的問題:
char *s = "string1"; strcpy(s, "string2"); 這樣為什么會segmentation fault?
后面的解答:
char *s="string1" //此時(shí)"string1"在常量區(qū) s是指向常量區(qū)的一個(gè)指針 你不能對 常量區(qū)的內(nèi)容進(jìn)行修改
char s[]="string2" //此時(shí)"string2" 在棧區(qū) 此時(shí)可以對里面的內(nèi)容進(jìn)行修改
所以你可以寫成
char s[]="string1"; strcpy(s,"string2");
http://www.aurora-x.net/blog/oasis/?p=92 by opengl@rygh
現(xiàn)在市面上系統(tǒng)的,由淺及深的講Ruby的書鳳毛麟角,這本是分量最重的一本。原書第二版出版到現(xiàn)在也過去兩年了,國內(nèi)剛剛在這個(gè)月由博文引進(jìn)電工發(fā)行了譯本。
800+頁的大部頭,一半是核心庫和標(biāo)準(zhǔn)庫的參考,另外一半分為三個(gè)部分——基礎(chǔ)、環(huán)境、高級。這其中我覺得比較有價(jià)值的部分在于“高級這一塊”有助于讓你從一個(gè)更高的層次來理解和掌握這門語言,這一部分也是需要經(jīng)常參考的部分。
就國內(nèi)目前引進(jìn)的唯一一本算是講Ruby的書,要從這本書開始學(xué)習(xí)Ruby估計(jì)會嚇跑不少潛在用戶。作者是大牛沒錯(cuò),不過教學(xué)則是另一回事了(大家都應(yīng)有體會,本科上課的時(shí)候課講的最好的老師通常都不是學(xué)術(shù)最牛的老師)。缺點(diǎn)有兩處很明顯:
第一是自頂向下,它的順序是這樣的:
對象和類–>容器、集合–>標(biāo)準(zhǔn)類型–>表達(dá)式–>異常和模塊–>基本輸入輸出
一個(gè)從上降到低又陡然上升的過程。作者自己也說在第一版里這樣的效果并不好,因此在第二版里特意增加了一個(gè)介紹性章節(jié),粗略的先把所有東西列給讀者看一遍。雖然起到些作用,但是實(shí)際效果我想對于初學(xué)者來說仍然不會太好。C++/Java的書我都讀過不少,也沒見哪個(gè)是一上來就把Class/Object這些東西甩給讀者的,總是從基本類型、控制語句過渡。除非你已經(jīng)對OO這套相當(dāng)熟悉了,否則上來這個(gè)門檻就能把一堆新手擋在門外。
接下來的,談不上十分晦澀,但也不是什么讀來輕快的內(nèi)容,關(guān)鍵是作者給的例子較少,使得象塊、迭代這些特色難以掌握。再有一點(diǎn),ruby的語法風(fēng)格有相當(dāng)部分還保留有Perl的痕跡,而Perl是出了名的以奇怪符號著稱于世,這些符號在新手眼里不外天書,而高手們則愛不釋手。
所以,要能比較順利的通過這本書的入門之路,你得事先具有OO的基礎(chǔ),至少一門腳本語言的經(jīng)驗(yàn)(Perl最佳),函數(shù)式編程的一些概念(否則當(dāng)你看到塊的一些用法時(shí)會很迷惑)。然后,可以用Ruby笨拙的寫一些小程序了。
Ruby作為動態(tài)語言之一,它的最大特點(diǎn)自然是“動態(tài)”兩個(gè)字,其著名的“duck typing”就是一大體現(xiàn)(在我看來,就像是基于接口的調(diào)用,但卻并不用一個(gè)給定的接口去事先限制)。這些在高級部分里都有專門講述,是應(yīng)該重點(diǎn)學(xué)習(xí)的部分。
那么入門究竟用什么書更好?我推薦《Everyday Scripting with Ruby》這本。和《Programming Ruby》同一個(gè)出版社,今年一月份剛出了原版。它是以相當(dāng)循序漸進(jìn)的方式帶領(lǐng)讀者進(jìn)入Ruby的世界,尤其是作者精心設(shè)計(jì)的幾個(gè)Project是全書亮點(diǎn)(學(xué)習(xí)一門語言最好的方式還是要?jiǎng)邮謱懗绦颍?/p>
最后總結(jié)如下,首先看《Everyday》這本書,跟著書中的指導(dǎo)擺弄過所有的Project,并完成相應(yīng)的練習(xí)。然后再看《Programmin》一書的Crystallized部分。再之后就主要是當(dāng)作API參考手冊了。花錢去買中文版我個(gè)人覺得不是很有必要,總共四部分中,前兩部分我覺得蠻雞肋的,第四部分在電腦上查更方便,只有第三部分有較高的價(jià)值。
1. Array#reject 方法遍歷一個(gè)集合的每個(gè)元素,并把符合條件的元素刪去。 例如去掉數(shù)組中的所有素?cái)?shù) 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. 判斷是否在命令行運(yùn)行腳本 if $0 == __FILE__ check_usage compare_inventory_files(ARGV[0], ARGV[1]) end 類似于Java類的main方法,在被其他類導(dǎo)入時(shí)不會運(yùn)行其中的代碼。
4. Enumerable#any? 方法查找一個(gè)集合中是否有滿足條件的元素 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. 關(guān)于測試 這本書(Everyday Scripting with Ruby)的很多程序都是依循測試驅(qū)動開發(fā)的思想寫出來的,測試單元中的方法通常有兩種目的。 一種是direct test,需要測試那個(gè)函數(shù)就直接調(diào)用那個(gè)函數(shù),傳遞的參數(shù)都是直接寫出來的。 另一種是bootstrapping test,被測試函數(shù)的參數(shù)也是通過生成這些參數(shù)的函數(shù)生成的,即一個(gè)方法測試了多個(gè)對象。 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,風(fēng)格和In Action系列差不多,大量的實(shí)例。 現(xiàn)在學(xué)Ruby的主要目的也是everyday scripting,方便數(shù)據(jù)處理、生成,文件批處理等,RoR之類的暫時(shí)不考慮。
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....就能了解一點(diǎn)了
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]
后者與前者的不同之處在于,在處理數(shù)據(jù)的同時(shí),每次處理的返回結(jié)果都會保存到一個(gè)新的數(shù)組中返回。 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. 呃,還是沒有感性認(rèn)識。
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跨平臺的優(yōu)勢啊。。。
1. 把WinXP下的所有項(xiàng)目文件復(fù)制到Ubuntu下 2. 通過EMS導(dǎo)出數(shù)據(jù)庫的sql文件,在Ubuntu下導(dǎo)入 3. 在Ubuntu下用MyEclipse加入該項(xiàng)目,重新設(shè)置引用的包的路徑(包括數(shù)據(jù)庫的配置) 4. 把所有包含中文的java源程序設(shè)置編碼方式為GBK 5. 注意linux下的mysql是對大小寫敏感的,因此原來windows下配置正確的Hibernate文件可能在linux下需要修改 6. 部署,enjoy
|