请教一个C语言的数据结构编程的题目,高手进!

来源:百度知道 编辑:UC知道 时间:2024/09/21 21:57:20
设置二维数组A[6][6]表示6节点无向图的邻接矩阵,编写程序,从键盘上输入邻接矩阵的数据,求出该无向图的边数以及各节点的度,输出所求结果。

请写出程序,必要的的在右边解释一下,谢谢!

#include <stdio.h>

int A[6][6];
int i,j,sides=0,degree[6];
int main() {

for(i=0;i<6;i++) for(j=0;j<6;j++){
scanf("%d",&A[i][j]);

if(A[i][j]){
sides++;
degree[i]++;
degree[j]++;
}
}

sides/=2;
printf("total number of sides is %d \n",sides);
for(i=0;i<6;i++) {
degree[i]/=2;
printf("degree of vertex %d is %d \n",i,degree[i]);
}

return 0;
}

数据:
0 1 1 0 1 0
1 0 1 1 0 1
1 1 0 0 1 1
0 1 0 0 1 0
1 0 1 1 0 1
0 1 1 0 1 0

输出:
total number of sides is 10
degree of vertex 0 is 3
degree of vertex 1 is 4
degree of vertex 2 is 4
degree of vertex 3 is 2
degree of vertex 4 is 4
degree of vertex 5 is 3

说明:
A[i][j]=0 代表 i节点与j节点无连接,因为是无向的, A[j][i]一定=0.
A[i][j]=1时亦然.

因此 A[i][j]图一定是