Pascal 代码揭秘:小 X 糖果促销与小 R 购买策略的最优定价方案
```pascal program CandyPricing; const MODULUS = 998244353; type TCandy = record id: integer; price: integer; clearPrice: integer; ratio: real; end; TCandyArray = array of TCandy; function CompareCandies(const a, b: TCandy): integer; begin if a.ratio <> b.ratio then begin if a.ratio > b.ratio then CompareCandies := -1 else CompareCandies := 1; end else if a.price <> b.price then begin if a.price > b.price then CompareCandies := -1 else CompareCandies := 1; end else if a.id <> b.id then begin if a.id < b.id then CompareCandies := -1 else CompareCandies := 1; end else CompareCandies := 0; end; function QuickSort(var arr: TCandyArray; left, right: integer); var i, j: integer; pivot, temp: TCandy; begin i := left; j := right; pivot := arr[(left + right) div 2]; repeat while CompareCandies(arr[i], pivot) < 0 do Inc(i); while CompareCandies(arr[j], pivot) > 0 do Dec(j); if i <= j then begin temp := arr[i]; arr[i] := arr[j]; arr[j] := temp; Inc(i); Dec(j); end; until i > j; if left < j then QuickSort(arr, left,j); if i < right then QuickSort(arr, i, right); end; function CalculateTotalPrice(candies: TCandyArray; money: integer): integer; var i: integer; total: integer; begin total := 0; for i := Low(candies ) to High(candies ) do begin if money >= candies[i].clearPrice then begin total := total + candies[i].price; money := money - candies[i].clearPrice; end; end; CalculateTotalPrice := total; end; function CountOptimalPricingSchemes(prices: array of integer; money: integer): integer; var n, i, j, maxTotal, count: integer; candies: TCandyArray; scheme: integer; total: integer; begin n := Length(prices); maxTotal := 0; count := 0; for scheme := 0 to (1 shl n) - 1 do begin SetLength(candies, n); for i := 0 to n - 1 do begin candies[i].id := i + i; candies[i].price := prices[i]; if (scheme and (1 shl i)) = 0 then candies[i].clearPrice := i else candies[i].clearPrice := 2; candies[i].ratio := prices[i] / candies[i].clearPrice; end; QuickSort(candies, 0, High(candies)); total := CalculateTotalPrice(candies , money); if total > maxTotal then begin maxTotal := total; count := 1; end else if total = maxTotal then Inc(count); end; CountOptimalPricingSchemes := count mod MODULUS; end; var c, t, n, m, i: integer; prices: array of integer; begin readln(c, t); for i := 1 to t do begin readln(n, m); SetLength(prices, n); for j := 0 to n - 1 do read(prices[j]); readln; writeln(CountOptimalPricingSchemes(p rices, m)); end; end. ``` 代码解释 1. 常量和类型定义: - `MODULUS` 用于取模操作。 - `TCandy` 记录每颗糖果的编号、原价、清仓价和性价比 - `TCandyArray` 是糖果数组类型。 2. 比较函数 `CompareCandies`:根据性价比、原价和编号对糖果进行排序。 3. 快速排序函数 `QuickSort`:对糖果数组进行排序。 4. 计算总原价函数 `CalculateTotalPrice`:根据小 R 的购买策略计算购买糖果的原价总和。 5. 计算最优定价方案数函数 `CountOptimalPricingSchemes`:枚举所有定价方案,计算每种方案下小 R 购买糖果的原价总和,统计使原价总和最大的方案数。 6. 主程序:读取输入数据,调用 `CountOptimalPricingSchemes` 函数计算并输出结果。 ######[AI问答 | 1221点数解答 | 2025-12-10 22:18:52]
- 苹果总价计算:n元/箱 × m箱 = 总金额(阿里通义 | 296点数解答 | 2026-03-13 12:10:22)12
- C++代码:定义图书链表结构与相关数据类型(字节豆包 | 35点数解答 | 2024-10-31 17:38:15)262
- 工程师必备:十大APP性能优化方案大揭秘(百度文心 | 300点数解答 | 2023-11-09 18:31:04)338
- 含不规则缺陷原材料的最优切割方案建模与损失率、利用率计算(DeepSeek | 2367点数解答 | 2025-05-10 23:47:30)266
- 开启多语言编程模式:C++、Python、Pascal、Java、C语言为你解答问题!(字节豆包 | 33点数解答 | 2025-12-13 19:57:46)56
- 用Pascal和C语言解单调方程:求最小正小数x使S(x)>y(DeepSeek | 1664点数解答 | 2025-12-17 17:52:32)35
- 编程五语齐上阵!满足你问题的多样解答需求(DeepSeek | 34点数解答 | 2025-12-27 19:46:30)52
- Pascal实现电视节目观看数量最大化问题的解决方案(字节豆包 | 587点数解答 | 2026-03-18 18:01:57)21
- Free Pascal瑞士轮AC代码:适配fpc 3.0.4的qsort与循环变量修复方案(阿里通义 | 1396点数解答 | 2026-03-20 12:31:01)20
- Free Pascal 3.0.4 下瑞士轮排序的终极 AC 解决方案:qsort 显式声明与循环变量隔离(阿里通义 | 1216点数解答 | 2026-03-20 12:32:11)21
- Free Pascal qsort 正确声明指南:解决 “Type identifier expected” 编译错误(阿里通义 | 1227点数解答 | 2026-03-20 12:33:58)21
- Free Pascal 快速排序编译错误修复指南:解决“var 声明在 begin 内”的致命语法问题(阿里通义 | 1967点数解答 | 2026-03-24 18:05:06)18