#include <algorithm> #include <stdio.h> int triangleSum(int n) { return (1 + n) * n / 2; } void calculate(const int k, const int row, const int column, const int year, int& minYear) { const int totalSum = triangleSum(row); const int leftSum = triangleSum(column - 1); const int rightSum = triangleSum(row - column); const int currentSum = totalSum - leftSum - rightSum; if (currentSum <= k) { minYear = std::min(year, minYear); } } int main() { int n, k, year, minYear = 2020; scanf("%d %d\n", &n, &k); for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { scanf("%d", &year); calculate(k, i + 1, j + 1, year, minYear); } } printf("%d\n", minYear); return 0; }
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 | #include <algorithm> #include <stdio.h> int triangleSum(int n) { return (1 + n) * n / 2; } void calculate(const int k, const int row, const int column, const int year, int& minYear) { const int totalSum = triangleSum(row); const int leftSum = triangleSum(column - 1); const int rightSum = triangleSum(row - column); const int currentSum = totalSum - leftSum - rightSum; if (currentSum <= k) { minYear = std::min(year, minYear); } } int main() { int n, k, year, minYear = 2020; scanf("%d %d\n", &n, &k); for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { scanf("%d", &year); calculate(k, i + 1, j + 1, year, minYear); } } printf("%d\n", minYear); return 0; } |