topcoder应用python解决数组问题

问题综述:

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,害,太菜了我,还得继续努力!循环太多,运行速度过慢,可能是扣分的原因,在这里如果有简便方法的宝宝们可以留言哈~~
运行通过的图片【快乐大家一起分享呦】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值