| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11751 | Sorting makes me happy |
|
| 11752 | My Insertion Sort |
|
| 11765 | HAPPY NEW YEAR |
|
| 11766 | NEW Postman |
|
Description
On the Wednesday morning in week 15, TA remembers that HT gives students a homework, which is related to quick sorting with struct.
Now, TA wants to examine that whether you did the homework.
We will give you a list of objects, each has three attributes: a int number, a string, and a float number.
And you should sort them according to int number first, and then string, and then float number.
(All in the descending order. For example: int number 3 is behind 4 in output list)
(For example: string annie is behind string apple in output list)
HINT: If you don't understand the meaning of the problem after reading above, please see the sample IO and think clearly :)
You should implement funcion.c below and submit it. (Due to Partial judge)
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "function.h"int compare_function(const void* a, const void* b){// set appropriate type// first sorted by value// if equal then soreted by str// if still equal then sorted by x}
You should see .c and .h file below to know how the function and the whole program works.
Input
Input will contain four lines.
First line contains N, indicating the number of objects you should read.
The following three lines contain each object's three attributes: a int number, a string, and a float number.
( 0 < N < 100, 0 < len(string) < 30)
Output
Output contain N lines. (And you should printf "\n" in the end)
You should show the sorted objects in each line in descending order.
Sample Input Download
Sample Output Download
Partial Judge Code
11751.cPartial Judge Header
11751.hTags
Discuss
Description
In out class, HT Chen introduced a way to implement a qsort-like mysort using bubble sort.
To make you more clearly to the machism, you are asked to implement a qsort-like mysort using insertion sort.
The main function has already provided for you, what you have to do is just implement the mysort function.
For more specific, you just need to fill the following blank:
#include <stdio.h> #include <stdlib.h> int compare(const void* a, const void* b) { // compare a with b } void assign(char* x, char* y, size_t size) { // assign y to x } void mysort(void* arr, size_t count, size_t size, int (*cmp) (const void*, const void*)) { // do sorting!!! }
The pseudo code below would be helpful if you want to know how the insertion sort works.
i ← 1
while i < length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
end while
i ← i + 1
end while
Input
There are 2 lines input.
The first line contains a integer, indicating the total number of integers would be sorted.
The second line consists of the integers being sorted.
Output
The integers have been sorted.
Please notice that the sequence has to be in ascending order.
Sample Input Download
Sample Output Download
Partial Judge Code
11752.cPartial Judge Header
11752.hTags
Discuss
Description
Collected letters would be classified by zip code. After that, certain postman likes to sort the letters by sender’s name. Here’s an example:
30013 Xiao 10850 Wang
23679 Huang 23679 Huang
24241 Chen -> 24241 Chen
89346 Hsu 30013 Tsai
10850 Wang 30013 Xiao
30013 Tsai 89346 Hsu
Letters would be arranged firstly by zip code, secondly by name once zip codes are the same. Sort the letters with the name by the weight of the string from high to low. The weight of string is defined as following:
Lower case letters: a ~ z corresponds to 1 ~ 26
Upper case letters: A ~ Z corresponds to 2 ~ 27
| letter | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
| weight | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| letter | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
| weight | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
For example, the weight of "Allen" is 2 + 12 + 12 + 5 + 14 = 45
Once the zip code and the weight are the same. Dictionary order is used for sorting names. As an example, the weight of “Tsai” and “Xiao” are the same, “T” is previous than “X”, which results the order: 30013 Tsai, 30013 Xiao.
main.c
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
int main(void){
int n;
int i;
Letter *letters;
// Read inputs
scanf("%d", &n);
letters = (Letter*)malloc(sizeof(Letter)*n);
for(i=0; i<n; i++)
scanf("%d %s", &(letters[i].zipcode), letters[i].name);
// Sort the data
qsort((void*)letters, n, sizeof(Letter), compare);
// Output result
for(i=0; i<n; i++)
printf("%d %s\n", letters[i].zipcode, letters[i].name);
free(letters);
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
typedef struct {
int zipcode;
char name[15];
} Letter;
int compare(const void *, const void *);
#endif
Input
First line is an integer N, number of the letters. The following N lines have an integer, zip code, followed by a name. N is less than 10 million.
Output
Sorted result, N lines, each line contains the zip code and a name separated by a space.