diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-03-30 09:43:35 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-03-30 09:43:35 +0400 |
| commit | 5e6dc29068bf980bacd9c5421341c66fabc838ac (patch) | |
| tree | 32e791ece6dfd73591e91f7fb0f023317cf1a5c5 /graphs | |
| parent | 85dcaa072faeadc318ec717caa29d1cb94bddd59 (diff) | |
Исправил третью задачу и функцию вывода графа
Diffstat (limited to 'graphs')
| -rw-r--r-- | graphs/Makefile | 4 | ||||
| -rw-r--r-- | graphs/task1.cpp | 13 | ||||
| -rw-r--r-- | graphs/task2.cpp | 13 | ||||
| -rw-r--r-- | 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<iostream> #include<stdio.h> #include<vector> +#include<algorithm> 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; |