Egbert Lin's Blog

“Life is not a race, but a journey to be savoured each step of the way” by Brian Dyson

Classic C/C++ program - You must to understand

Classic and Basic C/C++ Question

There are four classical question abount C/C++ interview.

Question 1: Fibonacci Sequence

Version 1

This is a recursive function method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int fib(int n){
if(n == 0) return 0;
if(n == 1) return 1;
return fib(n -1) + fib(n-2);
}

int main() {
int n;
cout << "Please give n:"
cin >>n;
for(int i = 0; i < 7; ++i){
cout << fib(i) << " ";
}
return 0;
}

Version 2

This is an iteration method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

void fib(int n){
int curr = 0;
int next1 = 1;
int next2 = 0;

for(int i = 0; i < n; ++i){
cout << curr << " ";
next2 = curr + next1;
curr = next1;
next1 = next2;
}
}

int main() {
int n;
cout << "Please give n:";
cin >>n;
fib(n);
return 0;
}

Question 2: Palindrome Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

bool palindrome(int n){
if(n < 0 || (n!=0 && n %10 == 0)) return false;
int res = 0;
while(n > res){
res = res*10 + n % 10;
n /= 10;
}
return (res == n) || (res / 10 == n);
}

int main() {
int n;
cout << "Please give n:";
cin >>n;
cout << boolalpha;
cout << palindrome(n);
return 0;
}

Question 3: Check number if it's prime number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

using namespace std;

bool primeNumber(int n){
if(n < 2) return false;

vector<int> v(n + 1, 0);
int vNumber = v.size();
int i = 2;

while(i < vNumber){
if(v[i] == 0){
for(int j = 2; i*j < vNumber; ++j){
v[i*j] = 1;
}
}
++i;
}
return (v[n]==0)?true:false;
}

int main() {
int n;
cout << "Please give n:";
cin >>n;
cout << boolalpha;
cout << primeNumber(n);
return 0;
}

Question 4: Draw Pyramid and Triangle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>

using namespace std;


void drawPyramid(int floors){
for(int i = 1; i <= floors; ++i){
for(int j = 1; j <= floors - i; ++j){
cout << " ";
}
for(int k = 1; k <= i*2 - 1; ++k){
cout << "*";
}
cout << endl;
}
cout << endl;
}

void drawInvertedPyramid(int floors){
for(int i = floors; i >= 1; --i){
for(int j = i; j < floors; ++j ){
cout << " ";
}

for(int k = 1; k <= i*2 - 1; ++k){
cout << "*";
}
cout << endl;
}
cout << endl;
}

void drawTriangle(int floors){
for(int i = 1; i <= floors; ++i){
for(int j = 1; j <= i*2 - 1; ++j){
cout << "*";
}
cout << endl;
}
cout << endl;
}

void drawInvertedTriangle(int floors){
for(int i = floors; i >= 1; --i){
for(int j = i; j >= 1; --j){
cout << "*";
}
cout << endl;
}
cout << endl;
}

int main() {
int f;
cout << "Please give f:";
cin >> f;
drawPyramid(f);
drawInvertedPyramid(f);
drawTriangle(f);
drawInvertedTriangle(f);

return 0;
}