UVa 12015 - Google is Feeling Lucky

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址

找出關聯值最高的URL,有相同時照輸入的順序輸出。

Solution

sort 後輸出關聯最高的URL即可,因為要保持相同關聯值時的相對順序,所以使用 std::stable_sort 。(不過一開始用 std::sort 也 AC 了…)

Code

UVa 12015UVa 12015 - Google is Feeling Lucky
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
27
28
29
30
31
32
33
34

#include<cstdio>
#include<algorithm>

struct Data
{
char str[101];
int relevance;
};
int main()
{
Data search[10];
int Case;
scanf("%d", &Case);

for (int i = 1; i <= Case; i++)
{
for (int j = 0; j < 10; j++)
scanf("%s%d", search[j].str, &search[j].relevance);

std::stable_sort(search, search + 10, [](const Data& a, const Data& b)->bool{ return a.relevance > b.relevance; });

printf("Case #%d:\n", i);
int r = search[0].relevance;
puts(search[0].str);

for (int j = 1; search[j].relevance == r; j++)
puts(search[j].str);

}

return 0;
}