0
Question
Hello everyone The question is we have 3 input x,y,z and the condition we want to fulfill is that i*x+y==z print value of i and if i*x+y!=z print the value of i in which sum i*x+y is nearest to z . Example like 2 5 11 So i*2+5<=11 print 3 2nd input 4,10,20 So i*4+10<=20 18<=20 Value of i which nearest to z is 2 I solved the 1 input My code t=int(input()) for i in range(t): X,Y,Z=map(int,input().split()) for i in range(0,Z): if i*X+Y<=Z: print(i) I'm stuck in the 2nd input?
2 Answers
+ 2
Is the lower value always the nearest?
+ 1
Try this way:
if I*x+y==z : print(I) break
else :
if I*x+y < z : continue
else :
print(i-1) break
#its an algorithm.