天梯赛题解(3-6)

前五题都是非常简单的题,就不写解析了

1-3 帮助色盲

cpp 复制代码
#include<iostream>
using namespace std;

int main(){
	int a, b;
	cin >> a >> b;
	if(b == 1){
		if(a == 1) cout << "-" << endl << "move";
		else if(a == 2) cout << "-" << endl << "stop";
		else cout << "-" << endl << "stop"; 
	}
	else if(b == 0){
		if(a == 1) cout << "dudu" << endl << "move";
		else if(a == 2) cout << "-" << endl << "stop";
		else cout << "biii" << endl << "stop";
	}
	return 0;
} 

1-4

注意有可能值为负,所以拿不了满分
cpp 复制代码
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n, m, k; 
int ans = 0;

signed main()
{
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> k;
        ans += k;
    }
    int diff = ans - n * (m - 1);
    cout << max((long long)0, diff) << endl;
    return 0;
}

1-5 程序员买包子

cpp 复制代码
#include<iostream>
using namespace std;

int main(){
	int n, m, k;
	string x;
	cin >> n >> x >> m >> k;
	if(k == n) cout << "mei you mai " << x << " de";
	else if(k == m) cout << "kan dao le mai " << x << " de";
	else cout << "wang le zhao mai " << x << " de";
	return 0;
} 

1-6 试试手气

有可能有更简单的方法。。。
cpp 复制代码
#include<iostream>
using namespace std;

// 全局二维数组 b[6][7]:
// 第一维 [0-5] → 对应6个骰子的编号(第0个到第5个骰子)
// 第二维 [0-6] → 对应骰子点数(0位闲置不用,1-6对应骰子的6个面)
// 标记规则:b[骰子编号][点数] = 1 表示该骰子已出现过这个点数;=0 表示未出现过
int b[6][7];

int main(){
	// 数组a[6]:存储6个骰子「当前的点数」,最终会更新为第n次摇出的结果
	int a[6];
	// 变量n:需要模拟摇骰子的次数(题目要求输出第n次的结果)
	int n;
	
	// 第一步:读取6个骰子的初始点数,并初始化「点数出现标记数组」b
	for(int i = 0; i < 6; i++){
		cin >> a[i];               // 输入第i个骰子的初始点数,存入a[i]
		b[i][a[i]] = 1;            // 标记:第i个骰子已出现过a[i]这个点数
	}
	
	// 第二步:读取需要模拟的摇骰子次数n
	cin >> n;

	// 第三步:核心逻辑 → 模拟摇n次骰子的过程
	// while(n--) 等价于循环n次(每循环一次n减1,直到n=0停止)
	while(n--){
		// 遍历6个骰子,逐个更新本次摇出的点数
		for(int k = 0; k < 6; k++){
			// 从6到1倒序查找:优先选「最大的、且未出现过」的点数(满足题目规则)
			for(int j = 6; j > 0; j--){
				// 如果第k个骰子从未出现过点数j
				if(b[k][j] == 0){
					a[k] = j;        // 更新第k个骰子的当前点数为j
					b[k][j] = 1;     // 标记该点数已使用,避免后续重复
					break;           // 找到最大可用点数后,跳出内层循环,处理下一个骰子
				}
			}	
		}
	}
	
	// 第四步:输出第n次摇出的结果(优化格式:行首尾无多余空格)
	for(int i = 0; i < 6; i++){
		if(i!=5)                  // 前5个骰子(0-4号):输出点数后加空格
            cout << a[i] << " ";
        else                     // 第6个骰子(5号):仅输出点数,不加空格
            cout << a[i];
	}
	
	return 0;
}
相关推荐
BothSavage9 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn9 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽11 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试