信息学奥赛一本通-2059:【例3.11】买笔

该博客介绍了一个C++程序,旨在帮助班长小Q用剩余的班费购买尽可能多的钢笔,确保没有剩余资金。程序考虑了三种不同价格的钢笔,并通过条件判断优化购买方案,以实现最大化钢笔数量。

【题目描述】

期末来临了,班长小Q决定将剩余班费x元钱,用于购买若干支钢笔奖励给一些学习好、表现好的同学。已知商店里有三种钢笔,它们的单价为6元、5元和4元。小Q想买尽量多的笔(鼓励尽量多的同学),同时他又不想有剩余钱。请您编一程序,帮小Q制订出一种买笔的方案。

【输入】

一个正整数x(剩余班费)。

【输出】

一行,依次为6元、5元和4元钱笔的数目,用一个空格隔开。

【输入样例】

10

【输出样例】

1 0 1

------------------------------

买较多的笔,则先全买4元的笔共c支,剩余d元钱:

d要考虑3种情况:

1->d=1时,将一只4元笔加上这一元重新买一只五元笔,c-1,b+1;

2->d=2时,将一只4元笔加上这两元重新买一只六元笔,c-1,a+1;

3->d=3时,将两只4元笔加上这三元重新买一只五元笔,一只六元笔,c-2,a+1,b+1。

