Home > other >  Please help explain the meaning of the following a few lines of code each row, especially between th
Please help explain the meaning of the following a few lines of code each row, especially between th

Time:02-06

Dry:
Write a program that for any given positive integer N (N<=100), looking for all the four groups (a, b, c, d), meet a * * 3=b * * * * 3 + 3 + c * * 3 d
Among them 1 & lt; A, b, c, d & lt;=N, according to the value of a growing up, each row of the output, a perfect cubic equation of b, c, d, in accordance with the descending order output,????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
(if a value in the same way the two perfect stand, b value small first output; In the case of b values are equal, c value small output, first in b and c are equal, d value small first output)
Answer:
The import itertools
N=int (input ())
Ls=list (itertools.com binations (range (2, n + 1), 3))
Ln=[]
For I in ls:
S=sum (x * * 3 x in I)
If round (s * * (1/3), 3)==int (round (s * * (1/3), 3)) & lt;=n:
Ln=ln + [(int (round (s * * (1/3), 3)), I)]
Ln. Sort (key=(lambda x: x [0]))
For I in ln:
Print (" Cube={}, Triple={} ". The format (I [0], I [1]))

CodePudding user response:

 import itertools 
N=int (input ())
Ls=list (itertools.com binations (range (2, n + 1), 3))
Ln=[]
For I in ls:
S=sum (x * * 3 x in I)
If round (s * * (1/3), 3)==int (round (s * * (1/3), 3)) & lt;=n:
Ln=ln + [(int (round (s * * (1/3), 3)), I)]
Ln. Sort (key=(lambda x: x [0]))
For I in ln:
Print (" Cube={}, Triple={} ". The format (I [0], I [1]))


Round (number [ndigits])
Return the number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns on the integer to its input.

For the built - in types supporting round (), values are rounded to the closest multiple of 10 to the power minus ndigits. If two multiples are equally close, rounding is done toward the even choice (so, for example, both round and round (0.5) (0.5) are 0, and round (1.5) is 2). Any integer value is valid for ndigits (, zero, or negative). The return value is an integer if called with one argument, otherwise of the same type as number.

Class int (x, base=10)
Return an integer object constructed from a number or string x, or Return 0 if no arguments are given. If x is a number, Return x. __int__ (.) For floating point Numbers, this truncates forward zero.

CodePudding user response:

Thanks for the reply and explain! Clear that which I truly always understand is its content not the function. So, the puzzle in...
  • Related