fork(10) download
  1. #include <algorithm>
  2. #include <array>
  3. #include <iostream>
  4.  
  5. template <std::size_t OUT, std::size_t IN>
  6. std::array<int, OUT> sampling(const std::array<int, IN>& a)
  7. {
  8. std::array<int, OUT> res;
  9.  
  10. for (int i = 0; i != OUT - 1; ++i) {
  11. int index = i * (IN - 1) / (OUT - 1);
  12. int p = i * (IN - 1) % (OUT - 1);
  13.  
  14. res[i] = ((p * a[index + 1]) + (((OUT - 1) - p) * a[index])) / (OUT - 1);
  15. }
  16. res[OUT - 1] = a[IN - 1]; // done outside of loop to avoid out of bound access (0 * a[IN])
  17. return res;
  18. }
  19.  
  20. void print(const std::array<int, 77>& a)
  21. {
  22. for (int i = 0; i != a.size(); ++i)
  23. std::cout << i << ":" << a[i] << std::endl;
  24. }
  25.  
  26. int main() {
  27. std::array<int, 100> a100;
  28. int n(-10);
  29. std::generate(a100.begin(), a100.end(), [&n]{ return n += 10; });
  30.  
  31. print(sampling<77>(a100));
  32. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0:0
1:13
2:26
3:39
4:52
5:65
6:78
7:91
8:104
9:117
10:130
11:143
12:156
13:169
14:182
15:195
16:208
17:221
18:234
19:247
20:260
21:273
22:286
23:299
24:312
25:325
26:338
27:351
28:364
29:377
30:390
31:403
32:416
33:429
34:442
35:455
36:468
37:481
38:495
39:508
40:521
41:534
42:547
43:560
44:573
45:586
46:599
47:612
48:625
49:638
50:651
51:664
52:677
53:690
54:703
55:716
56:729
57:742
58:755
59:768
60:781
61:794
62:807
63:820
64:833
65:846
66:859
67:872
68:885
69:898
70:911
71:924
72:937
73:950
74:963
75:976
76:990