2018年9月CCFCSP满分python3代码与思路

第一题:

image.png

简单的第一题主要是考虑使用python3达到输入与输出的标准格式化。
代码如下:

n = int(input())
shopList = []
inputList = list(map(int, input().split()))
tomorrowList = []
for i in range(n):
    shopList.append(inputList[i])
for i in range(n):
    if i == 0:
        tomorrowList.append(int((shopList[0] + shopList[1]) /2))
    elif i == (n - 1):
        tomorrowList.append(int((shopList[n - 2] + shopList[n - 1]) / 2))
    else:
        tomorrowList.append(int((shopList[i - 1] + shopList[i] + shopList[i + 1]) / 3))
print(" ".join(str(i) for i in tomorrowList))

结果展示如下:


image.png

第二题:

image.png

结果为两人碰面时间差的和。
代码如下:

n = int(input())
littleH = []
littleM = []
for i in range(n):
    littleH.append(list(map(int, input().split())))
for j in range(n):
    littleM.append(list(map(int, input().split())))
sumTime = 0
# startN = 0
start = 0
for i in range(n):
    j = start
    while j < n:
        if littleH[i][0] < littleM[j][0]:
            if littleH[i][1] < littleM[j][0]:
                j = n
                continue
            elif littleH[i][1] < littleM[j][1]:
                sumTime += (littleH[i][1] - littleM[j][0])
            else:
                sumTime += (littleM[j][1] - littleM[j][0])
        elif littleH[i][0] < littleM[j][1]:
            if littleH[i][1] < littleM[j][1]:
                sumTime += (littleH[i][1] - littleH[i][0])
            else:
                sumTime += (littleM[j][1] - littleH[i][0])
        else:
            start = j + 1
            j = j + 1
            continue
        j = j + 1
        
print(int(sumTime))

测试结果展示如下:


image.png

第三题:

image.png

image.png

image.png

image.png

该题需要考虑较多的临界条件,也存在较多的解决方法。
代码如下:

# 输入部分及对大小写处理
inputNum = list(map(int, input().split()))
treeSubstance = []
searchList = []
for i in range(inputNum[0]):
    inputStrList = input().split()
    lowerList = []
    dealLow = ""
    for inputStr in inputStrList:
        dealLow = inputStr
        if "#" not in inputStr:
            dealLow = inputStr.lower()
        lowerList.append(dealLow)
    dealStrLow = " ".join(str(x) for x in lowerList)
    treeSubstance.append(dealStrLow)
for j in range(inputNum[1]):
    inputStrList = input().split()
    lowerList = []
    dealLow = ""
    for inputStr in inputStrList:
        dealLow = inputStr
        if "#" not in inputStr:
            dealLow = inputStr.lower()
        lowerList.append(dealLow)
    dealStrLow = " ".join(str(x) for x in lowerList)
    searchList.append(dealStrLow)

# 对输入的树进行拆分简化处理
dictSubsList = []
for i in range(inputNum[0]):
    dictSub = {}
    dictSub['level'] = treeSubstance[i].count("..",0,len(treeSubstance[i]))
    dictSub['content'] = treeSubstance[i].replace(".","").split()
    dictSubsList.append(dictSub)

for i in range(inputNum[0]):
    dictSubsList[i]['parent'] = -1
    for iParent in range(i - 1, -1, -1):
        if dictSubsList[iParent]['level'] < dictSubsList[i]['level']:
            dictSubsList[i]['parent'] = iParent
            break
# print(dictSubsList)
# 查找
result = []
for j in range(inputNum[1]):
    searPartList = searchList[j].split()
    resultItem = []
    if len(searPartList) == 1:
        for iDictSub in range(inputNum[0]):
            if searPartList[0] in dictSubsList[iDictSub]['content']:
                resultItem.append(iDictSub + 1)
                # ts
                # print("searPartList:",searPartList)
    # 后代选择器
    else:
        # 记录最后子元素的值
        regularList = []
        # 遍历元素树
        for iDictSub in range(inputNum[0]):
            regIter = len(searPartList) - 1
            # 用来启动循环查找parent,初始化值根据循环所需的数据设定
            parentPos = iDictSub
            # 遍历本次搜索的子选择器
            while regIter >= 0:
                # 查找到元素后,跟更新查找的位置和元素树的位置
                if searPartList[regIter] in dictSubsList[parentPos]['content']:
                    # 已全部找到
                    if regIter == 0:
                        regularList.append(iDictSub)
                        break
                    regIter -= 1
                    parentPos = dictSubsList[parentPos]['parent']
                    # 没有验证完全部子元素,失败跳出
                    if parentPos == -1:
                        break
                # 不相等且是子选择器的最后一个元素,则直接跳过 
                elif regIter == len(searPartList) - 1:
                    break
                # 该父元素不是则向上继续查询
                else:
                    parentPos = dictSubsList[parentPos]['parent']
                    # 没有验证完全部子元素,失败跳出
                    if parentPos == -1:
                        break
                        

        resultItem.extend(map(lambda x :x + 1, regularList))

    if len(resultItem) == 0:
        result.append("0")
    else:
        resultItem.insert(0, len(resultItem))
        result.append(" ".join(str(x) for x in resultItem))


print("\n".join(str(x) for x in result))

你可能感兴趣的:(2018年9月CCFCSP满分python3代码与思路)