Date: January 11th, 2012
Time: 14:10-15:40
Open book; turn off computer & mobile phone
// character string
#include <iostream>
using std::cout;
int main()
{
char name[] = 'Coconut';
cout << sizeof(name) << '\n';
return 0;
}
// Precedence (P.77)
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int a=2, n=3;
int sum=0;
while ( n-- >= 1)
{
sum+=a;
a = a * 10 + a % 10;
}
cout << sum << endl;
return 0;
}
// ASCII code of a character
#include <iostream>
void bar(short n)
{
for (short i=0; i<n; i++)
printf("%c", 72); // 'A' == 65
printf("\n");
return;
}
int main()
{
short b[] = { 5, 4, 3, 5, 4, 3 };
int i;
for (i=0; i< 4; i++)
{
printf("%2d ", b[i]);
bar(b[i]);
}
return 0;
}
// nested for-loop
#include <iostream>
using std::cout;
int main()
{
bool prime;
int i, j;
cout << "These are prime numbers:\n";
for (i=2; i < 20; i++)
{
prime = true;
for (j=3; j < i; j++)
if (i / j * j == i) // Is i dividable by j?
prime = false;
if (prime)
cout << i << ' ';
}
return 0;
}
// array
#include <iostream>
using std::cout;
int main()
{
const int MAX = 100;
bool prime[MAX] = { false, false };
int i, j, count = 0;
for (i=2; i < MAX; i++)
prime[i] = true;
for (i=2; i < MAX; i++)
for (j=i*2; j < MAX; j+=i)
prime[j] = false;
for (i=2; i < MAX; i++)
{
if (prime[i])
{
cout << i << '\t';
if (++count % 5 == 0) cout << '\n';
}
}
return 0;
}
#include <iostream>
using std::cout;
void swap(int& i, int &j)
{
j ^= i; i ^= j; j ^= i; // eXclusive OR
}
int main()
{
int data[] = { 1, 3, 5, 7, 2, 4, 6 };
int i, j;
for (i=0; i<6; i++)
for (j=i+1; j<7; j++)
if (data[i] < data[j])
swap(data[i], data[j]);
for (i=0; i<7; i++)
cout << data[i];
return 0;
}
// Linked-list
#include <iostream>
using std::cout;
struct Node
{
int value;
Node* next;
};
int main()
{
int a[] = { 3, 5, 1, 7, 2, 4, 6};
Node* p;
Node* current;
Node* previous;
Node* head = new Node;
head->value = a[0]; head->next = NULL;
int i, n;
n = sizeof(a) / sizeof(a[0]);
for (i=1; i < n; i++)
{
p = new Node;
p->value = a[i];
if (head->value > a[i]) // I am the smallest
{
p->next = head;
head = p;
continue;
}
current = previous = head;
while (current != NULL && current->value < a[i])
{
previous = current;
current = current->next;
}
if (current == NULL) // I am the largest
{
previous->next = p;
p->next = NULL;
}
else
{
previous->next = p;
p->next = current;
}
}
p = head;
while (p)
{
cout << p->value;
p = p->next;
}
cout << '\n';
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int n = 28;
int odd = 0;
int even = 0;
while (n > 0)
{
switch (n % 2)
{
case 0:
even++;
break;
case 1:
odd++;
} // This "break" statement is omitted. What will happend?
n >>= 1;
}
cout << "even=" << even << endl;
cout << "odd=" << odd << endl;
return 0;
}
// This question comes from NTHU
#include <iostream>
using std::cout;
int f(char *s, char *t)
{
char *p1, *p2;
for (p1 = s; *p1; p1++)
{
for (p2 = t; *p2; p2++)
if (*p1 == *p2) break;
if (*p2 == '\0') break;
}
return p1 - s;
}
int main()
{
cout << f("abccbad", "babc") << '\n';
return 0;
}