/******************************
* ΛΥΣΕΙΣ ΘΕΜΑΤΩΝ ΙΟΥΝΗ 2015 *
*****************************/
/*************************************************************************
* ΘΕΜΑ 1ο
*************************************************************************/
// thema1.hpp
#ifndef thema1_hpp
#define thema1_hpp
#define divides(x,y) (((y)%(x) == 0)? 1 : 0)
void change (int n, int a[], int b[], int c[]){
for (int i = 0; i < n; i++)
if (divides(a[i],b[i])) {
c[i] = a[i] + b[i];
}
else {
c[i] = a[i] - b[i];
}
}
void count (int n, int a[], int b[], int &count1, int &count2){
count1 = 0; count2 = 0;
for (int i = 0; i < n; i++)
if (divides(a[i],b[i])){
count1++;
}
else {
count2++;
}
}
#endif
// thema1.cpp
#include <iostream>
#include "thema1.hpp"
using namespace std;
int main(){
const unsigned int n = 3;
int x[n], y[n], z[n];
int c1, c2;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
change(n, x, y, z);
for (int i = 0; i < n; i++) cout << z[i] << '\n';
count(n, x, y, c1, c2);
cout << c1 << " " << c2 << '\n';
return 0;
}
/*************************************************************************
* ΘΕΜΑ 2ο
*************************************************************************/
//thema2.cpp
#include <iostream>
using namespace std;
void subtract (int n, int a[], int b[], int c[], int i) {
if (i == 0) {
c[0] = a[0] - b[n-1];
}
else {
c[i] = a[i] - b[n-1-i];
subtract(n, a, b, c, i-1);
}
}
float sum (int n, int a[], int i){
return (i == 0) ? float(a[0]) : (sum(n, a, i-1) + float(a[i]) / (i+1) );
}
// την sum () θα μπορούσα να την γράψω και ως:
//float sum2 (int n, int a[], int i){
// if (i == 0) return float(a[0]);
// else return (sum(n, a, i-1) + float(a[i]) / (i+1));
//}
int main() {
unsigned int n;
cin >> n;
if (n == 0) return -1;
int *x = new int[n];
int *y = new int[n];
int *z = new int[n];
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
subtract(n, x, y, z, n-1);
for (int i = 0; i < n; i++) cout << z[i] << '\n';
cout << sum(n, x, n-1) << '\n';
delete [] x; delete [] y; delete [] z;
return 0;
}
/*************************************************************************
* ΘΕΜΑ 3ο
*************************************************************************/
//thema3.cpp
#include <iostream>
using namespace std;
int main() {
unsigned int n;
//cout << "Δώσε το n\n";
cin >> n;
if (n == 0) {
return -1;
}
int *x = new int[n], *y = new int[n], *z = new int[n];
int *px = NULL, *py = NULL, *pz = NULL;
//cout << "Δώσε τα x\n";
for (px = x; px < x + n; px++) {
cin >> *px;
}
//cout << "Δώσε τα y\n";
for (py = y; py < y + n; py++) {
cin >> *py;
}
for (px = x, py = y + n - 1, pz = z; px < x + n; px++){
*pz = *px - *py;
py--;
pz++;
}
for (pz = z; pz < z + n; pz++) {
cout << *pz << '\n';
}
float sum = 0.0f;
px = px - n;// px -= n;
for (int i = 0; i < n; i++){
sum = sum + float(*px)/(i+1); // sum += float(*px)/(i+1);
px++;
}
cout << sum << '\n';
delete [] x, y, z; // ή delete [] x; delete [] y; delete [] z;
return 0;
}
/*************************************************************************
* ΘΕΜΑ 4ο
*************************************************************************/
// thema4.cpp
#include <iostream>
#include <cmath>
using namespace std;
const float pi = 4.0f*atan(1.0f);// pi = acos(-1.0);
struct point {
float x;
float y;
};
class circle {
private:
char c;
point pos;
float r;
public:
// constructor
circle (char cc, point ppos, float rr) {
c = cc;
pos.x = ppos.x;
pos.y = ppos.y;
r = rr;
}
// constructor
circle (const circle &a) {
c = a.c;
pos.x = a.pos.x;
pos.y = a.pos.y;
r = a.r;
}
// destructor
~circle () {
cout << "\n destructing " << c << " " << pos.x << " "
<< pos.y << " " << r << "\n";
}
// method 1
bool cocentric (const circle &a){
return c == a.c;
}
// method 2
float area (){
return pi*pow(r,2);
}
// operator = overloading (ανάθεση τιμών δεξιού κύκλου στον αριστερό)
circle &operator = (const circle &a){
if (this != &a){ // έλεγχος αυτοαναφοράς
c = a.c;
pos.x = a.pos.x;
pos.y = a.pos.y;
r = a.r;
}
return *this; // αλλιώς επέστρεψε τον κύκλο circle1 (αν circle1 = circle1)
}
// operator != overloading (έλεγχος αν έχουμε διαφορετικούς κύκλους)
bool operator != (const circle &a) {
if (c != a.c || r != a.r) return true;
else return false;
}
friend void print (circle a);
};
void print (circle a) {
cout << a.c << " " << a.pos.x << " " << a.pos.y << " " << a.r;
}
int main () {
point p;
p.x = 0.3f;
p.y = 2.f;
circle c1 ('A', p, 3.4f);
circle c2 (c1);
print (c1);
if (c1.cocentric(c2)){
cout << " cocentric with ";
}
else {
cout << " is not concentric with ";
}
print(c2);
cout << "\n";
circle *c3 = new circle(c2);
cout << c3->area() << "\n";
print(c2);
if (c2 != c1){
cout << " is not the same with ";
}
else {
cout << " is the same with ";
}
print (c1);
cout << "\n";
return 0;
}
Τους κώδικες μπορείτε να τους κατεβάσετε από τα αρχεία: thema1.cpp, thema1.hpp, thema2.cpp, thema3.cpp, thema4.cpp.
Οι εκφωνήσεις: Θέματα Ιούνη 2015(pdf)