fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define N_MAX 100
  5.  
  6.  
  7. int main( void )
  8. {
  9. int N; // 点の数
  10. int M; // 辺の数
  11. int adjacent[N_MAX][N_MAX]; // 隣接行列 (N_MAX × N_MAXの配列)
  12. FILE *fp; // ファイルポインタ
  13.  
  14. int i, j, v1, v2;
  15.  
  16. // 配列の初期化
  17. for ( i=0; i<N_MAX; i++ )
  18. for ( j=0; j<N_MAX; j++ )
  19. adjacent[i][j] = 0;
  20.  
  21.  
  22. // graph1.txtから辺行列を読み込む
  23.  
  24. // ファイルオープン
  25. if ( (fp = fopen( "graph1.txt", "r" )) == NULL ) {
  26. printf( "ファイルが開けません.\n" );
  27. exit( 1 );
  28. }
  29.  
  30. // 点の数を読み込む
  31. // fscanf( ファイルポインタ, "書式指定子", 変数のアドレス );
  32. fscanf( fp, "%d", &N );
  33.  
  34. if ( N >= N_MAX ) {
  35. fprintf( stderr, "配列容量オーバーです\n" );
  36. exit( 1 );
  37. }
  38.  
  39. printf( "点の数: %d\n", N );
  40.  
  41. // 辺行列を読み込む
  42. M = 0;
  43. while( fscanf( fp, "%d %d", &v1, &v2 ) != EOF ) {
  44. M++;
  45.  
  46. // 辺がある場合には隣接行列を1にする
  47. adjacent[v1][v2] = adjacent[v2][v1] = 1;
  48. }
  49.  
  50. printf( "辺の数: %d\n", M );
  51.  
  52. for ( i=0; i<N; i++ )
  53. for ( j=i; j<N; j++ )
  54. if ( adjacent[i][j] )
  55. printf( "%d - %d\n", i, j );
  56.  
  57. // ファイルポインタのクローズ
  58. fclose( fp );
  59.  
  60. return 0;
  61. }
  62.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty