python 编码问题_Python鞋子搭配| 竞争编码问题

面对乱成一团的鞋子,如何找出最多能配对的鞋?此文章介绍了一个算法问题,通过给定一系列鞋子的大小和左右类型,寻找可以配对的最大数量。使用Python3实现,通过比较每只鞋子的大小和类型,确保一对鞋子大小相同且一左一右。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 编码问题

Question:

题:

Zack has messed up all his shoes. He has N shoes (single shoe, not paired).  You have to search for the partner of each shoe. Find the maximum pair you can make out of them.

扎克弄乱了他的所有鞋子。 他有N鞋(单鞋,不成对)。 您必须搜索每只鞋子的搭档。 找到您可以从中做出的最大配对

You will be given a description of each shoe i.e. the size of the shoes and if it is left shoes (L) or right shoes (R). Two shoes can be paired if they are having the same size and one is left L and the other is right R.

您将得到每只鞋子的描述,即鞋子的大小以及是左鞋( L )还是右鞋( R )。 如果两双鞋子的尺码相同,则可以配对,并且一只鞋子左L ,另一只鞋子R。

Input:

输入:

The first line contains a single integer N. The number of shoes Next N lines has a description for each shoe, with spaces, separated size, and L/R.

第一行包含一个整数N。 鞋子的数量接下来的N行包含每只鞋子的描述,并带有空格,分隔的大小和L / R。

    5
    1	L
    2	R
    3	L
    1	R
    2	L

Output:

输出:

A single integer, maximum pairs can be made out of the given shoes.

可以从给定的鞋子中制成一个整数,最大对。

    2

Constraints:

限制条件:

    1<=N<=1000
    Shoe size will be a positive integer less than 100.

Explanation/Solution:

说明/解决方案:

In this question, we are given a number of shoes with their size and type whether it is left or right. Our aim is to make a pair of shoes which have the same size and one is left and the other is right. So we make two array one have the shoe size and other having the shoe type. Each time we compare the shoe size with other shoes and also check the types of shoes it is left or right the make one pair of shoes.

在这个问题中,我们给了许多鞋子,它们的大小和类型是左还是右。 我们的目标是制造一双尺寸相同的鞋子,一双左,另一双右。 因此,我们使两个数组中的一个具有鞋子的大小,另一个具有鞋子的类型。 每次我们将鞋子的尺码与其他鞋子进行比较,并检查鞋子的类型时,左,右都是一双鞋子。

To solve this question, we are using Python3.

为了解决这个问题,我们使用Python3。

Code:

码:

s = int(input())

arr = [ ]
brr = [ ]
p = s

while(s>0):
  s-=1
  a,b = input().split()
  arr.append(int(a))
  brr.append(b)

i,j,count=0,0,0
s = p

while(i<s-1):
  j=i+1
  while(j<s):
    if(arr[i]==arr[j] and brr[j]!= brr[i] and brr[j]!='S' and brr[i]!='S'):
      brr[i]='S'
      brr[j]='S'
      count+=1
      break
    j+=1
  i+=1
print(count)

Output

输出量

6
1 L
2 L
3 L
1 R
2 R
3 R
3


翻译自: https://www.includehelp.com/python/shoes-matching-competitive-coding-questions.aspx

python 编码问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值