------------------------------

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	scanf("%d",&x);
	int a=0,b=0,c=x/4,d=x%4;
	if(d==1)
	c--,b++;
	else if(d==2)
	c--,a++;
	else if(d==3)
	c-=2,a++,b++;
	printf("%d %d %d",a,b,c);
	return 0;
}
你提供的这段 GitHub Actions `steps` 是一个典型的 **Windows 平台专用构建流程**,用于: - 检出代码 - 设置 Python 3.11 - 执行 PowerShell 脚本打包 Windows 可移植程序(portable app) - 上传构建产物 下面我将 **逐行解释其功能、潜在问题与优化建议**,并提供完整改进--- ## ✅ 一、逐行解析与说明 ```yaml - name: Checkout uses: actions/checkout@v4 ``` > 使用官方 Action 检出仓库源码。这是所有 CI 流程的第一步。 ✅ 推荐使用 `@v4`,支持 Git LFS 和子模块(如有需要可配置)。 --- ```yaml - name: Set up Python 3.11 uses: actions/setup-python@v5 with: python-version: "3.11" ``` > 在运行器上安装指定本的 Python(这里是 3.11)。 📌 注意: - 如果你的脚本依赖其他包(如 `pyinstaller`, `requests`),你需要后续用 `pip install` 安装。 - 推荐添加缓存以加速依赖安装(见后文优化部分)。 --- ```yaml - name: Show env run: | python --version powershell $PSVersionTable ``` > 输出当前环境信息,便于调试。 ✅ 建议保留此步骤用于排查兼容性问题,特别是当你遇到执行策略或模块加载失败时。 输出示: ```text Python 3.11.8 Name Value ---- ----- PSVersion 7.4.3 PSEdition Core GitCommitId 7.4.3 OS Microsoft Windows 10.0.20348 Platform Win32NT ``` --- ```yaml - name: Build Windows portable bundle shell: powershell run: | Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass .\scripts\windows\build_windows_portable.ps1 ``` > 关键构建步骤:使用 PowerShell 运行本地构建脚本。 🔍 分析: - `shell: powershell` 明确指定使用 PowerShell(而不是默认的 `pwsh` 或 `bash`) - `Set-ExecutionPolicy ...` 允许当前进程运行未签名脚本 —— 必要操作,否则 `.ps1` 脚本会被阻止 - `.\scripts\windows\build_windows_portable.ps1` 是项目中的自定义构建脚本,常会调用 PyInstaller 等工具生成 `.exe` ⚠️ 风险提示: - 此脚本必须存在于仓库中且路径正确 - 若脚本出错但未设置 `set -e` 或等效机制,CI 可能误报成功 ✅ 改进建议:增强错误处理 ```yaml run: | Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force $ErrorActionPreference = &#39;Stop&#39; # 关键:让任何错误中断流程 .\scripts\windows\build_windows_portable.ps1 ``` --- ```yaml - name: Upload artifact (Windows Offline Bundle) uses: actions/upload-artifact@v4 with: name: AI_Manager_Windows_Offline path: dist/AI_Manager_Windows_Offline/** if-no-files-found: error ``` > 将构建生成的文件上传为 GitHub Artifact,供下载或后续部署使用。 🔧 参数详解: - `name`: 下载时显示的名字 - `path`: 要上传的目录内容(支持 glob 模式) - `if-no-files-found: error`:如果没有找到匹配文件,则标记 job 失败 —— **强烈推荐!防止静默失败** 💡 提示: - 构建脚本必须确保输出目录是 `dist/AI_Manager_Windows_Offline/` - 否则此处会触发 `error` 并中断 workflow --- ## 🛠️ 二、完整优化建议本 ```yaml name: Build Windows Portable on: push: branches: [ main, master ] workflow_dispatch: # 允许手动触发 jobs: build-windows: runs-on: windows-latest strategy: matrix: python-version: [&#39;3.11&#39;] steps: - name: Checkout code uses: actions/checkout@v4 - name: Setup Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Cache pip packages uses: actions/cache@v3 with: path: ~\AppData\Local\pip\Cache key: ${{ runner.os }}-pip-${{ hashFiles(&#39;requirements.txt&#39;) }} restore-keys: | ${{ runner.os }}-pip- - name: Install dependencies shell: powershell run: | python -m pip install --upgrade pip if (Test-Path requirements.txt) { pip install -r requirements.txt } - name: Show environment run: | python --version Write-Host "PowerShell Version:" $PSVersionTable - name: Build Windows portable bundle shell: powershell env: PYTHONPATH: ${{ github.workspace }} run: | Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force $ErrorActionPreference = &#39;Stop&#39; .\scripts\windows\build_windows_portable.ps1 - name: Upload artifact (Windows Offline Bundle) uses: actions/upload-artifact@v4 with: name: AI_Manager_Windows_Offline path: dist/AI_Manager_Windows_Offline/** if-no-files-found: error ``` --- ## 🔍 三、关键优化点说明 | 优化项 | 说明 | |-------|------| | `workflow_dispatch` | 支持手动点击运行(方便测试) | | 缓存 `pip` | Windows 的 pip 缓存路径是 `%LOCALAPPDATA%\pip\Cache` → 对应 `~\AppData\Local\pip\Cache` | | 自动升级 pip | 避免旧 pip 导致安装失败 | | `Test-Path requirements.txt` | 更健壮地判断是否存在依赖文件 | | `$ErrorActionPreference = &#39;Stop&#39;` | 让 PowerShell 在出错时立即退出 | | 设置 `PYTHONPATH` | 确保脚本能找到项目模块 | --- ## 💡 四、常见问题排查清单 | 问题现象 | 可能原因 | 解决方案 | |--------|---------|----------| | `.ps1 cannot be loaded because running scripts is disabled` | 执行策略未绕过 | 加 `Set-ExecutionPolicy` | | `build_windows_portable.ps1 : File not found` | 路径错误或换行符问题 | 检查文件是否存在,`.gitattributes` 是否设为 `CRLF` | | `No files were found with the provided path` | 输出目录名不符 | 检查脚本是否真生成了 `dist/AI_Manager_Windows_Offline/` | | `PyInstaller not found` | 未安装依赖 | 在 `Install dependencies` 步骤加入 `pip install pyinstaller` | --- ## 📦 五、后续扩展建议 ### 1. 添加测试步骤 ```yaml - name: Run unit tests shell: powershell run: pytest tests/ ``` ### 2. 构建完成后发送知 ```yaml - name: Send Discord notification on success if: success() run: curl -d "content=✅ Build succeeded!" ${{ secrets.DISCORD_WEBHOOK }} ``` ### 3. 自动发布到 GitHub Releases(当打 tag 时) ```yaml if: startsWith(github.ref, &#39;refs/tags/&#39;) uses: softprops/action-gh-release@v1 with: files: dist/AI_Manager_Windows_Offline/** ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值