diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e915029 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +п»ї################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs diff --git a/Seminar01/sem1_solutions/1_allEx.cpp b/Seminar01/sem1_solutions/1_allEx.cpp new file mode 100644 index 0000000..3241e17 --- /dev/null +++ b/Seminar01/sem1_solutions/1_allEx.cpp @@ -0,0 +1,125 @@ +#include +using namespace std; + + +int main() { + ////1 + //cout << "Oh what\na happy day!\nOh yes,\nwhat a happy day!"; + + ////2 + //double s1 = 5.4*7.9; + //double p1 = (5.4+7.9) * 2; + //cout << "\nS = "<> lev; + //double dollar = lev * 0.56; + //double euro = lev * 0.51; + //cout << "\nDollar = " << dollar; + //cout << "\nEuro = " << euro; + + ////4 + //cout << "\nPlease enter length and width of the rectangle:\n"; + //double a, b; + //cin >> a>> b; + //double s2 = a * b; + //double p2 = (a + b) * 2; + //cout << "\nS = " << s2; + //cout << "\nP = " << p2; + + ////5 + //int c, d; + //cin >> c >> d; + //cout << "\n"; + //bool c_is_smaller = c < d;for + //cout <<"" << c_is_smaller; + + ////6 + //int dividend, divisor; + + //cout << "\nDividend: "; + //cin >> dividend; + //cout << "\nDivisor: "; + //cin >> divisor; + //cout << "\nThe quotient of the division is : " << (dividend / divisor); + //cout << "\nThe remainder of the division is : " << (dividend % divisor); + + ////7 + //int apples, pears, bananas; + //cout << "Apples: "; + //cin >> apples; + //cout << "Pears: "; + //cin >> pears; + //cout << "Bananas: "; + //cin >> bananas; + //cout << "Pesho, don’t forget to buy " << apples << " apples, " << pears << " pears and " << bananas << " bananas!"; + + + ////8 + //double r, length, S; + //cin >> r; + //length = 2 * 3.14159265 * r; + //S = 3.14159265 * r * r; + + ////9 + //int a, b, c; + //cin >> a >> b >> c; + + //double D = (b * b) - (4 * a * c); + //double x1, x2; + + //x1 = (-b + sqrt(D)) / (2 * a); + //x2 = (-b - sqrt(D)) / (2 * a); + //cout << "x1 = " << x1 << ", x2 = " << x2; + +// //10 +// int a, b, save; +// cin >> a >> b; +// +// /*save = a; +// a = b; +// b = save;*/ +// a += b; +// b = a - b; +// a -= b; +//cout << a << " " << b; + + ////11 + //int a, b; + //cin >> a >> b; + //bool is_a_bigger = a > b; + //cout<< is_a_bigger*a + + + + ////12 + //int seconds,days,hours,minutes; + //cin >> seconds; + //days = seconds / 60 / 60 / 24; + //seconds -= days * 24 * 60 * 60; + //hours = seconds / 60 / 60; + //seconds -= hours * 60 * 60; + //minutes = seconds / 60; + //seconds -= minutes * 60; + //cout << days << " days, " << hours << " hours, " << minutes << " minutes and " << seconds << " seconds"; + + + //13 + cout << "Point1: "; + int x1, y1, x2, y2; + cin >> x1 >> y1; + cout << "\nPoint2:"; + cin >> x2 >> y2; + + double length = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + int x = length * 100; + cout << (double)x / 100.00 << std::endl; + + + + //14 + int n; std::cin >> n; + std::cout << n * (n + 1) / 2< +using namespace std; + +int main() +{ + + // Zad_1 + //int year; + //cin >> year; + //if (year % 400 == 0) { + // cout << year << " is Leap year. \n"; + //} + //else { + // + // cout << year << " isn't Leap year. \n"; + // + //} + // + //// Zad_2 + //char symbol; + //cin >> symbol; + // + //if ((symbol >= 65 && symbol <= 90) || (symbol >= 97 && symbol <= 122)) { + // + // switch(symbol) { + + // case'a': + // case'A': + // case'o': + // case'O': + // case'e': + // case'E': + // case'i': + // case'I': + // case'u': + // case'U': + // case'y': + // case'Y': + // cout << symbol << " is Vowel letter. \n"; + // break; + + // + // default: + // cout << symbol << " is Consonant letter. \n"; + // + // + // } + //} + //else + //{ + // + // cout << symbol << " isn't a letter. \n"; + // + //} + // + //// Zad_3 + //int num1, num2; + //cin >> num1 >> num2; + // + //cout << ((num1 > num2) ? num1 : num2); + // + //// Zad_4 + //int percentage; + //cout << "Result: \n"; + //cin >> percentage; + // + //cout << "Grade: \n"; + // + //if (percentage >= 90) { + // + // cout << "6+"; + // + //} else if (percentage >= 80) { + // + // cout << "6"; + // + //}else if (percentage >= 70) { + // + // cout << "5"; + // + //}else if (percentage >= 60) { + // + // cout << "4"; + // + //}else if (percentage >= 40) { + // + // cout << "3"; + // + //}else { + // + // cout << "2"; + // + //} + // + // //Zad_5 + //int a, b, c; + //cin >> a >> b >> c; + // + //if ((a + b > c) && (b + c > a) && (a + c > b)) { + // + // cout << "VALID"; + //} + //else + //{ + // cout << "NOT VALID"; + //} + + //// Zad_6 + //int trafficLightState; + //cout << "Enter traffic light state: \n"; + // + //cin >> trafficLightState; + // + //switch (trafficLightState) { + // + //case 1: cout << "Green. You can pass! "; break; + // + //case 0: cout << "Yellow. Warning!"; + // + //case -1: cout << "Red. You cannot pass!"; break; + //} + // + //// Zad_7 + //char symbol; + //cin >> symbol; + // + //if ((symbol >= 'A' && symbol <= 'Z')) { + // + // std::cout << (char)(symbol + 32)<= 'a' && symbol <= 'z')) { + // std::cout << (char)(symbol - 32) << std::endl; + //} + //else + //{ + // + // cout << "Invalid character"; + //} + + // Zad_8 + //int number; + + //cout << "Arabic numeral: \n"; + //cin >> number; + + //cout << "Roman numerals: \n"; + +//switch (number) { +// +//case 1: +// +// std::cout << "I" << std::endl; +// break; +// +//case 2: +// std::cout << "II" << std::endl; +// break; +//case 3: +// std::cout << "III" << std::endl; +// break; +//case 4: +// std::cout << "IV" << std::endl; +// break; +//case 5: +// std::cout << "V" << std::endl; +// break; +//case 6: +// std::cout << "VI" << std::endl; +// break; +//case 7: +// std::cout << "VII" << std::endl; +// break; +//case 8: +// std::cout << "VIII" << std::endl; +// break; +//case 9: +// std::cout << "IX" << std::endl; +// break; +// +// } + + // Zad_9 + int num1, num2, num3; + cin >> num1 >> num2 >> num3; + int temp; + if (num1 > num3) { + temp = num1; + num1 = num3; + num3 = temp; + } + if (num1 > num2) { + temp = num1; + num1 = num2; + num2 = temp; + } + if (num2 > num3) { + temp = num3; + num3 = num2; + num2 = temp; + } + + cout << num1 << " " << num2 << " " << num3; + + // Zad_10 + //double num1, num2; + //cin >> num1 >> num2; + + //char action; + + //cout << "Action: "; + //cin >> action; + + //if (action == '+') { + + // cout << num1 + num2; + + //}else if (action == '-') { + + // cout << num1 - num2; + + //}else if (action == '*') { + + // cout << num1 * num2; + + //}else if (action == '/') { + + // if (num2 == 0) { + + // cout << "undefined"; + + // } + // else + // { + + // cout << num1 / num2; + // } + //} + + // Zad_11 + //int num1, num2, num3; + //cin >> num1 >> num2 >> num3; + // + //int sum = num1 + num2 + num3; + //int maxNum = max(max(num1, num2), num3); + //int minNum = min(min(num1, num2), num3); + //int middle = sum - maxNum - minNum; + // + //cout << maxNum << middle << minNum; + + // Zad_12 + //int day, month; + + //cout << "Date: "; + //cin >> day >> month; + + //day++; + + //if (month % 2 == 0) { + + // if (month == 2) { + + // if (day > 28) { + + // day = 1; + // month++; + // } + + // } + // else + // { + // if (month == 12) { + + // if (day > 31) { + + // day = 1; + // month = 1; + // } + + + // }else if (day > 31) { + + // day = 1; + // month++; + // } + + // } + + //} + //else { + + // if (day > 30) { + + // day = 1; + // month = 1; + // } + + //} + + //cout << "Next date: " << day << " " << month; + + // Zad_13 + //int num1, num2, num3; + //cin >> num1 >> num2 >> num3; + + //int maxNum = ( (num1 > num2) ? ( (num1 > num3) ? num1 : num3 ) : ( (num2 > num3) ? num2 : num3) ); + //cout << "Max: " << maxNum; + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex1.cpp b/Seminar03/sem3_solutions/3_ex1.cpp new file mode 100644 index 0000000..cdfe16f --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex1.cpp @@ -0,0 +1,19 @@ + +#include +using namespace std; +int main() +{ + + int a; + std::cin >> a; + int factorial = 1; + + for (int i = a ; i > 1; i--) { + + factorial *= i; + } + + cout << "Factorial: " << factorial; + +} + diff --git a/Seminar03/sem3_solutions/3_ex10.cpp b/Seminar03/sem3_solutions/3_ex10.cpp new file mode 100644 index 0000000..6a18d13 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex10.cpp @@ -0,0 +1,18 @@ + +#include +using namespace std; +int main() +{ + +int n; std::cin >> n; +int sum_of_n = n * (n + 1) / 2; +for (int i = 1; i <= n - 1; i++) +{ + int num; std :: cin >> num; + sum_of_n -= num; +} +std::cout << sum_of_n; + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex11.cpp b/Seminar03/sem3_solutions/3_ex11.cpp new file mode 100644 index 0000000..5685e9d --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex11.cpp @@ -0,0 +1,36 @@ + +#include +using namespace std; +int main() +{ + +int number; +bool is_bad_number= true; + + while (is_bad_number) { + + std::cin >> number; + int save_number = number; + int sum = 0; + while (number != 0) { + + sum += number % 10; + number /= 10; + + } + + if ((save_number + sum) % 10 == 0) + { + std::cout << save_number << " -> End of program [ " << save_number << " + " << sum << " = " << (save_number + sum) << " which is divisible by 10 ]" << endl; + is_bad_number = false; + } + else { + + std::cout << save_number << " -> Bad number [ " << save_number << " + " << sum << " = " << (save_number + sum) << " is not divisible by 10 ]" << endl; + + } + + + } + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex12.cpp b/Seminar03/sem3_solutions/3_ex12.cpp new file mode 100644 index 0000000..1f29dfc --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex12.cpp @@ -0,0 +1,25 @@ + +#include +using namespace std; +int main() +{ + + + int n; std::cout << "n = "; std::cin >> n; +int sum = 0; + +for (int group = 1; group <= n; group++) +{ + for (int numbers_to_add = 1; numbers_to_add <= group; numbers_to_add++) + { + + sum += numbers_to_add; + + } + +} +std::cout << sum; + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex13.cpp b/Seminar03/sem3_solutions/3_ex13.cpp new file mode 100644 index 0000000..377e617 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex13.cpp @@ -0,0 +1,97 @@ + +#include +using namespace std; +int main() +{ + + + int first_hour, first_min; + cin >> first_hour >> first_min; + + int second_hour, second_min; + cin >> second_hour >> second_min; + + int early_hour, early_min; + int late_hour, late_min; + + if ((first_hour * 60) + first_min > (second_hour * 60) + second_min) { + late_hour = first_hour; + late_min = first_min; + + early_hour = second_hour; + early_min = second_min; + } + else + { + late_hour = second_hour; + late_min = second_min; + + early_hour = first_hour; + early_min = first_min; + } + + + + + + for (int hours = early_hour; hours <= late_hour; hours++) + { + + + if (hours == early_hour) { + for (int minutes = early_min; minutes < 60; minutes++) + { + + if (minutes >= 0 && minutes <= 9) + { + + std::cout << hours << ":"<< "0" << minutes << endl; + + } + else { + + std::cout << hours << ":" << minutes << endl; + + } + + } + } + else if (hours == late_hour) + { + for (int minutes = 0; minutes <= late_min; minutes++) + { + if (minutes >= 0 && minutes <= 9) + { + + std::cout << hours << ":" << "0" << minutes << endl; + + } + else { + + std::cout << hours << ":" << minutes << endl; + + } + } + } + else + { + for (int minutes = 0; minutes < 60; minutes++) + { + if (minutes >= 0 && minutes <= 9) + { + + std::cout << hours << ":" << "0" << minutes << endl; + + } + else { + + std::cout << hours << ":" << minutes << endl; + + } + } + } + + } + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex14.cpp b/Seminar03/sem3_solutions/3_ex14.cpp new file mode 100644 index 0000000..7995057 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex14.cpp @@ -0,0 +1,37 @@ + +#include +using namespace std; +int main() +{ + int n; std::cin >> n; +int input_num; std::cin >> input_num; +int sum = 0; +while (input_num != -1) { + + int count = 0; + + if (input_num > 1) { + + for (int i = 1; i <= input_num; i++) + { + if (input_num % i == 0) { + count++; + } + + + } + + if (count == 2 && n % input_num != 0) { + sum += input_num; + } + } + + + std::cin >> input_num; +} + +std::cout << sum; + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex15.cpp b/Seminar03/sem3_solutions/3_ex15.cpp new file mode 100644 index 0000000..3c20658 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex15.cpp @@ -0,0 +1,29 @@ + +#include +using namespace std; +int main() +{ + int n; cin >> n; + +for (int c = 1; c <= n; c++) +{ + for (int b = 1; b <= n; b++) + { + for (int a = 1; a <= n; a++) + { + + if (a + b > c && a + c > b && b + c > a) + { + + if (a < b && a * a + b * b == c * c) { + std::cout << a << " " << b << " " << c << endl; + } + + } + + } + } +} + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex16.cpp b/Seminar03/sem3_solutions/3_ex16.cpp new file mode 100644 index 0000000..feec0ba --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex16.cpp @@ -0,0 +1,28 @@ + +#include +using namespace std; +int main() +{ + + int m, n; cin >> m >> n; + +for (int number = m; number <= n; number++) +{ + int sum = 0; + for (int divisor = 1; divisor < number; divisor++) + { + if (number % divisor == 0) { + sum += divisor; + } + } + + if (sum == number) { + std::cout << number< +using namespace std; +int main() +{ + + int n; std::cin >> n; + if (n < 0) { + return 1; + } + int max_sum = 0; + int sum = 0; + for (int i = 0; i < n; i++) + { + int a, b; + std::cin >> a >> b; + int current_sum = a + b; + sum += current_sum; + if (current_sum > max_sum) { + max_sum = current_sum; + } + + } + if (sum / n == n) { + std::cout << "Equal sums: " << n; + + } + else + { + std::cout << "Different sums. Max Difference: " << max_sum; + } + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex18.cpp b/Seminar03/sem3_solutions/3_ex18.cpp new file mode 100644 index 0000000..fd718a8 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex18.cpp @@ -0,0 +1,46 @@ + +#include +using namespace std; +int main() +{ + + int k; std::cin >> k; +if (k < 0) { + return 1; +} + +int n, m; +std::cin >> n >> m; + +for (int number = n; number <= m; number++) +{ + bool is_div = true; + int num_for_use = number; + + while (is_div) { + + int last_digit = num_for_use % 10; + num_for_use /= 10; + + if (last_digit == 0 || k % last_digit != 0) + { + is_div = false; + break; + } + + if (num_for_use == 0) { + break; + } + + } + +if (is_div) { + std::cout << number << " "; + } + +} + + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex19.cpp b/Seminar03/sem3_solutions/3_ex19.cpp new file mode 100644 index 0000000..c9238b0 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex19.cpp @@ -0,0 +1,24 @@ + +#include +using namespace std; +int main() +{ + + int n;std:: cin >> n; + +if (n < 0) { + return 1; +} + +for (int row = 1; row <= n; row++) +{ + for (int col = 1; col <=row; col++) + { + std::cout << "$ "; + } + std::cout << endl; +} + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex2.cpp b/Seminar03/sem3_solutions/3_ex2.cpp new file mode 100644 index 0000000..8adcdb8 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex2.cpp @@ -0,0 +1,32 @@ + +#include +using namespace std; +int main() +{ + + int start, end; + + cout << "Start: " << endl; + cin >> start; + + cout << "End: " << endl; + cin >> end; + if (start >= 0 && start <= 255) { + if (end >= start) { + + while (start != (end + 1)) { + + char index = start; + + cout << start << " --> " << index << endl; + start++; + + } + + } + else { + cout << "Invalid code!"; + } + } + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex20.cpp b/Seminar03/sem3_solutions/3_ex20.cpp new file mode 100644 index 0000000..2c31b2e --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex20.cpp @@ -0,0 +1,77 @@ + +#include +using namespace std; +int main() +{ + + int n; std::cin >> n; +int width =5*n; + +//top part +for (int row = 1; row < n; row++) +{ + + for (int num = 1; num <= row; num++) + { + + std::cout << num << " "; + } + for (int space = 1; space <= (width - (row * 4)); space++) + { + std::cout << " "; + } + + for (int rev_num = row; rev_num >=1 ; rev_num--) + { + std::cout << " " << rev_num; + } + + std::cout << endl; + +} + +//middle part + +for (int num = 1; num <= n; num++) +{ + std::cout << num << " "; +} + +for (int space = 1; space <= n; space++) +{ + std::cout << "-"; +} + + +for (int rev_num = n; rev_num >= 1; rev_num--) +{ + std::cout << " " << rev_num; +} +std::cout << endl; + +//bottom part + +for (int row = n - 1; row >= 1; row--) +{ + + for (int num = 1; num <= row; num++) + { + + std::cout << num << " "; + } + for (int space = 1; space <= (width - (row * 4)); space++) + { + std::cout << " "; + } + + for (int rev_num = row; rev_num >= 1; rev_num--) + { + std::cout << " " << rev_num; + } + + std::cout << endl; + +} + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex21.cpp b/Seminar03/sem3_solutions/3_ex21.cpp new file mode 100644 index 0000000..657d0f1 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex21.cpp @@ -0,0 +1,106 @@ + +#include +using namespace std; +int main() +{ + int n; std::cin >> n; + +//top part + +for (int i = 1; i <= n+2; i++) +{ + std::cout << "#"; +} +std::cout << endl; + +int number = 0; +int divisor = 1; +for (int num = 1; num <= n; num++) +{ + divisor *= 10; + number *= 10; + number += num; + +} +divisor /= 10; + + +//first mid part +std::cout << "#" << number << "#" << endl; +for (int i = 1; i < n; i++) +{ + ; + int first_digit = number / divisor; + + number = (number % divisor) * 10 + first_digit; + std::cout << "#" << number << "#" << endl; +} + + +//sec mid part + +std::cout << "#"; +if (n % 2 == 0) +{ + for (int i = 1; i <= n; i++){ + + if (i == n / 2) + { + std::cout << "XX"; + i++; + } + else + { + std::cout << " "; + } + + } + +} +else +{ + + for (int i = 1; i <= n; i++) { + + if (i == (n / 2) + 1) + { + std::cout << "X"; + } + else + { + std::cout << " "; + } + + } + +} + + +std::cout << "#" << endl; + + + + +//third mid part +std::cout << "#" << number << "#" << endl; +for (int i = 1; i < n; i++) +{ + + int last_digit = number % 10; + number/=10; + number = divisor * last_digit + number; + std::cout << "#" << number << "#" << endl; +} + + + +//bottom +for (int i = 1; i <= n + 2; i++) +{ + std::cout << "#"; +} + + + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex3.cpp b/Seminar03/sem3_solutions/3_ex3.cpp new file mode 100644 index 0000000..8506149 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex3.cpp @@ -0,0 +1,20 @@ + +#include +using namespace std; +int main() +{ + + int n; cin >> n; + int x = 1; + + while (x != n) { + + + int y = n - x; + + cout << "x = " << x << ", y = " << y << endl; + + x++; + } + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex4.cpp b/Seminar03/sem3_solutions/3_ex4.cpp new file mode 100644 index 0000000..ebb8500 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex4.cpp @@ -0,0 +1,17 @@ + +#include +using namespace std; +int main() +{ + + long long number; cin >> number; + int sum = 0; + while (number != 0) { + + sum += (number % 10); + number /= 10; + + } + cout << "Sum:" << sum; + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex5.cpp b/Seminar03/sem3_solutions/3_ex5.cpp new file mode 100644 index 0000000..42d50c7 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex5.cpp @@ -0,0 +1,24 @@ + +#include +using namespace std; +int main() +{ + + + int number; + cin >> number; + number *= number; + int reversed = 0; + + + while (number != 0) { + + int last_digit = number % 10; + + reversed = reversed * 10 + last_digit; + number /= 10; + } + cout << "Reversed:" << reversed; + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex6.cpp b/Seminar03/sem3_solutions/3_ex6.cpp new file mode 100644 index 0000000..a8463a4 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex6.cpp @@ -0,0 +1,18 @@ + +#include +using namespace std; +int main() +{ + + int m, n; cin >> m >> n; + int num = 1; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + cout << num << " "; + num++; + } + cout << endl; + } + + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex7.cpp b/Seminar03/sem3_solutions/3_ex7.cpp new file mode 100644 index 0000000..4bc3780 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex7.cpp @@ -0,0 +1,22 @@ + +#include +using namespace std; +int main() +{ + + int sum = 0; +for (int start = 100; start <= 999; start++) { + int sum_of_digits = 0; + int number = start; + for (int i = 0; i < 3; i++) { + + sum_of_digits += number % 10; + number /= 10; + + } + + + +} + +} \ No newline at end of file diff --git a/Seminar03/sem3_solutions/3_ex8.cpp b/Seminar03/sem3_solutions/3_ex8.cpp new file mode 100644 index 0000000..03e99c0 --- /dev/null +++ b/Seminar03/sem3_solutions/3_ex8.cpp @@ -0,0 +1,19 @@ + +#include +using namespace std; +int main() +{ + + int numbers[1000] = {0,1}; + +for (int position = 2; position <= n - 1; position++) { + + numbers[position] = numbers[position - 1] + numbers[position - 2]; + +} + +for (int i =0 ; i +using namespace std; +int main() +{ +int low_end; std :: cin >> low_end; +int high_end; std :: cin >> high_end; +int sum_of_prime_numbers = 0; +if (low_end < high_end && low_end >= 1 && high_end > 1) +{ + + for (int posible_prime_num = low_end; posible_prime_num <= high_end; posible_prime_num++) { + + int count_of_divisions = 0; + + for (int i = 1; i <= posible_prime_num; i++) + { + if (posible_prime_num % i == 0) { + count_of_divisions++; + } + } + if (count_of_divisions == 2) { + sum_of_prime_numbers += posible_prime_num; + } + + + } + +} + +std::cout << "Sum of primes: " << sum_of_prime_numbers; + + +} \ No newline at end of file diff --git a/Seminar04/sem4_solutions/4_allEx.cpp b/Seminar04/sem4_solutions/4_allEx.cpp new file mode 100644 index 0000000..eff8bdf --- /dev/null +++ b/Seminar04/sem4_solutions/4_allEx.cpp @@ -0,0 +1,561 @@ + + +#include +using namespace std; + + +int sum(int a, int b); +bool isEven(int num); +bool isPositive(int num); +bool isNegative(int num); +int abs(int num); +double fabs(double num); +void print(char s, int n); +bool isCapitalLetter(char s); +bool checkSymbol(char s); +char to_lower(char ch); +char to_upper(char ch); +int num_length(int n); +int index_of_mun(int n, int k); +void has_sqrt(int number); +int least_common_multiple_of_3_numbers(int num1, int num2, int num3); +int concat(int first, int second); +int aksUser(int from, int to); +int pow(int num, unsigned n); +double calculate(int a, int b, char action); +int greatest_common_divisor_of_4_numbers(int a, int b, int c, int d); +int pow2_num_closest_to_n(int n); +bool is_prime_num(int number); +void print_prime_numbers(int to); +bool has_sum_of_primes(int number); +int sum_of_all_digit_until_1(long long number); +void multiplied_primes(int number); +int sorted_num(long long number); +int main() +{ + std::cout << "Ex1 test" << endl; + std::cout << sum(5, -23) << endl; + + std::cout << "Ex2 test" << endl; + std::cout << isEven(7) << endl; + std::cout << isEven(14) << endl; + + + std::cout << "Ex3 test" << endl; + std::cout << isPositive(0) << endl; + std::cout << isNegative(0) << endl; + std::cout << isPositive(15) << endl; + std::cout << isNegative(-20) << endl; + + std::cout << "Ex4 test" << endl; + std::cout << abs(13) << endl; + std::cout << abs(-130) << endl; + std::cout << fabs(15.923) << endl; + std::cout << fabs(-20.9842) << endl; + + std::cout << "Ex5 test" << endl; + print('d', 6); + print('n', 4); + + std::cout << "Ex6 test" << endl; + std::cout << isCapitalLetter('d') << endl;//0 + std::cout << isCapitalLetter('A') << endl;//1 + std::cout << checkSymbol('@') << endl;//0 + std::cout << checkSymbol('h') << endl;//1 + std::cout << checkSymbol('H') << endl;//1 + + std::cout << "Ex7 test" << endl; + std::cout << to_lower('h') << endl;//h + std::cout << to_lower('H') << endl;//h + std::cout << to_upper('h') << endl;//H + std::cout << to_upper('H') << endl;//H + + std::cout << "Ex8 test" << endl; + std::cout << num_length(13) << endl;//2 + std::cout << num_length(14621) << endl;//5 + + + + std::cout << "Ex9 test" << endl; + std::cout << index_of_mun(97672, 6) << endl;//Out-of-bounds + std::cout << index_of_mun(15, 1) << endl;//1 + std::cout << index_of_mun(972, 3) << endl;//2 + std::cout << index_of_mun(348, 2) << endl;//4 + + std::cout << "Ex10 test" << endl; + has_sqrt(25);//5 + has_sqrt(24);//? + has_sqrt(144);//12 + has_sqrt(3437316);//1854 + + std::cout << "Ex11 test" << endl; + std::cout << least_common_multiple_of_3_numbers(15, 21, 33) << endl; + std::cout << least_common_multiple_of_3_numbers(14, 8, 24) << endl; + + + + std::cout << "Ex12 test" << endl; + std::cout << concat(15, 21) << endl; + std::cout << concat(3, 1) << endl; + + std::cout << "Ex13 test" << endl; + //std::cout<> n; + while (n < 100 || n> 100000) { + std::cin >> n; + } + std::cout << pow2_num_closest_to_n(n) << endl; + + std::cout << "Ex18.1 test" << endl; + std::cout << is_prime_num(5) << endl; + std::cout << is_prime_num(7) << endl; + std::cout << is_prime_num(14) << endl; + std::cout << is_prime_num(17) << endl; + std::cout << is_prime_num(4) << endl; + + std::cout << "Ex18.2 test" << endl; + print_prime_numbers(20); + + std::cout << "Ex18.3 test" << endl; + std::cout << has_sum_of_primes(4) << endl; + std::cout << has_sum_of_primes(9) << endl; + std::cout << has_sum_of_primes(30) << endl; + + std::cout << "Ex19 test" << endl; + std::cout << sum_of_all_digit_until_1(999999999999) << endl; + + std::cout << "Ex20 test" << endl; + multiplied_primes(225); + multiplied_primes(31668); + + std::cout << "Ex21 test" << endl; + std::cout << sorted_num(836051241) << endl; + std::cout << sorted_num(9283821) << endl; + std::cout << sorted_num(6342) << endl; +} +//zad1 +int sum(int a, int b) { + + return (a + b); +} + +//zad2 +bool isEven(int num) { + + + return (num % 2 == 0) ? true : false; + +} + +//zad 3 +bool isPositive(int num) { + + return (num > 0) ? true : false; + +} + +bool isNegative(int num) { + return (num < 0) ? true : false; +} + +//zad4 +int abs(int num) { + return (num < 0) ? num * (-1) : num; +} + +double fabs(double num) { + return (num < 0) ? num * (-1) : num; +} + + +//zad5 + +void print(char s, int n) { + for (int i = 0; i < n; i++) + { + std::cout << s << " " << endl; + } +} + + +//zad6 + +bool isCapitalLetter(char s) { + + return (s >= 65 && s <= 90) ? true : false; + +} +bool checkSymbol(char s) { + + return ((s >= 65 && s <= 90) || (s >= 97 && s <= 122)) ? true : false; + +} + + +//zad7 +//грешно условие ?! +//to_lower логично би трябвало да прави главните букви малки,а +//to_upper обратното!!! + +char to_lower(char ch) { + return(ch >= 65 && ch <= 90) ? char(ch + 32) : ch; +} + +char to_upper(char ch) { + return(ch >= 97 && ch <= 122) ? char(ch - 32) : ch; +} + +//zad8 + +int num_length(int n) { + int length = 0; + + while (n != 0) { + n /= 10; + length++; + } + + return length; + +} + +//zad9 + +int index_of_mun(int n, int k) { + + int length = 0; + int divisor = 1; + int num = n; + while (num != 0) { + num /= 10; + length++; + divisor *= 10; + } + + if (length < k) { + std::cout << "Out of bounds"; + return 0; + } + else { + for (int position = 0; position <= length; position++) + { + int last_digit = n / divisor; + n %= divisor; + divisor /= 10; + + if (position == k) { + return last_digit; + } + } + } + + + + +} + + +//zad10 + +void has_sqrt(int number) { + int a = -1; + int num_save = number; + int counter = 0; + while (number > 0) { + + a += 2; + number -= a; + counter++; + + } + std::cout << num_save; + if (number == 0) { + std::cout << " - yes (" << counter << "^2)" << endl; + + } + else { + std::cout << " - no (?^2)" << endl; + + } + +} + + +//zad11 + +int least_common_multiple_of_3_numbers(int num1, int num2, int num3) { + + int lcm = 2; + + while (true) { + + if ((num1 % lcm == 0) && (num2 % lcm == 0) && (num3 % lcm == 0)) { + return lcm; + } + lcm++; + } + + + +} + + +//zad12 + +int concat(int first, int second) { + + int save_sec = second; + + while (save_sec != 0) { + save_sec /= 10; + first *= 10; + + } + + return first + second; + + +} + +//zad13 + +int aksUser(int from, int to) { + + int n; + std::cin >> n; + while (!(n >= from && n <= to)) { + + cin >> n; + + } + return n; +} + +//zad14 +int pow(int num, unsigned n) { + n--; + while (n != 0) { + num *= num; + n--; + } + return num; +} + +//zad15 + +double calculate(int a, int b, char action) { + + switch (action) + { + case'+': + default: + return a + b; + + case'-': + return a - b; + + case'*': + return a * b; + + case'/': + + return (double)a / b;; + + case'%': + return a % b; + + + } +} + + +//zad16 +int greatest_common_divisor_of_4_numbers(int a, int b, int c, int d) { + + int first = (a >= b) ? a : b; + int sec = (c >= d) ? c : d; + + int gcd = (first >= sec) ? first : sec; + + while (true) { + + if ((a % gcd == 0) && (b % gcd == 0) && (c % gcd == 0) && (d % gcd == 0)) { + return gcd; + } + gcd--; + } + + + +} + +//zad17 + +int pow2_num_closest_to_n(int n) { + + int high_pow_num = 1; + + while (high_pow_num < n) { + high_pow_num *= 2; + } + int low_pow_num = high_pow_num / 2; + + + if (n - low_pow_num < high_pow_num - n) { + return low_pow_num; + } + else + { + return high_pow_num; + } + + +} + + +//zad18 +//a) +bool is_prime_num(int number) { + int count = 0; + for (int divisor = 1; divisor <= number; divisor++) + { + if (number % divisor == 0) + { + count++; + } + } + + if (count == 2) { + return true; + } + else + { + return false; + } + +} + +//b) + +void print_prime_numbers(int to) { + + for (int i = 1; i <= to; i++) + { + if (is_prime_num(i)) + { + std::cout << i << endl; + } + } + +} + +//c) + +bool has_sum_of_primes(int number) { + + for (int i = 1; i <= number; i++) + { + if (is_prime_num(i)) + { + if (is_prime_num(number - i)) + { + cout << i << " + " << number - i << " = " << number << endl; + return true; + } + } + } + return false; +} + +//zad19 + +int sum_of_all_digit_until_1(long long number) { + +LOOP_START: + int sum = 0; + while (number != 0) { + + int last_gigit = number % 10; + sum += last_gigit; + number /= 10; + + } + number = sum; + if (number > 9) { + goto LOOP_START; + } + else + { + return number; + } + + +} + +//zad20 +void multiplied_primes(int number) { + + int i = 2; + while (number != 1) { + REPEAT: + if (number % i == 0) + { + number /= i; + std::cout << i; + if (number == 1) + { + break; + } + else { + std::cout << "."; + } + + goto REPEAT; + } + else + { + i++; + } + + } + std::cout << endl; +} + +//zad21 +int sorted_num(long long number) { + int new_num = 0; + + for (int digit = 0; digit < 9; digit++) + { + for (int temp = number; temp > 0; temp /= 10) + { + if (temp % 10 == digit) + { + new_num *= 10; + new_num += digit; + } + } + } + + return new_num; + + +}