From 5e6dc29068bf980bacd9c5421341c66fabc838ac Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Tue, 30 Mar 2021 09:43:35 +0400 Subject: =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D1=8C=D1=8E=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=D1=83=20=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=84=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphs/Makefile | 4 ++-- graphs/task1.cpp | 13 +++++++++---- graphs/task2.cpp | 13 +++++++++---- graphs/task3.cpp | 27 +++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/graphs/Makefile b/graphs/Makefile index 11c6460..d20a34b 100644 --- a/graphs/Makefile +++ b/graphs/Makefile @@ -27,8 +27,8 @@ test3: task3 task4: $(COMPILE) -o task.out task4.cpp test4: task4 - @printf "" | ./task.out - @printf "Answer: \n" + @printf "4\n5\n1 2\n1 3\n1 4\n2 3\n3 4" | ./task.out + @printf "Answer: Связный\n" task5: $(COMPILE) -o task.out task5.cpp diff --git a/graphs/task1.cpp b/graphs/task1.cpp index 62806ae..1fea795 100644 --- a/graphs/task1.cpp +++ b/graphs/task1.cpp @@ -10,11 +10,16 @@ void print(graph g) for (int i = 0; i < int(g.size()); ++i) { cout << i + 1 << ": "; - for (auto node : g[i]) - cout << node + 1 << ", "; - cout << endl; - } + for (int j = 0; j < int(g[i].size()) - 1; ++j) + cout << g[i][j] + 1 << ", "; + + if (g[i].size() > 0) + cout << g[i].back() + 1; + else + cout << "нет смежных вершин"; + cout << ";" << endl; + } } int main() diff --git a/graphs/task2.cpp b/graphs/task2.cpp index 17dcdbe..0213be0 100644 --- a/graphs/task2.cpp +++ b/graphs/task2.cpp @@ -10,11 +10,16 @@ void print(graph g) for (int i = 0; i < int(g.size()); ++i) { cout << i + 1 << ": "; - for (auto node : g[i]) - cout << node + 1 << ", "; - cout << endl; - } + for (int j = 0; j < int(g[i].size()) - 1; ++j) + cout << g[i][j] + 1 << ", "; + + if (g[i].size() > 0) + cout << g[i].back() + 1; + else + cout << "нет смежных вершин"; + cout << ";" << endl; + } } int main() diff --git a/graphs/task3.cpp b/graphs/task3.cpp index 01f3307..1b4aab6 100644 --- a/graphs/task3.cpp +++ b/graphs/task3.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; @@ -11,11 +12,28 @@ void print(graph g) for (int i = 0; i < int(g.size()); ++i) { cout << i + 1 << ": "; - for (auto node : g[i]) - cout << node + 1 << ", "; - cout << endl; + for (int j = 0; j < int(g[i].size()) - 1; ++j) + cout << g[i][j] + 1 << ", "; + + if (g[i].size() > 0) + cout << g[i].back() + 1; + else + cout << "нет смежных вершин"; + + cout << ";" << endl; } +} +float calc_deg(int x, graph g) +{ + int deg_m = int(g[x].size()); + int deg_p = 0; + for (int i = 0; i < int(g.size()); ++i) + { + if (find(g[i].begin(), g[i].end(), x) != g[i].end()) + ++deg_p; + } + return (deg_m + deg_p) / 2.0f; } int main() @@ -50,7 +68,8 @@ int main() cout << "Степени каждой из вершин:" << endl; for (int i = 0; i < int(g.size()); ++i) { - printf("d(%i) = %i\n", i + 1, int(g[i].size())); + float deg = calc_deg(i, g); + printf("d(%i) = %f\n", i + 1, deg); } return 0; -- cgit v1.2.3