本文主要是介绍2018年贝壳网校招(秋招)算法笔试编程题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、题目描述:
给定一个长度为N的序列A1到AN,求所有区间[L,R](1≤L≤R≤N)的极差之和,其中区间[L,R]的极差定义为AL到AR中的最大值与最小值之差。
思路:暴力求解,通过率36%
代码如下:
# -*- coding:utf-8 -*-
class Solution:# array 二维列表def Find(self, N, array):# write code herenum = 0for i in range(N-1):for j in range(i+1,N):num += (max(array[i:j+1]) - min(array[i:j+1]))return num
while (True):try:n = input()array = input()N = int(n)arr = array.split(' ')for i in range(N):arr[i] = eval(arr[i])s = Solution()num = s.Find(N,arr)print(num)except:break
2、题目描述:
众所周知,新款笔记本电脑都拥有自己的省电方案,现有某品牌笔记本电脑,正常工作状态下耗电量为P1/min,当机主离开电脑T1min时,电脑进入屏幕保护状态,耗电量为P2/min,当电脑进入屏保状态T2min后,电脑进入睡眠状态,耗电量为P3/min。
已知电脑主人的一天有n段时间在使用电脑工作,即[l1, r1], [l2, r2],…, [ln, rn],在[rn-1, ln]期间是没有操作电脑的,问在[l1, rn]时间内,电脑共耗电多少瓦。、
思路:没啥好说的,分段函数求解,通过率100%
代码如下:
# -*- coding:utf-8 -*-
class Solution:# array 二维列表def Find(self, arr, ran):# write code heren, p1, p2, p3, t1, t2 = arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]num1 = s.ran_nei(ran,p1)num2 = s.ran_wai(ran, t1, t2, p1, p2, p3)return (num1+num2)def ran_nei(self,ran,p):score = 0for i in range(len(ran)):score += (ran[i][1] - ran[i][0])*preturn scoredef ran_wai(self,ran,t1,t2,p1,p2,p3):score = 0for i in range(len(ran)-1):sub_ran = ran[i+1][0] - ran[i][1]if sub_ran <= t1:score += sub_ran*p1elif t1 < sub_ran <= (t2+t1):score += (t1*p1 + (sub_ran-t1)*p2)elif sub_ran > (t2+t1):score += (t1*p1 +t2*p2 + (sub_ran-t2-t1)*p3)return scorewhile (True):try:ran = []array = input()arr = array.split(' ')for i in range(len(arr)):arr[i] = eval(arr[i])for i in range(arr[0]):a = input()b = a.split(' ')c,d = eval(b[0]),eval(b[1])ran.append([c,d])s = Solution()num = s.Find(arr,ran)print(num)except:break
3、题目描述:
扑克牌分别为A23456789TJQK共13张牌
出牌规则如下:
单牌,对子,三带至多一张牌,四带至多两张牌,连续至少五张为顺子
输入:一个长度为20的字符串,例如‘8K67A65K27T59K346AK2’
输出:至少多少轮能把手里的牌出完
思路:暂时只想起将输入的字符串挨个放入健排序的字典中,对字典的健可以进行排序(找顺子),以及每个健(对应牌值)底下有几张牌(找四带二,三带一,对子,单牌),继而算出至少多少轮出完
代码如下(暂时只实现字符串读取进字典):
# -*- coding:utf-8 -*-
class Solution:# array 二维列表def Find(self, array):# write code herereturn 4
while (True):try:dic = {}arr = input()print(arr)a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13 = [],[],[],[],[],[],[],[],[],[],[],[],[]for i in range(len(arr)):if arr[i] == 'A':a1.append(arr[i])elif arr[i] == '2':a2.append(arr[i])elif arr[i] == '3':a3.append(arr[i])elif arr[i] == '4':a4.append(arr[i])elif arr[i] == '5':a5.append(arr[i])elif arr[i] == '6':a6.append(arr[i])elif arr[i] == '7':a7.append(arr[i])elif arr[i] == '8':a8.append(arr[i])elif arr[i] == '9':a9.append(arr[i])elif arr[i] == 'T':a10.append(arr[i])elif arr[i] == 'J':a11.append(arr[i])elif arr[i] == 'Q':a12.append(arr[i])elif arr[i] == 'K':a13.append(arr[i])dic[1], dic[2], dic[3], dic[4], dic[5], dic[6], dic[7], dic[8], dic[9], dic[10], dic[11], dic[12], dic[13] = a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13s = Solution()num = s.Find(dic)print(num)except:break
这篇关于2018年贝壳网校招(秋招)算法笔试编程题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!