妙先生


私信TA

用户名:uq_57083779177

访问量:24868

签 名:

妙啊!

等  级
排  名 234
经  验 6003
参赛次数 0
文章发表 73
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

    错误9%、实在是不知道为什么。

解题思路:
    博客https://blog.csdn.net/bianxia123456/article/details/104388390?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160138683319195240403079%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160138683319195240403079&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v3~pc_rank_v2-1-104388390.first_rank_ecpm_v3_pc_rank_v2&utm_term=%5D%E5%AE%8C%E7%BE%8E%E7%9A%84%E4%BB%A3%E4%BB%B7python&spm=1018.2118.3001.4187

参考代码:

n = int(input())
s = list(input())
s1 = s[::-1]
total = 0
"""判断是否可以做回文串"""
def check(S):
    count = 0
    if len(S)%2 ==0:
        for s in S:
            if S.count(s) != 2:
                return False
    else:
        for s in S:
            if S.count(s) != 2:
                count += 1
                if count >= 2:
                    return False
    return True

def swap(s,s1,n):
    global total
    if check("".join(s)):
       for i in range(n//2):
           a = s[i]
           if s.count(a) != 1:
                total += s1.index(a)
                s1.pop(s1.index(a))
                s = s1[::-1]
           else:
                total += n//2 - i
                s1.pop(a)
                s = s1[::-1]
swap(s,s1,n)
if total != 0:
    print(total)
else:
    print("Impossible")


 

0.0分

3 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

line 14判断是否为回文就有问题
2021-04-06 23:33:09
  • «
  • 1
  • »