The given N integer sequences, sum of two, and output M numbers from large to small.
Because the sum of all integers cannot exceed 5000
Use hash. 1 // simple hash
2 # include <stdio. h>
3 # include <stdlib. h>
4 # include <string. h>
5 # include <algorithm>
6 using namespace std;
7
8 const int maxn = 3005;
9 int arr [maxn], sum [10001], n, m, x, y;
10 // because all integers do not exceed 5000, so the maximum and also do not exceed 10000. Open the sum array by 10001!
11
12 int main ()
13 {
14 while (scanf ("% d", & n, & m )! = EOF)
15 {
16 arr [n];
17 for (int I = 0; I <n; I ++)
18 {
19 scanf ("% d", & arr [I]);
20}
21 memset (sum, 0, sizeof (sum ));
22 for (int I = 0; I <n-1; I ++)
23 {
24 for (int j = I + 1; j <n; j ++)
25 {
26 sum [arr [I] + arr [j] ++;
27}
28}
29 int count = 0;
30 for (int j = 10000; j> = 0; j --)
31 {
32 if (sum [j])
33 {
34 for (int I = 0; I <sum [j]; I ++)
35 {
36 count ++;
37 count = 1? Printf ("% d", j): printf ("% d", j );
38 if (count = m) break;
39 printf ("");
40}
41}
42 if (count = m) break;
43}
44 printf ("\ n ");
45}
46}
47