zoukankan      html  css  js  c++  java
  • L2-009. 抢红包


    L2-009. 抢红包

    时间限制
    300 ms
    内存限制
    65536 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    陈越

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

    输入格式:

    输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数,则这些人从1到N编号。随后N行,第i行给出编号为i的人发红包的记录,格式如下:

    K N1 P1 ... NK PK

    其中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

    这题目用java写最后一个测试点会超时,用c++可以过。

    通过代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    class person {
    public:
    	int money, num, count;
    } p[10001];
    
    bool compare(person a, person b)
    {
    	if (a.money != b.money)
    	{
    		return a.money > b.money;
    	}
    	if (a.count != b.count)
    	{
    		return a.count > b.count;
    	}
    	return a.num < b.num;
    }
    int main()
    {
    	
    	int n;
    	scanf("%d", &n);
    	for (int i = 1; i <= n; ++i)
    	{
    		p[i].num = i; // 这里必须初始化,有人可能只发没抢到,这样导致最后排序出来有编号0的收益情况,就是那个只发没抢到的人
    	} // 当然放在后面扣除发的钱的时候再初始化编号也可以
    	for (int i = 1; i <= n; ++i)
    	{
    		int t; 
    		scanf("%d", &t);
    		int sum = 0;
    		for (int j = 0; j < t; ++j)
    		{
    			int bianhao, money;
    			scanf("%d %d", &bianhao, &money);
    			sum += money;
    			p[bianhao].money += money;
    			p[bianhao].num = bianhao;
    			++p[bianhao].count;
    		}
    		p[i].money -= sum; // 也可以在这里给编号初始化
    	}
    	sort(p+1, p+n+1, compare);
    	for (int i = 1; i <= n; ++i)
    	{
    		printf("%d %.2f
    ", p[i].num, p[i].money*1.0 / 100);
    	}
    	return 0;
    }



    接下来发一下我用java的列表写的,但是最后一个点超时,语言特性导致的。

    import java.io.BufferedInputStream;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Scanner;
    
    class Person {
        int num, money, count;
        public Person(int num) {
            super();
            this.num = num;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            List<Person> list = new ArrayList<Person>();
            Scanner cin = new Scanner(new BufferedInputStream(System.in));
            int n = cin.nextInt();
            for (int i = 0; i < n; ++i) {
                list.add(new Person(i));
            }
            for (int i = 0; i < n; ++i) {
                int k = cin.nextInt();
                int sum = 0;
                for (int j = 0; j < k; ++j) {
                    int bianhao = cin.nextInt() - 1;
                    int money = cin.nextInt();
                    sum += money;
                    Person p = list.get(bianhao);
                    ++p.count;
                    p.money += money;
                }
                list.get(i).money -= sum;
            }
            cin.close();
            Collections.sort(list, new Comparator<Person>() {
                @Override
                public int compare(Person o1, Person o2) {
                    if (o1.money != o2.money) {
                        return o2.money - o1.money;
                    }
                    if (o1.count != o2.count) {
                        return o2.count - o1.count;
                    }
                    return o1.num - o2.num;
                }
            });
            for (int i = 0; i < n; ++i) {
                Person p = list.get(i);
                System.out.print((p.num + 1) + " ");
                System.out.printf("%.2f
    ", p.money * 1.0 / 100);
            }
        }
    }

    接下来发一下用java对象数组写的,ArrayList里面实现和对象数组一样,仍然是最后一个测试点超时

    import java.io.BufferedInputStream;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    
    class Person {
        int num, money, count;
        public Person(int num) {
            super();
            this.num = num;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(new BufferedInputStream(System.in));
            int n = cin.nextInt();
            Person[] p = new Person[n];
            for (int i = 0; i < n; ++i) {
                p[i] = new Person(i);
            }
            for (int i = 0; i < n; ++i) {
                int k = cin.nextInt();
                int sum = 0;
                for (int j = 0; j < k; ++j) {
                    int bianhao = cin.nextInt() - 1;
                    int money = cin.nextInt();
                    sum += money;
                    p[bianhao].money += money;
                    ++p[bianhao].count;
                }
                p[i].money -= sum;
            }
            cin.close();
            Arrays.sort(p, new Comparator<Person>() {
                @Override
                public int compare(Person o1, Person o2) {
                    if (o1.money != o2.money) {
                        return o2.money - o1.money;
                    }
                    if (o1.count != o2.count) {
                        return o2.count - o1.count;
                    }
                    return o1.num - o2.num;
                }
            });
            for (int i = 0; i < n; ++i) {
                System.out.print((p[i].num + 1) + " ");
                System.out.printf("%.2f
    ", p[i].money * 1.0 / 100);
            }
        }
    }

    ========================================Talk is cheap, show me the code=======================================


    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    链表问题
    有关动态规划的LeetCode
    正则表达式匹配与自动机
    基于数据库范式的一点点想法
    html5 API
    Web鼠标事件
    webpack模块化原理
    Jquery数据缓存
    图片懒加载
    MySql存储过程的调试
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179786.html
Copyright © 2011-2022 走看看