L2-007 家庭房产python/java

给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。

输入格式:

输入第一行给出一个正整数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

java代码,pta运行超时但是是对的

import java.io.*;
import java.util.*;

class Family {
    int id, cnt;
    double avgs, avga;

    public Family(int id, int cnt, int avgs, int avga) {
        this.id = id;
        this.cnt = cnt;
        this.avgs = 1.0*avgs;
        this.avga = 1.0*avga/cnt;
    }

    public double getAvga() {
        return this.avga;
    }

    public int getId() {
        return this.id;
    }
}

public class Main {
    static int n = 0;
    static int co = 0;
    static int N = 10001;
    static Object[] e = new Object[N];  // Object数组可以存储元组(id, father),每个元素是一个长度为2的数组
    static boolean[] st = new boolean[N];
    static int[] fa = new int[N];
    static int[] peo = new int[N];
    static int[] avgs = new int[N];
    static int[] avga = new int[N];

    public static void main(String[] args) throws IOException {
        StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        n = nextInt(tokenizer);
        for (int i = 0; i < n; i++) {
            int id = nextInt(tokenizer);
            int father = nextInt(tokenizer);
            int mother = nextInt(tokenizer);
            int k = nextInt(tokenizer);
            if (father != -1) {
                e[co++] = new int[]{id, father};
            }
            if (mother != -1) {
                e[co++] = new int[]{id, mother};
            }
            st[id] = true;
            for (int j = 0; j < k; j++) {
                int kid = nextInt(tokenizer);
                e[co++] = new int[]{id, kid};
            }
            avgs[id] = nextInt(tokenizer);
            avga[id] = nextInt(tokenizer);
        }

        // 初始化并查集
        for (int i = 0; i < N; i++) {
            fa[i] = i;
            peo[i] = 1;
        }

        // 处理并查集
        for (int i = 0; i < co; i++) {
            int[] edge = (int[]) e[i];
            int a = edge[0];
            int b = edge[1];
            st[a] = st[b] = true;
            union(a, b);
        }

        // 构建Family对象
        List<Family> ans = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            if (st[i] && fa[i] == i) {
                ans.add(new Family(i, peo[i], avgs[i], avga[i]));
            }
        }

        // 按照排序规则对Family对象列表进行排序
        ans.sort(Comparator.comparing(Family::getAvga).reversed().thenComparing(Family::getId));

        // 输出结果
        System.out.println(ans.size());
        for (Family t : ans) {
            System.out.printf("%04d %d %.3f %.3f%n", t.id, t.cnt, t.avgs / t.cnt, t.avga);
        }
    }

    static int find(int x) {
        return x == fa[x] ? x : (fa[x] = find(fa[x]));
    }

    static void union(int a, int b) {
        int faa = find(a);
        int fbb = find(b);
        if (faa != fbb) {
            fa[Math.max(faa, fbb)] = Math.min(faa, fbb);
            peo[Math.min(faa, fbb)] += peo[Math.max(faa, fbb)];
            avgs[Math.min(faa, fbb)] += avgs[Math.max(faa, fbb)];
            avga[Math.min(faa, fbb)] += avga[Math.max(faa, fbb)];
        }
    }

    static int nextInt(StreamTokenizer tokenizer) throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}

python

class Family:
    def __init__(self, id, cnt, avgs, avga):
        self.id = id
        self.cnt = cnt
        self.avgs = avgs
        self.avga = avga

n = 0
co = 0
N = 10001
e = [None] * N
st = [False] * N
fa = list(range(N))
peo = [1] * N
avgs = [0] * N
avga = [0] * N

def find(x):
    return x if fa[x] == x else find(fa[x])

def unionf(a, b):
    faa = find(a)
    fbb = find(b)
    if faa != fbb:
        fa[max(faa, fbb)] = min(faa, fbb)
        peo[min(faa, fbb)] += peo[max(faa, fbb)]
        avgs[min(faa, fbb)] += avgs[max(faa, fbb)]
        avga[min(faa, fbb)] += avga[max(faa, fbb)]

# 读取输入
n = int(input())
for i in range(n):
    line = input().split()
    id = int(line[0])
    father = int(line[1])
    mother = int(line[2])
    k = int(line[3])
    if father != -1:
        e[co] = (id, father)
        co += 1
    if mother != -1:
        e[co] = (id, mother)
        co += 1
    st[id] = True
    for j in range(k):
        kid = int(line[4 + j])
        e[co] = (id, kid)
        co += 1
    avgs[id] = int(line[4 + k])
    avga[id] = int(line[5 + k])

# 处理并查集
for i in range(co):
    a, b = e[i]
    st[a] = st[b] = True
    unionf(a, b)

# 构建Family对象
ans = []
for i in range(N):
    if st[i] and fa[i] == i:
        ans.append(Family(i, peo[i], avgs[i], avga[i]/peo[i]))

# 定义排序规则


# 按照排序规则对Family对象列表进行排序
ans.sort(key=lambda x: (-x.avga, x.id))

# 输出结果
print(len(ans))
for t in ans:
    print(f"{t.id:04d} {t.cnt} {t.avgs/t.cnt:.3f} {t.avga:.3f}")

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值