L2-007 家庭房产 (25 分)
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数N(≤1000),随后N行,每行按下列格式给出一个人的房产:
编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积
其中编号
是每个人独有的一个4位数的编号;父
和母
分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1
);k
(0≤k
≤5)是该人的子女的个数;孩子i
是其子女的编号。
输出格式:
首先在第一行输出家庭个数(所有有亲属关系的人都属于同一个家庭)。随后按下列格式输出每个家庭的信息:
家庭成员的最小编号 家庭人口数 人均房产套数 人均房产面积
其中人均值要求保留小数点后3位。家庭信息首先按人均面积降序输出,若有并列,则按成员编号的升序输出。
输入样例:
10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100
输出样例:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
#include<iostream>
#include<map>
#include<set>
using namespace std;
struct person {
int house = 0, area = 0;
set<int> parent, child;
};
struct family {
int minid, num;
double xhouse, xarea;
bool operator<(const family &other)const {
if (xarea != other.xarea)
return xarea > other.xarea;
return minid < other.minid;
}
}Now; //Now用来进行操作
map<int, person> people;
set<family> result;
int search(int id) {
if (id != -1 && people.find(id) != people.end()) {
person tmp = people[id]; //记录当前结点
people.erase(id); //去除当前结点,防止重复访问
for (const int &it : tmp.parent) //访问父母
search(it);
for (const int &it : tmp.child) //访问孩子
search(it);
Now.num++; //家庭人数+1
Now.xhouse += tmp.house; //房产套数求和
Now.xarea += tmp.area; //房产面积求和
return 1;
}
return 0;
}
int main() {
int n, id, father, mother, kid, k;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d%d", &father, &mother, &k);
people[id].parent.insert(father); people[id].parent.insert(mother);
if (father != -1)
people[father].child.insert(id); //父母与子女进行连接
if (mother != -1)
people[mother].child.insert(id);
for (int j = 0; j < k; j++) {
scanf("%d", &kid);
people[id].child.insert(kid); //父母与子女进行连接
people[kid].parent.insert(id);
}
scanf("%d%d", &people[id].house, &people[id].area);
}
while (!people.empty()) {
Now = {}; //重置家庭数据
Now.minid = people.begin()->first; //初始化
search(Now.minid);
Now.xarea /= Now.num; Now.xhouse /= Now.num; //求均值
result.insert(Now);
}
printf("%d\n", result.size());
for (auto &it : result)
printf("%04d %d %0.3lf %0.3lf\n", it.minid, it.num, it.xhouse, it.xarea);
return 0;
}
结构优化:
#include<iostream>
#include<map>
#include<set>
#include<vector>
using namespace std;
struct person {
int house = 0, area = 0;
vector<int> folk;
};
struct family {
int minid, num;
double xhouse, xarea;
bool operator<(const family &other)const {
if (xarea != other.xarea)
return xarea > other.xarea;
return minid < other.minid;
}
}Now; //Now用来进行操作
map<int, person> people;
set<family> result;
void search(int id) {
if (id != -1 && people.find(id) != people.end()) {
person tmp = people[id]; //记录当前结点
people.erase(id); //去除当前结点,防止重复访问
for (const int &it : tmp.folk) //访问亲属
search(it);
Now.num++; //家庭人数+1
Now.xhouse += tmp.house; //房产套数求和
Now.xarea += tmp.area; //房产面积求和
}
}
int main() {
int n, id, father, mother, kid, k;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d%d", &father, &mother, &k);
people[id].folk.push_back(father); people[id].folk.push_back(mother);
if (father != -1)
people[father].folk.push_back(id); //父母与子女进行连接
if (mother != -1)
people[mother].folk.push_back(id);
for (int j = 0; j < k; j++) {
scanf("%d", &kid);
people[id].folk.push_back(kid); //父母与子女进行连接
people[kid].folk.push_back(id);
}
scanf("%d%d", &people[id].house, &people[id].area);
}
while (!people.empty()) {
Now = {}; //重置家庭数据
Now.minid = people.begin()->first; //初始化
search(Now.minid);
Now.xarea /= Now.num; Now.xhouse /= Now.num; //求均值
result.insert(Now);
}
printf("%d\n", result.size());
for (auto &it : result)
printf("%04d %d %0.3lf %0.3lf\n", it.minid, it.num, it.xhouse, it.xarea);
return 0;
}