# 群聯 線上C測驗 ## 0101(竹北) ### 1 write the following code a.set Bit3 of int a b.clear Bit3 of int a a = a | (1<<3) a = a & ~(1<<3) --- ### 2 set absolut address 0x1234 to the value 0x4321 ``` int *p; p = (int*) 0x1234; *p = 0x4321; ``` --- ### 3 下面code有何錯誤 ``` unsigned int i=100; for(;i>=0;i--) { printf("%d\n",i); } ``` * i為unsigned int,因此不會小於0,造成無窮迴圈。 #### 主管加考: a. 如果i已經變成0,再繼續遞減會變成什麼? * Ans : unsigned int的最大值 b. %d無法顯示到unsigned int的最大值該改成什麼? * Ans : %lu --- ### 4 求a[]的值 int a[]={6,7,8,9,10}; int *p=a; *(p++)+=123; *(++p)+=123; * a[]={129,7,131,9,10} --- ### 5 #define sum(a,b) a+b what is sum(5,6)*10 * 5+6*10 = 65 該如何改善? 加() --- ### 6 what is size of StructA and StructB ``` Struct A { int a; short b; int c; char d; } Struct B { int a; short b; char c; int d; } ``` * Struct A size is 16 * Struct B size is 12 --- ## 0201(竹南) PCIe ### 1 * a. write a function to get bit18 value of an unsigned integer data and return 0 or 1. `a = (x>>18) & 0x01;` * b. write a function to set bit18 value of an unsigned integer data. `#define get(x,n) x = x | (1 << 18)` * c. write a function to clear bit18 value of an unsiged integer data. `#define clear(x,n) x = x & ~(1 << 18)` * d. write a function to inverse bit18 value of an unsiged integer data. `#define inverse(x,n) x = x ^ (1 << 18) ` ### 2 以下的迴圈印出的數字為何? Unsigned int i; for ( i=10 ; i>= 0; --i ) { printf("%d\n",i); } * 此為無窮迴圈,因為unsigned int不會變成負值,為0時進行遞減會變成unsigned int的最大值((2^32)-1) ### 3 以linked list實作queue, 需要達到以下功能: Push(data):把資料放進Queue Pop:把資料從Queue中移除 getFront:回傳最舊的資料 getBack:回傳最新的資料 IsEmpty:確認Queue裡是否有資料 getSize:回傳Queue裡的資料個數 ``` typedef struct list { int data; struct list *next; }List; List* push(List *a, int num) { List *node = malloc(sizeof(struct list)); List *first = a; while(a->next != NULL) { a = a ->next; } node -> data = num; a-> next = node; return first; } List* pop(List *a, int target) { List *first = a; List *pre = NULL; while(a->data != target && a->next != NULL) { pre = a; a = a ->next; } a -> data = 0; pre->next = a->next; free(a); return first; } ``` 我寫了兩個也不確定對不對,主管就說往後面的題目作答了 ### 4 what is the result? ``` int a[] = {1,2,3,4,5,6}; int *p = a; *(p++) += 100; *(++p) += 100; for(inti = 0; i<6 ; i++) printf(%d "",a[i]);" ``` * a[] = {101,2,103,4,5,6} ### 5 Re-write ``` int(*(*abcd)[10] (char*) typedef _______ pf(*abcd)[3] typedef int(*pf)(char*) 這題我背考古的XD我也不知道在寫啥,但他就說好然後就下一題了。BTW,感覺最後一個應該是[10]而不是[3] ``` ### 6 the value of *(&a+1), *(p-1)? ``` int a[5]={1,2,3,4,5}; int*p =(int*)(&a+1); ``` * *(&a+1) = 指到5之後的一個位置 * *(p-1) = 5 ### 白板題1 輸入二進制的unsigned int 將其inverse,再進行輸出。 ``` unsigned int reverse(unsigned int num) { unsigned int ans = 0; while(num) { ans = ans<<1 + num&0x01; num>>1; } return ans; } Test case: 111100000 output : 000001111 ``` ### 白板題2 判斷符號順序合理性,如{{(<>)}},並輸出true/false ``` enum apl {"{","(","<",">",")","}"}; // 0 1 2 3 4 5 此用法有誤 僅表達想法 int judge(char *s) { int len = strlen(s); int num = 0; // int lock = 0; int arr[100]; int arr_pos = 0; for(int i=0; i< len; i++) { if(pos == 0 && s[i]> 2) return 0; else if(s[i]<=2) { num+=s[i]; arr[arr_pos] = s[i]; pos++; } else if(arr[arr_pos] != 5-s[i]) return 0; else if(arr[arr_pos] == 5-s[i]) { num-=s[i]; pos--; } } if(num != 0) return 0; return 1; } 正常要用stack來解,但我腦袋卡住直接忘記XD Test case: {{(<>)}} {}()<> {{(<)>}} output : True true false ``` ### 白板題3 不用乘除判斷是否為3的倍數,也不可以用十進制中所有數字總和為3倍數的觀念。 ``` int mul(int num) { int odd=0,even=0; while(num) { odd += num&0x01; num>>1; even += num & 0x01; num>>1; } while(odd-even > 0) { odd -=3; } if(odd-even < 0) return 0; else if(odd-even == 0) return 1; } ```