酷代码 AI
菜单
服务商

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]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]