Test instructions
Four squared sum theorem, also known as Lagrange's theorem:
Each positive integer can be represented as a sum of squares of up to 4 positive integers.
If 0 is included, it can be represented as a sum of 4 squares.
Like what:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^ symbol denotes the meaning of a exponentiation)
For a given positive integer, there may be multiple representations of the sum of squares.
Ask you to sort 4 numbers: 0 <= a <= b <= c <= D
And for all possible representations press a,b,c,d for the combined primary key in ascending order, and finally output the first notation
Program input as a positive integer N (n<5000000)
Requires output of 4 non-negative integers, sorted from small to large, separated by spaces in the middle
For example, enter:
5
The program should output:
0 0 1 2
Analysis: Direct violence, three-layer cycle.
#include <bits/stdc++.h>using namespace Std;int main () { int N; scanf ("%d", &n); bool OK = false; for (int a = 0, a * a <= n; ++a) {for (int b = 0, b * b <= N; ++b) {for (int c = 0; c * C <= N; ++c) { in t tmp = n-a * A-B * b-c * C; Double T1 = (double) tmp; int t = (int) sqrt (t1); if (t * = = tmp) { printf ("%d%d%d%d\n", A, B, C, t); OK = true; break; } } if (OK) break; } if (OK) break; } return 0;}
2016 Blue Bridge Cup C/c++a group eighth question four square sum