PTA L2-009 抢红包 (25 分) C(gcc)实现(详细分析)

题目要求

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

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

KN​1​​P​1​​⋯N​K​​P​K​​

其中K(0≤K≤20)是发出去的红包个数,N​i​​是抢到红包的人的编号,P​i​​(>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

主要算法

  1. 定义People结构体
struct People
{
   int serial;
   int receiveCount;
   int money;
};
  1. 动态创建People数组并初始化
struct People *CreatePeopleArray(int num)
{
  return (struct People *)malloc(sizeof(struct People) * num);
}

void MemsetPeopleArray(struct People *ptr, int num, int money)
{
  for (int i = 0; i < num; i++)
  {
      ptr[i].money = money;
      ptr[i].receiveCount = 0;
      ptr[i].serial = i + 1;
  }
}
  1. 排序及其子函数
void SwapPeople(struct People *people, int i)
{
  struct People tempPeople = people[i];
  people[i] = people[i + 1];
  people[i + 1] = tempPeople;
}

void SortPeople(struct People *people, int num)
{
  bool flag = true;
  while (flag)
  {
      flag = false;
      for (int i = 0; i < num - 1; i++)
      {
          if (people[i].money < people[i + 1].money)
          {
              SwapPeople(people, i);
              flag = true;
          }
          if (people[i].money == people[i + 1].money)
          {
              if (people[i].receiveCount < people[i + 1].receiveCount)
              {
                  SwapPeople(people, i);
                  flag = true;
              }
              else if (people[i].receiveCount == people[i + 1].receiveCount)
              {
                  if (people[i].serial > people[i + 1].serial)
                  {
                      SwapPeople(people, i);
                      flag = true;
                  }
              }
          }
      }
  }
}
  1. 输入处理
void ReadInput(struct People *peoplePtr, int peopleCount)
{
   for (int i = 0; i < peopleCount; i++)
   {
       int receiveCount;
       int sendMoney = 0;
       scanf("%d", &receiveCount);
       for (int j = 0; j < receiveCount; j++)
       {
           int peopleSerial, receiveMoney;
           scanf("%d %d", &peopleSerial, &receiveMoney);
           peoplePtr[peopleSerial - 1].money += receiveMoney;
           peoplePtr[peopleSerial - 1].receiveCount += 1;
           sendMoney += receiveMoney;
       }
       peoplePtr[i].money -= sendMoney;
   }
}
  1. 主函数与输出
int main()
{
   int peopleCount;
   scanf("%d", &peopleCount);
   struct People *peoplePtr = CreatePeopleArray(peopleCount);
   MemsetPeopleArray(peoplePtr, peopleCount, 0);
   ReadInput(peoplePtr,peopleCount);
   SortPeople(peoplePtr, peopleCount);
   for (int i = 0; i < peopleCount; i++)
   {
       printf("%d %.2lf\n", peoplePtr[i].serial, peoplePtr[i].money / 100.0);
   }
}

完整实现代码

//By MadLongTom
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct People
{
    int serial;
    int receiveCount;
    int money;
};

struct People *CreatePeopleArray(int num)
{
    return (struct People *)malloc(sizeof(struct People) * num);
}

void MemsetPeopleArray(struct People *ptr, int num, int money)
{
    for (int i = 0; i < num; i++)
    {
        ptr[i].money = money;
        ptr[i].receiveCount = 0;
        ptr[i].serial = i + 1;
    }
}

void SwapPeople(struct People *people, int i)
{
    struct People tempPeople = people[i];
    people[i] = people[i + 1];
    people[i + 1] = tempPeople;
}

void SortPeople(struct People *people, int num)
{
    bool flag = true;
    while (flag)
    {
        flag = false;
        for (int i = 0; i < num - 1; i++)
        {
            if (people[i].money < people[i + 1].money)
            {
                SwapPeople(people, i);
                flag = true;
            }
            if (people[i].money == people[i + 1].money)
            {
                if (people[i].receiveCount < people[i + 1].receiveCount)
                {
                    SwapPeople(people, i);
                    flag = true;
                }
                else if (people[i].receiveCount == people[i + 1].receiveCount)
                {
                    if (people[i].serial > people[i + 1].serial)
                    {
                        SwapPeople(people, i);
                        flag = true;
                    }
                }
            }
        }
    }
}

void ReadInput(struct People *peoplePtr, int peopleCount)
{
    for (int i = 0; i < peopleCount; i++)
    {
        int receiveCount;
        int sendMoney = 0;
        scanf("%d", &receiveCount);
        for (int j = 0; j < receiveCount; j++)
        {
            int peopleSerial, receiveMoney;
            scanf("%d %d", &peopleSerial, &receiveMoney);
            peoplePtr[peopleSerial - 1].money += receiveMoney;
            peoplePtr[peopleSerial - 1].receiveCount += 1;
            sendMoney += receiveMoney;
        }
        peoplePtr[i].money -= sendMoney;
    }
}

int main()
{
    int peopleCount;
    scanf("%d", &peopleCount);
    struct People *peoplePtr = CreatePeopleArray(peopleCount);
    MemsetPeopleArray(peoplePtr, peopleCount, 0);
    ReadInput(peoplePtr,peopleCount);
    SortPeople(peoplePtr, peopleCount);
    for (int i = 0; i < peopleCount; i++)
    {
        printf("%d %.2lf\n", peoplePtr[i].serial, peoplePtr[i].money / 100.0);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值