Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
8 files changed, 393 insertions, 3 deletions
-rw-r--r--Sem. 03/README.md2
-rw-r--r--Sem. 03/drawing_examples.txt35
-rw-r--r--Sem. 03/solutions/exercise 15.cpp58
-rw-r--r--Sem. 04/README.md2
-rw-r--r--Sem. 04/example01_with_functions.cpp7
-rw-r--r--Sem. 05_kn/BAndC.cpp115
-rw-r--r--Sem. 05_kn/draw58
-rw-r--r--Sem. 05_kn/refferences.cpp119
diff --git a/Sem. 03/README.md b/Sem. 03/README.md
index dae7155..35624d4 100644
--- a/Sem. 03/README.md
+++ b/Sem. 03/README.md
@@ -160,5 +160,7 @@ do
+**Задача 15:** Да се напише програма, която приема цяло положително число n и отпечатва следната папионка:
+![enter image description here](https://i.ibb.co/NKSPm4z/ddz.png)
diff --git a/Sem. 03/drawing_examples.txt b/Sem. 03/drawing_examples.txt
index d8fad73..73e3130 100644
--- a/Sem. 03/drawing_examples.txt
+++ b/Sem. 03/drawing_examples.txt
@@ -120,6 +120,13 @@ $ $ $ $
. . . . .
. . . . . .
+вход: 5
+. . . . . . . . . .
+. . . .
+. . . . . . .
+. . . .
+. . . . . . . . . .
+
@@ -153,3 +160,31 @@ $ $ $ $
1
121
1
+
+задача 15:
+
+вход: 3
+
+1 1
+1 2 2 1
+1 2 3 --- 3 2 1
+1 2 2 1
+1 1
+
+вход: 4
+1 1
+1 2 2 1
+1 2 3 3 2 1
+1 2 3 4 ---- 4 3 2 1
+1 2 3 3 2 1
+1 2 2 1
+1 1
+
+
+вход: 2
+
+1 1
+1 2 –- 2 1
+1 1
+
+
diff --git a/Sem. 03/solutions/exercise 15.cpp b/Sem. 03/solutions/exercise 15.cpp
new file mode 100644
index 0000000..652a201
--- /dev/null
+++ b/Sem. 03/solutions/exercise 15.cpp
@@ -0,0 +1,58 @@
+#include <iostream>
+using namespace std;
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ int spacesCount = 2 * (n - 1) + n + 2 * (n - 1);
+ int toGuess;
+ for (int i = 1; i <= n - 1; i++)
+ {
+ for (int j = 1; j <= i; j++)
+ cout << j << " ";
+
+ for (int j = 0; j < spacesCount; j++)
+ cout << " ";
+
+ for (int j = i; j >= 1; j--)
+ cout << " " << j;
+
+ spacesCount -= 4;
+
+ cout << endl;
+
+ }
+
+ //mid
+ for (int i = 1; i <= n; i++)
+ cout << i << " ";
+ for (int i = 0; i < n; i++)
+ cout << '-';
+ for (int i = n; i >= 1; i--)
+ cout << " " << i;
+ cout << endl;
+
+
+ //down
+ spacesCount += 4;
+ for (int i = n - 1; i >= 0; i--)
+ {
+ for (int j = 1; j <= i; j++)
+ cout << j << " ";
+
+ for (int j = 0; j < spacesCount; j++)
+ cout << " ";
+
+ for (int j = i; j >= 1; j--)
+ cout << " " << j;
+
+ spacesCount += 4;
+
+ cout << endl;
+
+ }
+
+
+}
diff --git a/Sem. 04/README.md b/Sem. 04/README.md
index 1d5fcb8..8e8acb0 100644
--- a/Sem. 04/README.md
+++ b/Sem. 04/README.md
@@ -99,7 +99,7 @@ int main()
**Задача 2:** Напишете функция, която приема 4 цели числа - координати на 2 точки. Функцията да връща разстоянието между тези точки.
-*Вход: 1 1 4 3, Изход: 6.403*
+*Вход: 1 1 4 3, Изход: 3,605*
**Задача 3:** Напишете функция, която приема 6 целочислени координати (3 точки) и връща обиколката на триъгълника по тези точки.
diff --git a/Sem. 04/example01_with_functions.cpp b/Sem. 04/example01_with_functions.cpp
index f33b6b9..64d4367 100644
--- a/Sem. 04/example01_with_functions.cpp
+++ b/Sem. 04/example01_with_functions.cpp
@@ -1,8 +1,11 @@
#include <iostream>
using namespace std;
-bool isPrime(int n)
+bool isPrime(unsigned n)
{
+ if(n <= 1)
+ return false;
+
double temp = sqrt(n);
for (int i = 2; i <= temp; i++)
@@ -24,4 +27,4 @@ int main()
if (isPrime(i - 1) && isPrime(i + 1))
cout << i - 1 << " " << i + 1 << endl;
}
-} \ No newline at end of file
+}
diff --git a/Sem. 05_kn/BAndC.cpp b/Sem. 05_kn/BAndC.cpp
new file mode 100644
index 0000000..30f8a3d
--- /dev/null
+++ b/Sem. 05_kn/BAndC.cpp
@@ -0,0 +1,115 @@
+#include <iostream>
+using namespace std;
+
+
+const int DIGITS_COUNT = 4;
+
+int getNumLen(int n)
+{
+ int count = 0;
+ while (n != 0)
+ {
+ count++;
+ n /= 10;
+ }
+ return count;
+}
+
+bool containsDigit(int n, int digit)
+{
+ while (n != 0)
+ {
+ int lastDigit = n % 10;
+
+ if (digit == lastDigit)
+ return true;
+
+ n /= 10;
+ }
+ return false;
+}
+bool containsDiffrentDigits(int n)
+{
+ while (n >= 10)
+ {
+ int lastDigit = n % 10;
+ n /= 10;
+
+ if (containsDigit(n, lastDigit))
+ return false;
+ }
+ return true;
+}
+
+bool isValid(int n)
+{
+ return getNumLen(n) == DIGITS_COUNT && containsDiffrentDigits(n);
+}
+
+void userInput(int& toGuess)
+{
+ do
+ {
+ cin >> toGuess;
+ } while (!isValid(toGuess));
+}
+
+int getBulls(int toGuess, int myGuess)
+{
+ int bulls = 0;
+ while (toGuess != 0)
+ {
+ int toGuessLastDigit = toGuess % 10;
+ int myGuessLastDigit = myGuess % 10;
+
+ if (toGuessLastDigit == myGuessLastDigit)
+ bulls++;
+
+ toGuess /= 10;
+ myGuess /= 10;
+ }
+ return bulls;
+}
+
+int getCows(int toGuess, int myGuess, int bulls)
+{
+ int cows = 0;
+ while (myGuess != 0)
+ {
+ int lastDigit = myGuess % 10;
+
+ if (containsDigit(toGuess, lastDigit))
+ cows++;
+
+ myGuess /= 10;
+ }
+ return cows - bulls;
+}
+
+void getBullsAndCows(int toGuess, int myGuess, int& bulls, int& cows)
+{
+ bulls = getBulls(toGuess, myGuess);
+ cows = getCows(toGuess, myGuess, bulls);
+}
+
+int main()
+{
+ int toGuess;
+ userInput(toGuess);
+
+ cout << "Try to guess:" << endl;
+
+ int bulls = 0;
+ int cows = 0;
+
+ while (bulls != DIGITS_COUNT)
+ {
+ int myGuess;
+ userInput(myGuess);
+ getBullsAndCows(toGuess, myGuess, bulls, cows);
+
+ cout << "Bulls: " << bulls << " " << ", Cows: " << cows << endl;
+ }
+
+ cout << "Success!" << endl;
+}
diff --git a/Sem. 05_kn/draw b/Sem. 05_kn/draw
new file mode 100644
index 0000000..652a201
--- /dev/null
+++ b/Sem. 05_kn/draw
@@ -0,0 +1,58 @@
+#include <iostream>
+using namespace std;
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ int spacesCount = 2 * (n - 1) + n + 2 * (n - 1);
+ int toGuess;
+ for (int i = 1; i <= n - 1; i++)
+ {
+ for (int j = 1; j <= i; j++)
+ cout << j << " ";
+
+ for (int j = 0; j < spacesCount; j++)
+ cout << " ";
+
+ for (int j = i; j >= 1; j--)
+ cout << " " << j;
+
+ spacesCount -= 4;
+
+ cout << endl;
+
+ }
+
+ //mid
+ for (int i = 1; i <= n; i++)
+ cout << i << " ";
+ for (int i = 0; i < n; i++)
+ cout << '-';
+ for (int i = n; i >= 1; i--)
+ cout << " " << i;
+ cout << endl;
+
+
+ //down
+ spacesCount += 4;
+ for (int i = n - 1; i >= 0; i--)
+ {
+ for (int j = 1; j <= i; j++)
+ cout << j << " ";
+
+ for (int j = 0; j < spacesCount; j++)
+ cout << " ";
+
+ for (int j = i; j >= 1; j--)
+ cout << " " << j;
+
+ spacesCount += 4;
+
+ cout << endl;
+
+ }
+
+
+}
diff --git a/Sem. 05_kn/refferences.cpp b/Sem. 05_kn/refferences.cpp
new file mode 100644
index 0000000..47e4fba
--- /dev/null
+++ b/Sem. 05_kn/refferences.cpp
@@ -0,0 +1,119 @@
+#include <iostream>
+using namespace std;
+
+
+int f(int n)
+{
+ n++;
+ return n;
+}
+
+// lvalue ("връщане на променлива")
+// rvalue ("връщане на константа")
+
+//Референция - друго име за някоя променлива
+
+// Пример:
+// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра.
+
+void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit)
+{
+ int max = 0;
+ int min = 9;
+
+ while (n != 0)
+ {
+ int lastDigit = n % 10;
+
+ if (lastDigit > max)
+ max = lastDigit;
+
+ if (lastDigit < min)
+ min = lastDigit;
+
+ n /= 10;
+ }
+ maxDigit = max;
+ minDigit = min;
+
+}
+
+//!
+
+int& f(int& n) //--няма да върне копие.
+{
+ n++;
+ return n;
+}
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ int max, min;
+
+ getMaxAndMinDigit(n, max, min);
+ cout << "Min digit " << min << " " << "max digit " << max << endl;
+}
+
+
+
+
+
+#include <iostream>
+using namespace std;
+
+// lvalue ("връщане на променлива")
+// rvalue ("връщане на константа")
+
+//Референция - друго име за някоя променлива
+
+//Пример:
+// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра.
+
+void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit)
+{
+ int max = 0;
+ int min = 9;
+
+ while (n != 0)
+ {
+ int lastDigit = n % 10;
+
+ if (lastDigit > max)
+ max = lastDigit;
+
+ if (lastDigit < min)
+ min = lastDigit;
+
+ n /= 10;
+ }
+ maxDigit = max;
+ minDigit = min;
+
+}
+
+
+int f1(int& n) //-връщат константа (rvalue)
+{
+ n++;
+ return n;
+}
+int& f2(int& n) //--връщат променлива (lvalue).
+{
+ n++;
+ return n;
+}
+///!!!!!!!!!!! error
+int& f3()
+{
+ int a = 10;
+ return a;
+}
+int main()
+{
+ cout << ++f();
+
+
+}