給定一個prefix(第一行輸入),輸出不包含此prefix的string
aaaa
aaaabbbbccc
badssssd
mdjeld
bbaaaakkdkd
badssssd
mdjeld
bbaaaakkdkd
void check_prefix(string input, string prefix) {
string sim_input = input.substr(0, prefix.length());
if (sim_input != prefix) {
cout << input << "\n";
return;
}
}
矩陣轉置
3 3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
// get input
for (int i = 0 ; i < row ; i++) {
for (int j = 0 ; j < col ; j++) {
cin >> x[i][j];
}
}
// transpose
for (int i = 0 ; i < col ; i++) {
for (int j = 0 ; j < row ; j++) {
cout << x[j][i] << " ";
}
cout << "\n";
}
(選擇題)問以下呼叫greeting 10次,print出cnt是多少?
void greeting() {
static int cnt = 0;
++cnt;
}
// solution : 10
(問答題)問 j = ?
int j=0;
int n=10;
while(n--!=0)
{
j++;
}
// solution : 10
// can treat as
for (int i = n ; i != 0 ; i--) {
j++;
}
(選擇題)問輸出是多少?
#define SWAP(a,b) tmp = a; a = b ; b= tmp
int a = 10;
int b = 20;
int tmp = 0;
int n = 6;
if(n > 6)
SWAP(a,b);
printf("%d%d%d",tmp,a,b);
// solution : 0, 20, 0
(選擇題)哪個選項能使n = 5
int n = 3;
if(n > 2)
n = 4;
____(n > 3)
n = 5;
(1)else
(2)else if
(3)if
ans : (3)
Q5
(問答題)問輸出
int main()
{
int arr[8] = {0,1,2,3,4,5,6,7};
int* ptr1 = &arr[0];
char* ptr2 = (char*) ptr1;
printf("%d",*ptr2);
return 0;
}
(多選題)問哪邊會出現錯誤(以及錯在哪),及要如何初始化?
union Var{
char ch;
int num1;
double num2;
};
int main()
{
union Var var = {123};
printf("var.ch = %c\n",var.ch);
printf("var.num1 = %d\n",var.num1);
printf("var.num2 = %.3f\n",var.num2);
return 0;
}
(問答題)問輸出?
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int cnt;
for(cnt = 5 ; cnt-- ; ++a);
for(cnt = 5 ; --cnt ; b++);
for(cnt = 5 ; --cnt>0 ; ++c);
for(cnt = 5 ; cnt-->0 ; d++);
printf("%d",a*b*c*d);
return 0;
// a = 5 , b = 4 , c = 4 , d = 5
// 400
}
問以下pointer意思
int p //⼀個整型數(An integer)
int *p //⼀個指向整型數的指標(A pointer to an integer)
int **p //⼀個指向指標的的指標,它指向的指標是指向⼀個整型數(A pointer to a pointer to an integer)
int p[10] //⼀個有10個整型數的陣列(An array of 10 integers)
int *p[10] //⼀個有10個指標的陣列,該指標是指向⼀個整型數的(An array of 10 pointers to integers)
int (*p)[10] //⼀個指向有10個整型數陣列的指標(A pointer to an array of 10 integers)
int (*p)(int) //⼀個指向函數的指標,該函數有⼀個整型參數並返回⼀個整型數(A pointer to a function that takes an integer as an argument and
int (*p[10])(int) //⼀個有10個指標的陣列,該指標指向⼀個函數,該函數有⼀個整型參數並返回⼀個整型數
inline的好處
inline 可以將修飾的函式設為⾏內函式,即像巨集 (define) ⼀樣將該函式展開編譯,⽤來加速執⾏速度。
global,local,static 變數的生命週期
local:只存在當下函式執行期間
static:變數宣告時若加上 static,執行時期會一直存在記憶體的固定位置
global:全域變數的生命週期始於程式開始,終止於程式結束
解釋const
被const所修飾到的內容是不可變的。
reverse string in C?
#include <stdio.h>
#include <string.h>
void reverse(char*, int, int);
int main()
{
char a[100];
gets(a);
reverse(a, 0, strlen(a)-1);
printf("%s\n", a);
return 0;
}
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
call by value, call by reference?
Explain "volatile" ?
volatile 修飾的變數代表它可能會被不預期的更新。volatile 是在指⽰編譯器每次對該變數進⾏存取時都要「⽴即更新」,不應該
對其做任何最佳化。不存取register的值,⽽是直接從記憶體取值。
#define MIN(A,B) A < B ? A:B; 這樣會有甚麼問題?
int result = 2 * MIN(6,10); // 2*6<10?6:10;
//result 會變成10,但我們要的是12
void clearBit(unsigned int& x){
x = x & ~(1 << 17);
}
void setbit(unsigned int& x){
x = x | (1 << 17);
}
void reverseBit(unsigned int& x){
x = x ^ (1 << 17);
}
reverse string
同上
給一個struct,問你這個struct佔幾byte?
https://www.itread01.com/content/1549653142.html
已排序好的arr, Binary Seach 找 key,有的話回傳true,沒回傳的話false
bool binarySearch(vector<int>& nums, int target,int start ,int end)
{
if(start<=end)
{
int mid = (start + end) / 2;
if(nums[mid] == target)
return true;
else if(nums[mid] < target)
return binarySearch(nums, target, mid+1, end);
else
return binarySearch(nums, target, start, mid-1);
}
return false;
}
Linked list 實作queue的各種操作,push, pop, getfront, getback, isempty, qsize
問輸出
int main()
{
unsigned int a = 10;
for(a; a>=0; --a){
cout<<a<<endl;
}
return 0;
}
10
9
.
.
.
0
4294967295
4294967294
.
.
.
1
0
4294967295
重複下去
0~500個數字每次隨機 取一個數字出來,但下次在抽出時不可以出現已經抽過的數字,問你如何時實現。
https://leetcode.com/problems/shuffle-an-array/
pseudo code
vector<int>v(500,0);
for(int i = 1 ; i <= 500 ; i++)
v[i] = i;
for(int i = 499 ; i >= 0 ; i--)
{
int randIndex = rand(0,i);
cout<<v[randIndex]<<" ";
swap(v[randIndex],v[i]);
}
// Time complexity O(n)