Int main() {
int F = 1;
for (int i=2; i <= NUM; i++)
F *= i;
cout << NUM << "! = " << F << endl;
return 0;
}
6
#include <iostream>
using namespace std;
Int main() {
char D[] = {0,4,1,5,3,2};
cout << "D[D[3]]-D[D[5]] = " << D[D[3]]-D[D[5]] << endl;
return 0;
}
7, 8, 32, 41, 45
Указать к какому классу относится каждый из перечисленных IP адресов:
192.168.0.15
127.0.0.1
112.0.0.15
167.58.13.21
Решение:
IPv4 адрес состоит из 32 битов, которые разделены на 4 октеты.
Класс сети определяется по первым битам IP адреса:
Класс |
Первые биты |
Назначение |
A |
0 |
Unicast |
B |
10 |
Unicast |
C |
110 |
Unicast |
D |
1110 |
Multicast |
E |
1111 |
Reserved |
192.168.0.15
192 |
|||||||
1 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
Ответ: Класс С
127.0.0.1
127 |
|||||||
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
Ответ: Класс A
112.0.0.15
112 |
|||||||
0 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
Ответ: Класс A
167.58.13.21
167 |
|||||||
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Ответ: Класс B
9, 25, 26
#include <iostream>
using namespace std;
class Queue {
struct Element {
int value;
Element* prev;
Element(int val) {
value = val;
prev = 0;
}
};
Element* back;
Element* front;
public:
Queue() {
back = 0;
front = 0;
}
void push_back(int val) {
Element* newel = new Element(val);
if ( back )
back->prev = newel;
else
front = newel;
back = newel;
}
int pop_front() {
int res = front->value;
Element* oldfront = front;
front = front->prev;
if (! front)
back = 0;
delete[] oldfront;
return res;
}
bool empty() {
if (front)
return false;
else
return true;
}
~Queue() {
while (! empty())
pop_front();
}
};
Int main() {
Queue queue;
//default
queue.push_back(1);
queue.push_back(2);
cout << "List of commands:" << endl;
cout << "\t push_back" << endl;
cout << "\t pop_front" << endl;
cout << "\t quite" << endl;
cout << endl;
bool quite = false;
string cmd = "";
while (! quite) {
cout << "Input command: ";
cin >> cmd;
if (cmd == "pop_front" || cmd == "pop") {
if (! queue.empty() )
cout << " " << queue.pop_front() << endl;
else
cout << "The queue is empty!" << endl;
} else if ( cmd == "push_back" || cmd == "push") {
int val; cout << " Input value: "; cin >> val;
queue.push_back(val);
} else if ( cmd == "quite" || cmd == "q" || cmd == "exit") {
quite = true;
cout << "exit" << endl;
} else {
cout << "Unknown command!" << endl;
}
}
return 0;
}
10, 27
#include <iostream>
using namespace std;
void xorSwap (int &x, int &y) {
if (x != y) {
x ^= y;
y ^= x;
x ^= y;
}
}
void bufferSwap (int &x, int &y) {
int temp = y;
y = x;
x = temp;
}
void outputArray(int* array, int& size) {
cout << " array = [ ";
for(int i = 0; i < size; i++)
cout << array[i] << ", ";
cout << "]" << endl;
}
int main(int argc, char const *argv[]){
int size = 3;
cout << "Input size of array = "; cin >> size;
int* array = new int[size];
cout << "Input array:" << endl;
for(int i = 0; i < size; i++) {
cout << " array[" << i << "] = "; cin >> array[i];
}
cout << "\nInputed array:" << endl;
outputArray(array, size);
for(int left = 0, right = size - 1; left < size/2; left++, right--)
xorSwap(array[left], array[right]);
cout << "\nReverted with XOR:" << endl;
outputArray(array, size);
for(int left = 0, right = size - 1; left != right; left++, right--)
bufferSwap(array[left], array[right]);
cout << "\nReverted the reverted array with buffer:" << endl;
outputArray(array, size);
delete[] array;
return 0;
}
/*
XOR:
a= 1010
b= 0011
a= 1001
b= 1010
a= 0011
*/
11, 17
#include <iostream>
using namespace std;
void outputArray(unsigned short* array, unsigned short& size) {
cout << " array = [ ";
for(unsigned short i = 0; i < size; i++)
cout << array[i] << ", ";
cout << "]" << endl;
}