L2-009 抢红包(Java)

没有人没抢过红包吧…… 这里给出N个人之间互相发红包、抢红包的记录,请你统计一下他们抢红包的收获。

输入格式

输入第一行给出一个正整数N(≤104),即参与发红包和抢红包的总人数,则这些人从1到N编号。随后N行,第i行给出编号为i的人发红包的记录,格式如下:
KN1P1⋯NKPK
其中K(0≤K≤20)是发出去的红包个数,Ni是抢到红包的人的编号,Pi(>0)是其抢到的红包金额(以分为单位)。注意:对于同一个人发出的红包,每人最多只能抢1次,不能重复抢。

输出格式

按照收入金额从高到低的递减顺序输出每个人的编号和收入金额(以元为单位,输出小数点后2位)。每个人的信息占一行,两数字间有1个空格。如果收入金额有并列,则按抢到红包的个数递减输出;如果还有并列,则按个人编号递增输出。

输入样例

10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10

输出样例

1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32

解题思路

1. 定义一个类来表示每个人,这个类包含编号、收入总额和抢到的红包数。
2. 读取输入数据,更新每个人的收入和抢到的红包数。
3. 排序:根据收入金额、抢到红包的个数和编号排序。
4. 格式化输出结果。

解题过程中遇到的问题

当时我的第一版代码是这样的,money定义为double。然后排序逻辑也是没问题,但是最终的结果就是2和8的编号不能正确输出,我就很纳闷。结果就是如下图所看到的情况!!!

import java.util.*;

class Person implements Comparable<Person> {
    int id;
    double money;
    int redPackets;

    public Person(int id) {
        this.id = id;
        this.money = 0.0;
        this.redPackets = 0;
    }

    @Override
    public int compareTo(Person o) {
        if (this.money != o.money) {
            return Double.compare(o.money, this.money);
        } else if (this.redPackets != o.redPackets) {
            return o.redPackets - this.redPackets;
        } else {
            return this.id - o.id;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        Person[] persons = new Person[N + 1];
        for (int i = 1; i <= N; i++) {
            persons[i] = new Person(i);
        }
        
        for (int i = 1; i <= N; i++) {
            int K = scanner.nextInt();
            for (int j = 0; j < K; j++) {
                int recipient = scanner.nextInt();
                int amount = scanner.nextInt();
                persons[recipient].money += amount / 100.0;
                persons[recipient].redPackets++;
                persons[i].money -= amount / 100.0;
            }
        }
        
        Arrays.sort(persons, 1, N + 1);
        
        for (int i = 1; i <= N; i++) {
            System.out.printf("%d %.2f\n", persons[i].id, persons[i].money);
        }
    }
}

解题代码

import java.util.*;
import java.math.BigDecimal;

class Person implements Comparable<Person> {
    int id;
    BigDecimal money;
    int redPackets;

    public Person(int id) {
        this.id = id;
        this.money = BigDecimal.ZERO;
        this.redPackets = 0;
    }

    @Override
    public int compareTo(Person o) {
        int cmp = this.money.compareTo(o.money);
        if (cmp != 0) {
            return -cmp; // 收入金额递减排序
        } else if (this.redPackets != o.redPackets) {
            return o.redPackets - this.redPackets;// 抢到红包个数递减排序
        } else {
            return this.id - o.id;// 个人编号递增排序
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        Person[] persons = new Person[N + 1];
        for (int i = 1; i <= N; i++) {
            persons[i] = new Person(i);
        }

        for (int i = 1; i <= N; i++) {
            int K = scanner.nextInt();
            for (int j = 0; j < K; j++) {
                int recipient = scanner.nextInt();
                int amount = scanner.nextInt();
                BigDecimal amountBD = BigDecimal.valueOf(amount, 2);
                persons[recipient].money = persons[recipient].money.add(amountBD);
                persons[recipient].redPackets++;
                persons[i].money = persons[i].money.subtract(amountBD);
            }
        }

        Arrays.sort(persons, 1, N + 1);

        for (int i = 1; i <= N; i++) {
            System.out.printf("%d %.2f\n", persons[i].id, persons[i].money.doubleValue());
        }
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值