“This is the 34th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
I. Problem description
Country X will participate in the alliance parade.
The organizers requested that soldiers from each country form exactly two squares.
Country X discovered that weak country Y had sent a team of 130 soldiers, whose soldiers could change two formations as they marched:
130 = 81 + 49 = 9^2 + 7^2
130 = 121 + 9 = 11^2 + 3^2
The king of X was greatly stimulated. He felt that X was 6 times larger than Y, and he should create more formations. So he gave orders: We are going to send out a team, in the march to change into 12 formations!!
The underlings are miserable, trying to figure out at least how many sides they need to form 12 different squads. Please use the advantage of computers to calculate at least how many soldiers are needed.
(PS: Don’t lose faith, 1105 people can form 4 formations)
Two, the title requirements
Operating limits
- Maximum running time: 1s
- Maximum running memory: 128M
inspection
2. It is recommended to use 10~15 minutesCopy the code
Third, problem analysis
Simply put: Given a number, divide it into two different squares and add them together to find 12 different pairs of squares. And now they want you to figure out a solution that can have 12 different squares, which is the other way around.
The first for loop lists the numbers to judge, and the second for loop evaluates to see if the criteria are met. The numbers that meet the criteria are counted +1 until there are 12, and the result is printed, ending the loop. O!
Four, coding implementation
#include<iostream>
#include<math.h>
using namespace std;
int main(a)
{
long long int i,j,n,k,ans;// Initialization conditions
for(i=1105;; i++)// The first layer for loop
{
ans=0;// The counter is initialized to 0
for(j=4; j*j<=i/2; j++)// The second layer for loop
{
n=i-j*j;// The second square n, the first square is j
k=sqrt(n);// Take the square root of n to see if n is a square number
if(k*k+j*j==i)// The condition is met
{
ans++;// count +1}}if(ans==12)/ / to 12
{
cout<<i;// Output the result
exit(0);// End the loop}}return 0;
}
Copy the code
5. Output results
The output is 160225