The Fun Of Algorithm - Day1 - Find the traffic offender

Code

'''Scene:
   One truck broke the traffic rules, run away after colliding. Three witnesses at the scene witness the incident,
   but they didn't remember the car number. Only noted some characteristics of the car number.
   A said: the first two digits of the license plate are the same
   B Said: the last last digits of the license plate are the same.
   C is a mathematician, who said the car number of four digits is just the square of integer
   According to the above clues to find out the car number
   '''

# the first two digits is xx
# the last two digits is yy
# the four digits is 1000*x+100*x+10*y+y = i



import time
# add timer to calculate the performance
# Basic performance
###############################################################
start = time.time()
for x in range(10):
    for y in range(10):
        if x!=y:
            i = 1000*x+100*x+10*y+y
            # if number<30, its square will smaller than 4 digits
            # if number>100, its square will bigger than 4 digits
            for temp in range(31, 100):
                if temp*temp == i:
                    print("The car number is : ", i)
end = time.time()
print("The Runtime is {0}".format((end-start)))

# Better performance
###############################################################
start = time.time()
flog = 0
for x in range(10):
    if flog:
        break
    for y in range(10):
        if flog:
            break
        if x!=y:
            i = 1000*x+100*x+10*y+y
            # if number<30, its square will smaller than 4 digits
            # if number>100, its square will bigger than 4 digits
            for temp in range(31, 100):
                if temp*temp == i:
                    print("The car number is : ", i)
                    flog = 1
                    break
end = time.time()
print("The Runtime is {0}".format((end-start)))

Output

The car number is :  7744
The Runtime is 0.0010006427764892578
The car number is :  7744
The Runtime is 0.0009949207305908203
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值