本文主要是介绍六度空间(C++),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
题目:
输入、输出格式:
代码:
运行结果:
题目:
输入、输出格式:
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int g[10005][10005];
float n, k;
typedef struct node
{int data;int w = 0;
}node;
void warshall()//传递闭包
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){if (g[i][k] && g[k][j])//连通{if (i == j)continue;if (g[i][j] == 0 || g[i][j] > g[i][k] + g[k][j])/没有直接连通或者新通路距离小于之前通路g[i][j] = g[i][k] + g[k][j];}}
}
bool cmp(node a, node b)
{return a.w < b.w;
}
int main()
{cin >> n >> k;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)g[i][j] = 0;for (int i = 0; i < k; i++){int v1, v2;cin >> v1 >> v2;g[v1][v2] = 1;g[v2][v1] = 1;}warshall();/*for (int i = 1; i <= n; i++)//输出邻接矩阵{for (int j = 1; j <= n; j++)cout << g[i][j] << " ";cout << endl;}*/float ans[10005] ;for (int i = 1; i <= n; i++){ans[i] = 0;for (int j = 1; j <= n; j++){if (i == j)ans[i]++;if (g[i][j]>0 && g[i][j] <= 6)//符合条件ans[i]++;}}for (int i = 1; i <= n; i++)printf("%d:% .2f%%\n", i, ans[i] / n * 100);
}
运行结果:
这篇关于六度空间(C++)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!