问题综述:
Problem Statement
You are given an y. We are looking for any x that satisfies the following constraints:
x has exactly three elements
( x[0] * x[1] ) + x[2] = y
Each x[i] must be between -1000 and 1000, inclusive.
No x[i] can be equal to 0 or 1.
Find and return one such x.
If there are multiple valid solutions, you may return any of them. You may assume that for our constraints on y (specified below) at least one valid x always exists.
定义方式
Class:
AddMultiply
Method:
makeExpression
Parameters:
integer
Returns:
tuple (integer)
Method signature:
def makeExpression(self, y):
python代码块
class AddMultiply:
def makeExpression(self,y):
self.y=y
if 0<=y<=500:
x=[0,0,0]
for i in range(-1000,1001,1):
if ((x[0] * x[1] + x[2]) == y):
break
else:
x[0] = i
for j in range(-1000,1001,1):
x[1]=j
for k in range(-1000,1001,1):
x[2]=k
if((x[0]*x[1]+x[2])==y):
return x
break
else:
print("input wrong!Please try again!")
运行通过!!!开心开心~~~
这次的代码拿了92.5point,害,太菜了我,还得继续努力!循环太多,运行速度过慢,可能是扣分的原因,在这里如果有简便方法的宝宝们可以留言哈~~