提交时间:2024-08-21 17:58:03

运行 ID: 180728

#include <iostream> using namespace std; #include <cstdio> void rela(int n){ int people[n][n]; // 初始化关系矩阵,自己的位置填入-1,其余为0 for(int i=0;i<n;++i){ for (int j = 0; j < n; ++j){ if (i==j){ people[i][j] = -1; } else{ people[i][j] = 0; }}} // 将输入的有朋友关系的位置改为数值1 while(1){ int a = 0, b =-2; scanf(",(%d,%d)", &a, &b); if (b == -2)break; else { people[a - 1][b - 1] = 1; people[b - 1][a - 1] = 1; }} // 判断关系 for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { //位置为0的地方有朋友的朋友和其他情况 if (people[i][j]==0){ //筛选出朋友的朋友并输出结果 for (int k = 0; k < n; ++k) { if (people[i][k] == people[j][k]){ cout << "(" << i + 1 << "," << j + 1 << ")"; break; }}}}} // cout << endl; // for (int i = 0; i < n; ++i){ // for (int j = 0; j < n; ++j) cout <<people[i][j] << "\t"; // cout << endl;} } int main() { int x; cin >> x; rela(x); return 0; }