【每日一题20220429】一站到底2

:mage:‍ 给定一个数字n,找出1到n(含n)中,经过淘汰后最终剩下到数字。淘汰到规则是:从左到右,删除第一个数字,然后每隔一个数字删除一个,直到到达列表末尾。
重复上面的步骤,但这次是从右到左。也就是,删除最右侧的数字,然后剩下的数字每隔一个删除一个。
不断重复这两步,从左到右和从右到左交替进行,直到只剩下一个数字。

【示例】
输入:9
输出:6
解释:1,2,3,4,5,6,7,8,9中,第一轮淘汰1、3、5、7、9后剩下2、4、6、8,第二轮淘汰4、8剩下2、6,第三轮淘汰2,所以最终结果是6

题目难度:简单
题目来源:CodeWars-Last man standing

def solution(num: int)-> int:
    # your code here

assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510

n = 10:
数字集合为:1 2 3 4 5 6 7 8 9 10
第一轮,淘汰左侧,剩下:2 4 6 8 10
第二轮,淘汰右侧,剩下:2 6 10
第三轮,淘汰左侧,剩下:6
不清楚答案为什么是8
8在第二轮就淘汰了啊 :disappointed:

def solution(num: int) -> int:
    num = list(range(1, num + 1))
    flag = 1
    while len(num) != 1:
        num = num[flag + 1::2] if len(num) % 2 == 1 and flag == 0 else num[flag::2]
        flag = 1 if flag == 0 else 0
    return int(num[0])
2 个赞

不是淘汰右侧,是从右到左进行淘汰:

  • 第二轮从右到左,淘汰的是:2, 6, 10,剩下 4, 8
  • 第三轮从左到右,淘汰的是:4,剩下 8
def solution(num:int):
    num = [str(i) for i in range(1,num+1)]
    x = 1
    while len(num) >1:
        if x % 2 != 0:
            for i in range(len(num)):
                if i % 2 == 0:
                    del num[0]
                else:
                    num.append(num[0])
                    del num[0]
            x+=1
        else:
            for i in range(len(num)):
                if i % 2 == 0:
                    del num[-1]
                else:
                    num.insert(0,num[-1])
                    del num[-1]
            x+=1
    return int(num[0])

def solution (num: int)->int:
test = list(range(1, num + 1, 1))
# print(test)
if len(test) == 1:
return test[0]
else:
test1 = test[1::2]
# print test1
while len(test1) != 1:
test = test1
test1 = test[::-1][1::2]
# print test1
return test1[0]

资产

资产

# daily_20220429
def solution(num: int)-> int:
    # your code here
    num_list = [str(x+1) for x in range(num)]
    round = 1
    while len(num_list) > 1:
        if round % 2 == 0:
            del_num = [e for i, e in enumerate(num_list[::-1]) if i % 2 == 0]
            for i in del_num:
                num_list.remove(i)
            round += 1
        else:
            del_num = [e for i, e in enumerate(num_list) if i % 2 == 0]
            for i in del_num:
                num_list.remove(i)
            round += 1
    return int(num_list[0])
def solution(num: int)-> int:
    # your code here
    num_list = [i for i in range(1,num+1)]
    while len(num_list) > 1:
        for item in num_list[::2]:
            if len(num_list) == 1:
                break
            num_list.remove(item)
        for item in num_list[::-2]:
            if len(num_list) == 1:
                break
            num_list.remove(item)
    return num_list[0]

assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510

优化版本

def solution(num:int) -> int:
    num = [i for i in range(1,num+1)]
    x = 1
    while len(num) >1:
        if x % 2 ==0:
            num = num[-2::-2]
            num.reverse()
            x+=1
        else:
            num = num[1::2]
            x+=1
    return int(num[0])

def solution(num: int) -> int:
    step, res, arrow = 1, 1, 0
    while num > 1:
        if not num % 2 and arrow % 2:
            res -= step
        res += step
        num >>= 1
        step <<= 1
        arrow += 1
    return res


assert solution(1) == 1
assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510

def solution(num: int)-> int:
    # your code here
    l = [i for i in range(1, num + 1)]
    c = 1
    while len(l) > 1:
        if c % 2 != 0:
            l = [i for i in l if l.index(i) % 2 != 0 ]
        else:
            l.reverse()
            l = [i for i in l if l.index(i) % 2 != 0]
            l.reverse()
        c += 1
    return l[0]


assert solution(1) == 1
assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510
def solution(num: int) -> int:
    list_num = [i for i in range(1, num + 1)]
    k = 0
    begin = 0
    end = len(list_num)
    step = 2
    while True:
        list_num_new = [list_num[i] for i in range(begin, end, step)]
        for i in list_num_new:
            list_num.remove(i)
        k = k + 1
        if k % 2 == 0:
            begin = 0
            end = len(list_num)
            step = 2
        else:
            begin = len(list_num) - 1
            end = -1
            step = -2
        if len(list_num) == 1:
            return list_num[0]


assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510
def solution(num: int)-> int:
    l1 = [i for i in range(1,num+1)]
    count = 1
    while len(l1) != 1:
        if len(l1) %2 != 0:
            l1 = l1[1::2]
        else:
            if count % 2 != 0:
                l1 = l1[1::2]
            else:
                l1 = l1[::2]
        count += 1
    else:
            return l1[0]
def solution(num: int)->int:
    list = [i for i in range(1,num+1)]
    list_new = []
    while len(list) > 1:
        for i in range(0,len(list)):
            if i%2 !=0:
                list_new.append(list[i])
        if len(list_new) != 1:
            list_new.reverse()
            list,list_new = list_new,[]
        else:
            return list_new[0]
assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510
def solution(num):
	list_1 = [i for i in range(1,num+1)]
	flag = 1
	while len(list_1)>1:
		if flag % 2 != 0:
			list_1 = list_1[1::2]
			#print(list_1)
		else :
			list_1 = list_1[-2::-2]
			list_1.reverse()
			#print(list_1)
		flag +=1
			
	print(list_1)
solution(9)
solution(100)
solution(1000)
def solution(num: int) -> int:
    li = [i for i in range(1, num + 1)]

    while len(li) > 1:
        li = [li[i] for i in range(1, len(li), 2)]
        li.reverse()
    return li[0]

def test_solution():
    assert solution(9) == 6
    assert solution(100) == 54
    assert solution(1000) == 510
def solution(num: int)-> int:
    num_list=[i for i in range(1,num+1)]
    n=1
    while len(num_list)>1:
        if n%2!=0:
            num_list=num_list[1::2]
        else:
            num_list=num_list[-2::-2][::-1]
        n+=1
    return num_list[0]

assert solution(9) == 6
assert solution(100) == 54
assert solution(1000) == 510