C language qsort sorting L2-009

Topic`

No one hasn't grabbed red envelopes, right... Here is a record of N people giving red envelopes to each other and grabbing red envelopes. Please count their gains from grabbing red envelopes.

Input format:
Input the first line to give a positive integer N (≤10​4​^4), that is, the total number of people participating in red envelope distribution and red envelope grab, then these people are numbered from 1 to N. In the next N lines, the i-th line gives the record of the red envelope issued by the person numbered i, in the following format: KN​1​​ P​1​​ …N​K​​ P​K where K (0≤K≤20) It is the number of red envelopes sent. Ni is the number of the person who grabs the red envelope, and Pi (>0) is the amount of red envelopes (in cents). Note: For red envelopes issued by the same person, each person can only grab it once at most, and cannot be repeated.

Output format:
output each person’s serial number and income amount in descending order of income amount (in yuan, output 2 decimal places). Each person’s information is on one line with a space between the two numbers. If there is a tie in the amount of income, it will be output in descending order according to the number of red envelopes grabbed; if there is a tie, it will be output in increments according to the individual number.

Input example:
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
Output example:
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

The qsort function is included in <stdlib.h>
Usage

Code:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct peo{
    
    
	int id;
	int fmoney;
	int smoney;
	double jieguo;
	int geshu;
};

int cmp(const void *a,const void *b){
    
    
	struct peo s1 = *(struct peo*)a;
	struct peo s2 = *(struct peo*)b;
	if(s1.jieguo<s2.jieguo){
    
    
		return 1;
	}else if(s1.jieguo==s2.jieguo){
    
    
		if(s1.geshu<s2.geshu){
    
    
			return 1;
		}else if(s1.geshu==s2.geshu){
    
    
			if(s1.id>s2.id){
    
    
				return 1;
			}else if(s1.id==s2.id){
    
    
				return 0;
			}else{
    
    
				return -1;
			}
		}else{
    
    
			return -1;
		}
	}else{
    
    
		return -1;
	}
}
int main(int argc, char *argv[]) {
    
    
	int i,j,n,m,a,b;
	scanf("%d",&n);
	struct peo p[10005] = {
    
    0};
	for(i=0;i<n;i++){
    
    
		scanf("%d",&m); //发的红包个数
		p[i].id = i+1;
		for(j=0;j<m;j++){
    
    
			scanf("%d%d",&a,&b); //a抢红包的人,b抢到的钱
			p[a-1].smoney += b;
			p[a-1].geshu++;
			p[i].fmoney += b;
		} 
	}
	for(i=0;i<n;i++){
    
    
		p[i].jieguo = p[i].smoney - p[i].fmoney;
	}
	qsort(p,n,sizeof(struct peo),cmp);
	//输出
	for(i=0;i<n;i++){
    
    
		printf("%d %.2lf\n",p[i].id,p[i].jieguo/100);
	} 
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45880043/article/details/108764474