给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数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 <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#define MAX 10000
using namespace std;
class Record{
public:
int minPeopleId;
int peopleSum;
double avgHourse;
double avgArea;
public:
Record(){
this->minPeopleId=MAX;
this->peopleSum=0;
this->avgArea=0;
this->avgHourse=0;
}
};
class People{
public:
int id;
int mother;
int father;
vector<int> sonOrdaughter;
int sonOrdaughterNum;
int hourse;
double area;
public:
People(){
this->id = -1;
this->mother = -1;
this->father = -1;
this->sonOrdaughterNum = 0;
this->hourse = 0;
this->area = 0;
}
People(int id,int mother,int father,vector<int> sonOrdaughter
,int sonOrdaughterNum,int hourse,double area){
this->id = id;
this->mother = mother;
this->father = father;
this->sonOrdaughter = sonOrdaughter;
this->sonOrdaughterNum = sonOrdaughterNum;
this->hourse = hourse;
this->area = area;
}
};
People* peoples[MAX];
bool isVisit[MAX];
Record* record[MAX];
int recordIndex=-1;
void visitPeople(People*people);
bool dealRecord(Record *front,Record*behind);
int main(){
int n,i,id,mother,father,k,j,temp,hourse;
float area;
// 获取数据
scanf("%d",&n);
int* read = new int[n];
for(i=0; i<n; i++){
scanf("%d %d %d %d",&id,&mother,&father,&k);
read[i]=id;
vector<int> sonOrdaughter;
for(j=0; j<k; j++){
scanf("%d",&temp);
sonOrdaughter.push_back(temp);
}
scanf("%d %f",&hourse,&area);
peoples[id] =new People(id, mother, father, sonOrdaughter, k, hourse, area);
}
// 建立关系
for(i=0; i<n; i++){
mother = peoples[read[i]]->mother;
if(mother!=-1){
if(peoples[mother]==NULL){
peoples[mother] = new People();
peoples[mother]->sonOrdaughter.push_back(read[i]);
peoples[mother]->id = mother;
peoples[mother]->sonOrdaughterNum = 1;
}else{
vector<int> &sonOrdaughter = peoples[mother]->sonOrdaughter;
vector<int>::iterator it = find(sonOrdaughter.begin(),sonOrdaughter.end()
,read[i]);
if(it==sonOrdaughter.end()){
sonOrdaughter.push_back(read[i]);
peoples[mother]->sonOrdaughterNum=peoples[mother]->sonOrdaughterNum+1;
}
}
}
father = peoples[read[i]]->father;
if(father!=-1){
if(peoples[father]==NULL){
peoples[father] = new People();
peoples[father]->sonOrdaughter.push_back(read[i]);
peoples[father]->id = father;
peoples[father]->sonOrdaughterNum = 1;
}else{
vector<int> &sonOrdaughter = peoples[father]->sonOrdaughter;
vector<int>::iterator it = find(sonOrdaughter.begin(),sonOrdaughter.end()
,read[i]);
if(it==sonOrdaughter.end()){
sonOrdaughter.push_back(read[i]);
peoples[father]->sonOrdaughterNum=peoples[father]->sonOrdaughterNum+1;
}
}
}
vector<int> &sonOrdaughter = peoples[read[i]]->sonOrdaughter;
int &sonOrdaughterSum = peoples[read[i]]->sonOrdaughterNum;
for(j=0; j<sonOrdaughterSum; j++){
if(peoples[sonOrdaughter[j]]==NULL){
peoples[sonOrdaughter[j]] = new People();
peoples[sonOrdaughter[j]]->id=sonOrdaughter[j];
peoples[sonOrdaughter[j]]->mother = read[i];
}else if(peoples[sonOrdaughter[j]]->father==-1){
peoples[sonOrdaughter[j]]->father = read[i];
}
}
}
// 访问家族人员
for(i=0; i<n; i++){
if(isVisit[read[i]]==false){
recordIndex++;
record[recordIndex]=new Record();
visitPeople(peoples[read[i]]);
record[recordIndex]->avgArea /= record[recordIndex]->peopleSum;
record[recordIndex]->avgHourse /= record[recordIndex]->peopleSum;
}
}
recordIndex++;
// 整理记录
sort(record,record+recordIndex,dealRecord);
// 提交记录
printf("%d\n",recordIndex);
for(i=0; i<recordIndex; i++){
printf("%04d %d %.3f %.3f\n",record[i]->minPeopleId,record[i]->peopleSum
,record[i]->avgHourse,record[i]->avgArea);
}
return 0;
}
void visitPeople(People*people){
isVisit[people->id]=true;
if(record[recordIndex]->minPeopleId>people->id){
record[recordIndex]->minPeopleId=people->id;
}
record[recordIndex]->peopleSum+=1;
record[recordIndex]->avgArea+=people->area;
record[recordIndex]->avgHourse+=people->hourse;
// 访问母亲
if(people->mother!=-1 && isVisit[people->mother]==false){
visitPeople(peoples[people->mother]);
}
// 访问父亲
if(people->father!=-1 && isVisit[people->father]==false){
visitPeople(peoples[people->father]);
}
// 访问儿子或女儿
vector<int> &sonOrdaughter = people->sonOrdaughter;
int i;
for(i=0; i<people->sonOrdaughterNum; i++){
if(isVisit[sonOrdaughter[i]]==false){
visitPeople(peoples[sonOrdaughter[i]]);
}
}
}
bool dealRecord(Record *front,Record*behind){
if(front->avgArea > behind->avgArea){
return true;
}else if(front->avgArea == behind->avgArea
&& front->minPeopleId < behind->minPeopleId){
return true;
}else{
return false;
}
}