Google Search Box

自訂搜尋

Wednesday, July 14, 2010

[閒聊] 炮灰Day1 程式設計概要

作者: chrisdar (克里斯) 看板: Examination
標題: [閒聊] 炮灰Day1 程式設計概要
時間: Wed Jul 14 21:00:35 2010

#include
#include
#include
using namespace std;

int most_significant_bit_set_bit(int num) {
int r = -1;
for (; num >>= 1;) {
r++;
}
return r;
}

int string_search(char str[], char pat[]) {
for (int i = 0; str[i] != 0; ++i) {
int j = 0, k = 0;
for (; pat[j] != 0; ++j) {
k += (str[i+j] == pat[j] ? 1 : 0);
}
if (j == k) {
return i;
}
}
return -1;
}

typedef struct node {
int d;
struct node *next;
} node;

void split(node *h, node **h1, node **h2) {
node *h1w = 0, *h2w = 0;
for (; h != 0; h = h->next) {
if (((h->d) & 1) == 1) {
(h1w == 0 ? *h1 = h1w = h : h1w->next = h), h1w = h;
} else {
(h2w == 0 ? *h2 = h2w = h : h2w->next = h), h2w = h;
}
}
h1w->next = 0, h2w->next = 0;
}

int main(int argc, char* argv[]) {
srand((unsigned)time(0));
int v = rand();
cout << bitset<32>(v) << endl;
cout << most_significant_bit_set_bit(v) << endl << endl;

char str[] = "strin";
char pat[] = "ing";
cout << str << endl;
cout << pat << endl;
cout << string_search(str, pat) << endl << endl;


node data[] = {{3}, {4}, {5}, {7}, {8}};
for (int i = 0; i < 4; ++i) {
data[i].next = &data[i+1];
}
for (node *h = data; h != 0; h = h->next) {
cout << h << " - " << h->d << " - " << h->next << endl;
}
node *h = data, *h1 = 0, *h2 = 0;
split(h, &h1, &h2);
cout << "

1 comment:

  1. 推 lucifax:看的懂得人好厲害 根本就是天書 61.231.102.44 07/14 21:06
    推 yangmilk:看得懂不難 不過要po上來真的很屌 61.228.163.129 07/14 21:09
    → yangmilk:應該打字打很久吧 囧 厲害 61.228.163.129 07/14 21:09
    推 abc93:看得懂得人好厲害 根本就是亂碼... 118.165.48.130 07/14 21:10
    推 TsaoCCFGOGO:第一題跟我想的很像,只是我是反過來 124.11.241.50 07/14 21:10
    → morphine0821:>31 或 num=0 照題意不是要傳 -1? XD 114.47.214.55 07/14 21:11
    → chrisdar:忘了 是-1才對 123.195.65.233 07/14 21:13
    推 glovegp20:跨謀... 我也很佩服看懂的人 122.123.136.204 07/14 21:13
    推 BeTry:推有勇氣上來PO不過好像第一題都不考慮整數 61.59.153.207 07/14 21:14
    → BeTry:轉二進位的小bug? 還是都自己假設已經轉好 61.59.153.207 07/14 21:14
    推 unfinish:用筆寫程式跟用電腦寫程式真的是不一樣 114.43.48.216 07/14 21:15
    → BeTry:並且存好在對應的結構中? 61.59.153.207 07/14 21:15
    → BeTry:只有我白痴附加還寫了一段整數轉binary? 61.59.153.207 07/14 21:16
    → chrisdar:to BeTry: 因為函數直接就定義INT 123.195.65.233 07/14 21:16
    → chrisdar:int 就能直接操作了 123.195.65.233 07/14 21:17
    推 TsaoCCFGOGO:那只要前面兩個 if 做掉就好 XD 124.11.241.50 07/14 21:17
    → BeTry:我是說他是輸入一個num是個整數吧? 61.59.153.207 07/14 21:17
    → morphine0821:不就只要傳回最高位的1是哪位就好? 114.47.214.55 07/14 21:17
    → TsaoCCFGOGO:不然題目是「C語言」,原po的是C++… 124.11.241.50 07/14 21:18
    → BeTry:例如它給你444(題意)怎樣跳00110000之類的 61.59.153.207 07/14 21:18
    → BeTry:所以我才說大家是都假設num已經直接給0101? 61.59.153.207 07/14 21:19
    → morphine0821:第三題能直接呼叫字串函數搞定嗎?XD 114.47.214.55 07/14 21:19
    → TsaoCCFGOGO:電腦都是binary,十進位是人看的 124.11.241.50 07/14 21:19
    → TsaoCCFGOGO:BeTry大,那樣就變成string compare 124.11.241.50 07/14 21:19
    → chrisdar:C++ 嘛 單純看函數本身是C99啦 XDD 123.195.65.233 07/14 21:20
    → TsaoCCFGOGO:所以我覺得那不太會影響分數 XD 124.11.241.50 07/14 21:20
    推 BeTry:我寫的時候覺得很多陷阱~負整數進來我也想過 61.59.153.207 07/14 21:21
    → TsaoCCFGOGO:那更簡單,負整數一定是 31... 124.11.241.50 07/14 21:22
    → TsaoCCFGOGO:if (num < 0) return 31; 一行再見 124.11.241.50 07/14 21:22
    → BeTry:那是signal bit吧= = 61.59.153.207 07/14 21:22
    推 TsaoCCFGOGO:是 sign bit 也是 MSB… 124.11.241.50 07/14 21:26
    → TsaoCCFGOGO:http://0rz.tw/V2bGo 124.11.241.50 07/14 21:27
    → morphine0821:人家只要1,不用管 signal bit XD 114.47.214.55 07/14 21:27
    推 BeTry:就自己寫一寫覺得好像有漏洞要補 61.59.153.207 07/14 21:28
    推 alibodawahah:GG~沒考慮到負數XDXD 118.171.192.208 07/14 21:29
    推 arumina:用%就不用這麼麻煩了 114.41.44.159 07/14 21:29
    → BeTry:開始想東想西思考要不要去補起來 61.59.153.207 07/14 21:29
    推 Snou:我耍呆了 我把負數 return 32 = ="" 122.118.71.175 07/14 21:37
    → chrisdar:真的要玩考生就出 MSB_set_bit(long num) 123.195.65.233 07/14 21:38
    → chrisdar:一個LONG 32/64各自表述 123.195.65.233 07/14 21:38
    → BeTry:大概是看到題目不太完整怕被裱就想東想西 203.70.223.213 07/14 21:39
    → BeTry:<==腦筋太死 203.70.223.213 07/14 21:41

    ReplyDelete