Advertisement
cd62131

input graph

May 14th, 2019
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int *graph; /*一次元配列graph*/
  5. int n;      /* 頂点の数 */
  6. #define graph_at(x, y) graph[((x)-1) * ((x)) / 2 + (y)]
  7. void inputgraph(void) /* グラフデータ読込み */
  8. {
  9.   puts("頂点の数を入力してください");
  10.   if (scanf("%d*[^\n]", &n) != 1) {
  11.     n = 0;
  12.     return;
  13.   }
  14.   graph = calloc(n * (n - 1) / 2, sizeof(*graph));
  15.   for (int i = 0; i < n * (n - 1) / 2; ++i) {
  16.     graph[i] = false;
  17.   }
  18.   puts("各辺の両端点を小さい順に番号で入力してください");
  19.   {
  20.     int x, y;
  21.     while (scanf("%d%d*[^\n]", &y, &x) == 2) {
  22.       graph_at(x, y) = true;
  23.     }
  24.   }
  25. }
  26. int main(void) {
  27.   inputgraph(); /* 点の数 n, 辺の有無 graph[] を入力 */
  28.   puts("入力データ graph(i,j)=F/T");
  29.   for (int i = 0; i < n; ++i) {
  30.     printf("%3d", i);
  31.     for (int j = 0; j < i; ++j) {
  32.       printf(" %c", (graph_at(i, j) ? 'T' : 'F'));
  33.     }
  34.     puts("");
  35.   }
  36.   fputs("   ", stdout);
  37.   for (int i = 0; i < n; ++i)
  38.     printf("%2d", i);
  39.   puts("");
  40.   free(graph);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement