CodePudding user response:
# x and y are integer of x and y all possible# x + y=A
# x * y=B
Def fun (A, B) :
For I in range (A) :
For j in range (B) :
If I + j==A and I * j==B:
Print (I, j)
Fun (8 (9)
Nested for loop can work out the solution
CodePudding user response:
The problem with cycle may be not, because A, B are integers, including positive integer, zero, negative integerFor example: A=7, B=120,
The solution is:
X=15, Y=8
X=8, Y=15
The equation:
X ^ 2 - Ax + B=0
//pseudocode:
The delta=A ^ 2-4 b
If the delta & lt; 0 then 'No solution'
The else
If SQRT (delta) is not an integer AND (A + SQRT (delta) % 2 is not an integer AND (A - SQRT (delta) % 2 is not an integer
Then 'No Solution'
The else
X1=(A + SQRT (A ^ 2-4 b))/2, X2=(A - SQRT (A ^ 2-4 b))/2
If X1==X2 then only one solution: X=X1, Y=X1
Else there are two distinct solutions:
X=X1, X2 and X==Y X2, Y=X1
Note that the root of integer (123.0 to 123)
CodePudding user response:
Both please input A (integer, | A | & lt; 11 e7) : -Both please input B (integer, | | B & lt; 12 e14) :
X=3, Y=4
X=4, Y=3
A=(int) (input (" both please input A (integer, | A | & lt; 1 e7) : "))
B=(int) (input (" both please input B (integer, | | B & lt; 1 e14) : "))
If (abs (A) & gt;=1 e7 or abs (B) & gt;=1 e14) :
Print (" Invaild inputs ")
Def func (A, B) :
Threshold=2 * * (A) - (A) (B) + 1
For I in range (threshold) :
For j in range (threshold) :
If (I + j==A and I * j==B) :
Print (" X={}, Y={} ". The format (I, j))
Print (" X={}, Y={} ". The format (j, I))
Return
Elif (I + j==and - I * j==B) :
Print (" X={}, Y={} ". The format (I, j))
Print (" X={}, Y={} ". The format (j, I))
Return
Elif (- I - j==A and I * j==B) :
Print (" X={}, Y={} ". The format (- I, j))
Print (" X={}, Y={} ". The format (j, I))
Return
Elif (I - j==and - I * j==B) :
Print (" X={}, Y={} ". The format (I, j))
Print (" X={}, Y={} ". The format (j, I))
Return
The return - 1
If func (A, B)==1:
Print (" No solution ")
CodePudding user response:
Judge whether a number is an integer, the built-in function is_integer () is useful:A=1.2
Anderson s_integer () # False
B=1.0
B.i s_integer (#) True
CodePudding user response: