c++难题,求大神写出代码。(需要重点防护的城市的定义使去掉这座城市会...
发布网友
发布时间:2024-04-06 00:01
我来回答
共2个回答
热心网友
时间:2024-08-08 04:55
之前写过一篇博客(话说是OUC OnlineJudge的吗? 斜眼笑.jpg)网页链接
#include <iostream>
using namespace std;
int *Links(int **city, int numsSize) //这个函数里新增了一个数组,用来储存 哪个城市需要重点防护
{
static int *a = new int[numsSize + 1];
for (int i = 0; i < numsSize + 1; i++)
a[i] = 0;
int temp;
for (int i = 1; i < numsSize + 1; i++)
{
int counts = 0;
for (int j = 1; j < numsSize + 1; j++)
{
if (city[i][j] == 1)
{
counts++;
temp = j;
}
}
if (counts > 1)
continue;
else
{
a[temp] = 1;
}
}
return a;
}
int CountDangerCitys(int *link, int numsSize) //数一下 link里新建的那个数组里需要重点保护城市的个数
{
int counts = 0;
for (int i = 1; i < numsSize + 1; i++)
if (link[i] == 1)
counts++;
return counts;
}
int CountCitys(int numsSize) //这里用来输入城市的信息
{
int **City = new int *[numsSize + 1];
for (int i = 0; i < numsSize + 1; i++)
City[i] = new int[numsSize + 1];
for (int i = 0; i < numsSize + 1; i++)
for (int j = 0; j < numsSize + 1; j++)
City[i][j] = 0;
////////////////
int head_num;
bool Ishead = true;
while (true)
//这个就是记录城市连接信息 的那个表
{
char temp;
temp = cin.get();
if (temp == '\n')
{
Ishead = true;
continue;
}
if (temp > 48 && temp <= 57)
{
if (!Ishead)
{
City[head_num][temp - 48] = 1;
City[temp - 48][head_num] = 1;
continue;
}
else if (Ishead)
{
head_num = temp - 48;
Ishead = false;
}
}
if (temp == '0')
break;
}
int *link = Links(City, numsSize);
//调用函数 往上翻
int result = CountDangerCitys(link, numsSize);
return result;
}
int main()
{
int n = 1;
int count = 0;
//int a[100];
//int t = 0;
while (n != 0)
{
cin >> n;
if (n != 0)
count++;
getchar();
if (n == 0)
break;
int result;
result = CountCitys(n);
cout << result << "\n";
//a[t] = CountCitys(n);
//t++;
}
//for (int i = 0; i < count; i++)
// cout << a[i] << "\n";
return 0;
}
热心网友
时间:2024-08-08 04:59
双连通分量缩点,求桥