任务描述
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
编程要求
输入
多组数据,每组数据m+1行。第一行有两个数字n和m,代表有n个人和m组朋友关系。n个人的编号为1到n。第二行到第m+1行每行包括两个数字a和b,代表这两个人互相认识。当n和m都等于0时,输入结束。
输出
每组数据输出n行,对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
测试说明
平台会对你编写的代码进行测试:
测试输入:
10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
9 10
0 0
预期输出:
1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
1: 70.00%
2: 80.00%
3: 80.00%
4: 80.00%
5: 80.00%
6: 80.00%
7: 80.00%
8: 70.00%
9: 20.00%
10: 20.00%
#include <iostream>
#include <iomanip>
#define MAXSIZE 100
#define INFINITE 36577
using namespace std;
void CreateUDG(int G[][MAXSIZE], int m, int n)
{//使用连接短速度阵列的方法创建
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
G[i][j]=INFINITE; //原始区域中各个时间段的授权值太长,是否将设备存储在行中的单个点
int a, b;
for (int i=0;i<m;i++) { //存储位置被视为封闭组件。存储位置的测试值为负1,存储位置被看作封闭组件
cin>>a>>b;
G[a-1][b-1]=1;
G[b-1][a-1]=1;
}
}
int FindMinPath(bool S[], int D[], int n)
{//选择其中一个条件的当前最短期限,终点为n
int min=INFINITE;
int index=-1;
for (int i=0;i<n;i++) {
if (!S[i]&&D[i]<min) {
min=D[i];
index=i;
}
}
return index;
}
float SixDegree_DIJ(int G[][MAXSIZE], int n, int start)
{//通过这个“新的开始”,我们可以计算法律系统中六个空时间点的数量,而开始是交换机指定时间点的第一级
/**************begin************/
/**************end************/
}
int main()
{
int n,m;
while (true) {
cin>>n>>m;
if (n==0&&m==0) break;
int G[MAXSIZE][MAXSIZE]={ 0 };
CreateUDG(G,m,n);
for (int i=0;i<n;i++) { //首次验证n年的中央时间管理后
cout<<i+1<<": "<<fixed<<setprecision(2)<<SixDegree_DIJ(G,n,i)*100<<"%"<<endl;
}
}
return 0;
}
**
#include <stdio.h>
#include <stdlib.h>
struct queue{
int head;
int tail;
int data[1000];
};
struct stack{
int top;
int data[10];
};
int main()
{
int i,t;
struct queue q1,q2;
struct stack s;
int book[10];
q1.head=1;q1.tail=1;
q2.head=1;q2.tail=1;
s.top=0;
for(i=1;i<=9;i++)
book[i]=0;
for(i=1;i<=6;i++){
scanf("%d",&q1.data[q1.tail]);
q1.tail++;
}
for(i=1;i<=6;i++){
scanf("%d",&q2.data[q2.tail]);
q2.tail++;
}
while(q1.head<q1.tail&&q2.head<q2.tail)
{
t=q1.data[q1.head];
if(book[t]==0){
q1.head++;//q1出牌,即出队列,头指针+1
s.top++;//桌面上的牌为栈,仅有头指针操作
s.data[s.top]=t;
book[t]=1;
}
else{
q1.head++;
q1.data[q1.tail]=t;
q1.tail++;
while(s.data[s.top]!=t)
{
book[s.data[s.top]]=0;
q1.data[q1.tail]=s.data[s.top];
q1.tail++;
s.top--;
}
}
t=q2.data[q2.head];
if(book[t]==0)
{
q2.head++;
s.top++;
s.data[s.top]=t;
book[t]=1;
}
else
{
q2.head++;
q2.data[q2.tail]=t;
q2.tail++;
while(s.data[s.top]!=t)
{
q2.data[q2.tail]=s.data[s.top];
book[s.data[s.top]]=0;
q2.tail++;
s.top--;
}
}
}
if(q2.head==q2.tail)
{
printf("q1 win\n");
for(i=q1.head;i<q1.tail;i++)//tail始终指向结尾的下一个位置
printf("%d ",q1.data[i]);
}
else
{
printf("q2 win\n");
for(i=q2.head;i<q2.tail;i++)
printf("%d ",q2.data[i]);
}
return 0;
}