Introduction to a two-time sorting trick (best coder27 1001jump Jump)

Source: Internet
Author: User

Let's start by describing the problem:

Problem description
There are   n Children in the long jump to see who jumps the farthest. Each child can jump 3 times, the child's score is three times the maximum value of the distance. For example, when a child jumps 3 times 10, 30, and 20 respectively, the child's score is 30. Give each child a distance of three jumps and ask what the final ranking is for each child.

Problem Analysis:
Method 1:
Due to the small size of the original problem, only two or three children, can be used in a violent way to solve, can also meet the time within 1s (except Java code).

Method 2:
Since the problem is sorted by the maximum distance of the child's long jump, it appears when the child is in the order of the children, and is incorrect because the original order is not saved.

Solution:
Design a new structure to store a child's three structure information, long jump distance, current order and priority after sorting. Then sort by the long jump distance, then update the priority according to the order, and then sort in the original order of entry. The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 10

typedef struct JUMP_KID
{
int num;
int value;
int pri;

}jump_kid;

int max_of_three (int a,int b,int c)
{/* Premises a!=b!=c*/
int max3;
if (a > B && a > C)
Max3 = A;
else if (b > a && b > C)
Max3 = b;
Else
Max3 = C;
return max3;
}

int Comp_kids_value (const void *a, const void *b)
{//value Descending sort
Return (((Jump_kid *) b)->value-((Jump_kid *) a)->value);
}

int comp_kids_num (const void *a, const void *b)
{
Return (((Jump_kid *) a)->num-(((Jump_kid *) b)->num);
}
int main ()
{
int I,j,num_case,num_kid,jump1,jump2,jump3;
Jump_kid Kids[max_num];

scanf ("%d", &num_case);

for (i = 1; I <= num_case; i++)
{
memset (kids,0,sizeof (kids));
scanf ("%d", &num_kid);
for (j = 0; J < Num_kid; J + +)
{
scanf ("%d%d%d", &jump1,&jump2,&jump3);
Kids[j].num = j + 1;
Kids[j].pri = 0;
Kids[j].value = Max_of_three (Jump1,jump2,jump3);
}

Qsort (Kids,num_kid,sizeof (jump_kid), comp_kids_value); Sort by Value first

for (j = 0; J < Num_kid; J + +)
{
Kids[j].pri = j + 1; Update priority after sorting
}

Qsort (Kids,num_kid,sizeof (jump_kid), comp_kids_num);

for (j = 0; J < Num_kid; J + +)
printf ("%d", KIDS[J].PRI);

printf ("\ n");
}

}

Introduction to a two-time sorting trick (best coder27 1001jump Jump)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.