dizballanze

Untitled

Mar 1st, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. const int SIZE_LIMIT = 16;
  7. ofstream output;
  8.  
  9. void matrix_display(bool graph[SIZE_LIMIT][SIZE_LIMIT], int nodes_count)
  10. {
  11.     for (int i = 0; i < nodes_count; i++) {
  12.         for (int j = 0; j < nodes_count; j++) {
  13.             output << graph[i][j] << ' ';
  14.         }
  15.         output << endl;
  16.     }
  17. }
  18.  
  19. /**
  20. * Получить список вершин, у которых число входов больше чем число выходов
  21. */
  22. void show_filtred_nodes(bool graph[SIZE_LIMIT][SIZE_LIMIT], int nodes_count){
  23.     int in_count, out_count;
  24.     output << "--------------------------------------------------------------" << endl;
  25.     output << "Список вершин, у которых число входов больше чем число выходов" << endl;
  26.     for (int i = 0; i < nodes_count; i++) {
  27.         in_count = out_count = 0;
  28.         for (int j = 0; j < nodes_count; j++) {
  29.             if (graph[i][j])
  30.                 out_count++;
  31.         }
  32.  
  33.         for (int j = 0; j < nodes_count; j++) {
  34.             if (graph[j][i])
  35.                 in_count++;
  36.         }
  37.         if (in_count > out_count){
  38.             output << (i + 1) << endl;
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.     ifstream input("input.in");
  47.     bool graph[SIZE_LIMIT][SIZE_LIMIT] = {0};
  48.     output.open("output.out");
  49.    
  50.     int from, to, max_node=0;
  51.     while (input >> from >> to) {
  52.         if (max_node < to)
  53.             max_node = to;
  54.         if (max_node < from)
  55.             max_node = from;
  56.         if ((SIZE_LIMIT < from) || (SIZE_LIMIT < to)){
  57.             output << "Количество вершин графа должно быть не более " << SIZE_LIMIT << endl;
  58.             exit(0);
  59.         }
  60.         graph[from-1][to-1] = 1;
  61.     }
  62.     input.close();
  63.     output << "--------------------------------------------------------------" << endl;
  64.     output << "Количество вершин: " << max_node << endl;
  65.     output << "--------------------------------------------------------------" << endl;
  66.     output << "Матрица смежности:" << endl;
  67.     matrix_display(graph, max_node);
  68.     show_filtred_nodes(graph, max_node);
  69.     output.close();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment