尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

8.9华为OD机试真题 新系统 - 查找最佳充电策略 (Java/Py/C/C++/Js/Go)

8.9华为OD机试真题 新系统 - 查找最佳充电策略 (Java/Py/C/C++/Js/Go) 查找最佳充电策略2026 华为OD机试真题8月9日华为OD上机新系统考试真题 100 分题型点击查看华为 OD 机试真题完整目录2026最新华为OD机试新系统卷 双机位C卷 真题题库目录全覆盖题库 逐点算法考点详解题目描述给定一个一维数组priceArray表示未来priceRecords小时内每小时的电价单位分/kWh。找出充电成本最低的连续hours个小时时间段的开始时刻点。若存在多种成本最低方案优先返回最低成本方案的最早的时刻点。输入描述参数 1整数priceRecords表示电价记录数量参数 2整数hours表示连续小时数参数 3一维数组priceArray表示每小时的电价price1~priceN约束条件1 priceRecords 241 hours priceRecords1 price[i] 100输入为三行priceRecords hours priceArray其中priceArray按示例使用英文逗号分隔允许逗号后有空格。输出描述返回一个整数表示最优充电时段的起始索引从 0 开始。示例1输入12 3 25,15,20,18,12,25,30,28,22,16,14,35输出2说明连续 3 小时的最低电价时段是索引 2-4价格分别为 20,18,12总费用为 50 分。示例2输入12 4 23,35,67,68,89,12,24,37,57,10,12,45输出7说明连续 4 小时的最低电价时段是索引 7-10价格分别为 37,57,10,12总费用为 116 分。解题思路核心思想需要在长度为priceRecords的数组中找到长度恰好为hours的连续子数组使子数组和最小。使用固定长度滑动窗口即可在线性时间内完成。算法步骤从左到右遍历电价数组把当前价格加入窗口和。当窗口长度超过hours时移除窗口左端价格。当窗口长度等于hours时用当前窗口和更新最小成本。只有当当前窗口和严格小于历史最小值时才更新答案因此相同成本会保留更早起点。复杂度分析设电价记录数量为n。时间复杂度O(n)每个元素最多进出窗口一次。空间复杂度O(1)只使用常数个变量。Javaimportjava.util.*;publicclassMain{staticintsolve(intpriceRecords,inthours,int[]prices){// 固定长度滑动窗口窗口和表示当前连续 hours 小时的总费用intleft0;intsum0;intbestIndex0;intbestCostInteger.MAX_VALUE;for(intright0;rightpriceRecords;right){sumprices[right];while(right-left1hours){sum-prices[left];}if(right-left1hourssumbestCost){bestCostsum;bestIndexleft;}}returnbestIndex;}publicstaticvoidmain(String[]args){ScannerscannernewScanner(System.in);intpriceRecordsInteger.parseInt(scanner.nextLine().trim());inthoursInteger.parseInt(scanner.nextLine().trim());String[]partsscanner.nextLine().trim().split(,);int[]pricesnewint[parts.length];for(inti0;iparts.length;i){prices[i]Integer.parseInt(parts[i].trim());}System.out.println(solve(priceRecords,hours,prices));}}Pythondefsolve(price_records,hours,prices):# 维护长度为 hours 的窗口总费用只在发现更低费用时更新起点left0total0best_index0best_costfloat(inf)forrightinrange(price_records):totalprices[right]whileright-left1hours:total-prices[left]left1ifright-left1hoursandtotalbest_cost:best_costtotal best_indexleftreturnbest_index price_recordsint(input().strip())hoursint(input().strip())prices[int(x.strip())forxininput().strip().split(,)]print(solve(price_records,hours,prices))JavaScriptconstreadlinerequire(readline);functionsolve(priceRecords,hours,prices){// 固定长度滑动窗口窗口和越小代表充电成本越低letleft0;lettotal0;letbestIndex0;letbestCostInfinity;for(letright0;rightpriceRecords;right){totalprices[right];while(right-left1hours){total-prices[left];}if(right-left1hourstotalbestCost){bestCosttotal;bestIndexleft;}}returnbestIndex;}constrlreadline.createInterface({input:process.stdin,output:process.stdout});constlines[];rl.on(line,linelines.push(line));rl.on(close,(){constpriceRecordsNumber(lines[0].trim());consthoursNumber(lines[1].trim());constpriceslines[2].trim().split(,).map(xNumber(x.trim()));console.log(solve(priceRecords,hours,prices));});C#includebits/stdc.husingnamespacestd;intsolve(intpriceRecords,inthours,constvectorintprices){// 枚举所有长度为 hours 的连续窗口记录最小窗口和的最早起点intleft0;intsum0;intbestIndex0;intbestCostINT_MAX;for(intright0;rightpriceRecords;right){sumprices[right];while(right-left1hours){sum-prices[left];}if(right-left1hourssumbestCost){bestCostsum;bestIndexleft;}}returnbestIndex;}intmain(){intpriceRecords,hours;string line;cinpriceRecordshours;cin.ignore(numeric_limitsstreamsize::max(),\n);getline(cin,line);vectorintprices;stringstreamss(line);string item;while(getline(ss,item,,)){prices.push_back(stoi(item));}coutsolve(priceRecords,hours,prices)endl;return0;}Gopackagemainimport(bufiofmtosstrconvstrings)funcsolve(priceRecordsint,hoursint,prices[]int)int{// 固定窗口长度为 hours向右滑动时同步维护窗口和left:0total:0bestIndex:0bestCost:int(^uint(0)1)forright:0;rightpriceRecords;right{totalprices[right]forright-left1hours{total-prices[left]left}ifright-left1hourstotalbestCost{bestCosttotal bestIndexleft}}returnbestIndex}funcmain(){reader:bufio.NewReader(os.Stdin)line1,_:reader.ReadString(\n)line2,_:reader.ReadString(\n)line3,_:reader.ReadString(\n)priceRecords,_:strconv.Atoi(strings.TrimSpace(line1))hours,_:strconv.Atoi(strings.TrimSpace(line2))parts:strings.Split(strings.TrimSpace(line3),,)prices:make([]int,0,len(parts))for_,part:rangeparts{value,_:strconv.Atoi(strings.TrimSpace(part))pricesappend(prices,value)}fmt.Println(solve(priceRecords,hours,prices))}C语言#includestdio.h#includestdlib.h#includestring.h#includelimits.hintsolve(intpriceRecords,inthours,intprices[]){// 使用固定长度滑动窗口严格更小时才更新保证并列时返回最早起点intleft0;inttotal0;intbestIndex0;intbestCostINT_MAX;for(intright0;rightpriceRecords;right){totalprices[right];while(right-left1hours){total-prices[left];}if(right-left1hourstotalbestCost){bestCosttotal;bestIndexleft;}}returnbestIndex;}intmain(){intpriceRecords,hours;charline[1024];scanf(%d,priceRecords);scanf(%d,hours);getchar();fgets(line,sizeof(line),stdin);line[strcspn(line,\r\n)]\0;intprices[32];intcount0;char*tokenstrtok(line,,);while(token!NULL){prices[count]atoi(token);tokenstrtok(NULL,,);}printf(%d\n,solve(priceRecords,hours,prices));return0;}完整用例用例112 3 25,15,20,18,12,25,30,28,22,16,14,35用例212 4 23,35,67,68,89,12,24,37,57,10,12,45用例35 1 5,4,3,2,1用例45 5 10,20,30,40,50用例56 2 5,5,9,1,1,8用例68 3 9,8,7,1,2,3,4,5用例710 4 10,10,10,10,1,1,1,1,50,60用例84 2 100,1,100,1用例924 6 30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7用例107 3 4,3,2,1,2,3,4文章目录**查找最佳充电策略**题目描述输入描述输出描述示例1示例2解题思路核心思想算法步骤复杂度分析JavaPythonJavaScriptCGoC语言完整用例用例1用例2用例3用例4用例5用例6用例7用例8用例9用例10
返回列表