From 3950be91f9589567d8258c788e10a0dec758e659 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 13 Feb 2025 12:51:23 -0700 Subject: 1071. number spiral --- introductory_problems/number_spiral.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 introductory_problems/number_spiral.cpp (limited to 'introductory_problems/number_spiral.cpp') diff --git a/introductory_problems/number_spiral.cpp b/introductory_problems/number_spiral.cpp new file mode 100644 index 0000000..95290b7 --- /dev/null +++ b/introductory_problems/number_spiral.cpp @@ -0,0 +1,22 @@ +// 1071. Number Spiral +#include +using namespace std; + +long long x, y; + +long long solve(long long x, long long y) { + long long z = fmax(x, y) - 1; + if (z & 1) + return x < y ? z * z + x : z * z + 2 * z - y + 2; + else + return x < y ? z * z + 2 * z - x + 2 : z * z + y; +} + +int main() { + static int tmp; + scanf("%d", &tmp); + for (int i = 0; i < tmp; i++) { + scanf("%lld %lld", &x, &y); + printf("%lld\n", solve(x, y)); + } +} -- cgit v1.2.3