第四周作业——图的表示

代码:

  1. package 邻接链表;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.FileReader;  
  8. import java.io.IOException;  
  9. import java.io.OutputStreamWriter;  
  10.   
  11.   
  12. public class GraphRepresentation {  
  13.   
  14.     public static void main(String[] args) {  
  15.         try {  
  16.             FileReader in = new FileReader("F:\\tinyG.txt");  
  17.             BufferedReader bl = new BufferedReader(in);  
  18.             int tempchar;  
  19.             String str = "";  
  20.             str = bl.readLine();  
  21.             int x = Integer.parseInt(str);  
  22.             str = bl.readLine();  
  23.             int E = Integer.parseInt(str);  
  24.             int[][] g = new int[x][x];  
  25.             str = "";  
  26.             int a = 0, b = 0;  
  27.             while ((tempchar = bl.read()) != -1)  
  28.             {  
  29.                 str = str + (char) tempchar;  
  30.                 if ((char) tempchar == ' ')   
  31.                 {  
  32.                     a = Integer.parseInt(str.trim());  
  33.                     str = "";  
  34.                 }  
  35.                 if ((char) tempchar == '\n')  
  36.                 {  
  37.                     b = Integer.parseInt(str.trim());  
  38.                     g[a][b] = 1;  
  39.                     g[b][a] = 1;  
  40.                     str = "";  
  41.                 }  
  42.             }  
  43.               
  44.             bl.close();  
  45.             in.close();  
  46.               
  47.             FileOutputStream fos = new FileOutputStream("tinyG_matrix.txt");  
  48.             OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312");  
  49.             BufferedWriter bw = new BufferedWriter(osw);  
  50.             str="";  
  51.             System.out.println("得  到  图  的  邻  接  矩  阵  为 : ");  
  52.             for (int i = 0; i < x; i++)   
  53.             {  
  54.                 for (int j = 0; j < x; j++)   
  55.                 {  
  56.                     System.out.print(g[i][j]+"   ");  
  57.                     str=str+Integer.toString(g[i][j]);  
  58.                     if(j==12){  
  59.                         System.out.println();  
  60.                         bw.write(str);  
  61.                         bw.newLine();  
  62.                         str="";  
  63.                     }  
  64.                 }  
  65.             }  
  66.               
  67.             bw.close();  
  68.             osw.close();  
  69.               
  70.         }  
  71.         catch (NumberFormatException e)  
  72.         {  
  73.             e.printStackTrace();  
  74.         }   
  75.         catch (FileNotFoundException e)  
  76.         {  
  77.             e.printStackTrace();  
  78.         }  
  79.         catch (IOException e)  
  80.         {  
  81.             e.printStackTrace();  
  82.         }  
  83.           
  84.     }         
  85. }
  86.   



运行结果:



结果保存到tinyG_matrix.txt中,如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值