1652. 拆炸弹

要求

你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。

为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。

  • 如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。
  • 如果 k < 0 ,将第 i 个数字用 之前 k 个数字之和替换。
  • 如果 k == 0 ,将第 i 个数字用 0 替换。

由于 code 是循环的, code[n-1] 下一个元素是 code[0] ,且 code[0] 前一个元素是 code[n-1] 。

给你 循环 数组 code 和整数密钥 k ,请你返回解密后的结果来拆除炸弹!

示例1

输入:code = [5,7,1,4], k = 3
输出:[12,10,16,13]
解释:每个数字都被接下来 3 个数字之和替换。解密后的密码为 [7+1+4, 1+4+5, 4+5+7, 5+7+1]。注意到数组是循环连接的。

示例2

输入:code = [1,2,3,4], k = 0
输出:[0,0,0,0]
解释:当 k 为 0 时,所有数字都被 0 替换。

示例3

输入:code = [2,4,9,3], k = -2
输出:[12,5,6,13]
解释:解密后的密码为 [3+9, 2+3, 4+2, 9+4] 。注意到数组是循环连接的。如果 k 是负数,那么和为 之前 的数字。

实现思路

正常利用取余计算就好,注意为负数时循环次数

实现代码

class Solution {
public:
    vector<int> decrypt(vector<int>& code, int k) {
        int i,j,s,len = code.size();
        vector<int> v;
        for(i = 0 ;i<len;++i){
                if(k == 0)
                    v.push_back(0);
                else if(k > 0){
                    s = 0;
                    for(j=1;j <=k;++j)
                        s+= code[(i + j)%len];
                    v.push_back(s);
                }
                else{
                    s = 0;
                    for(j = 1;j<=-k;++j)
                        s += code[(i - j + len )%len];
                    v.push_back(s);
                }
        }
        return v;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个使用Cocos Creator和TypeScript创建炸弹人游戏的示例代码: ``` const { ccclass, property } = cc._decorator; enum ItemType { BombRange, SpeedUp } @ccclass export default class BombMan extends cc.Component { @property(cc.Prefab) private playerPrefab: cc.Prefab = null; @property(cc.Prefab) private enemyPrefab: cc.Prefab = null; @property(cc.Prefab) private itemPrefab: cc.Prefab[] = []; @property(cc.SpriteFrame) private brickSprite: cc.SpriteFrame = null; private player: cc.Node = null; private enemies: cc.Node[] = []; private items: cc.Node[] = []; private map: number[][] = []; private gridSize: number = 45; private gridRows: number = 15; private gridCols: number = 15; private bombRange: number = 1; private playerSpeed: number = 200; onLoad() { this.generateMap(); this.spawnPlayer(); this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } generateMap() { for (let i = 0; i < this.gridRows; i++) { this.map[i] = []; for (let j = 0; j < this.gridCols; j++) { let tileType = Math.random() < 0.8 ? 0 : 1; this.map[i][j] = tileType; if (tileType === 1) { let brickNode = new cc.Node(); let sprite = brickNode.addComponent(cc.Sprite); sprite.spriteFrame = this.brickSprite; brickNode.parent = this.node; brickNode.setPosition(j * this.gridSize, i * this.gridSize); } } } } spawnPlayer() { this.player = cc.instantiate(this.playerPrefab); this.player.parent = this.node; this.player.setPosition(this.gridSize / 2, this.gridSize / 2); } spawnEnemy() { let enemyNode = cc.instantiate(this.enemyPrefab); enemyNode.parent = this.node; // 随机生成敌人的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); enemyNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.enemies.push(enemyNode); // 随机生成敌人的生成时间间隔 this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); } spawnItem() { let itemType = Math.floor(Math.random() * this.itemPrefab.length); let itemNode = cc.instantiate(this.itemPrefab[itemType]); itemNode.parent = this.node; // 随机生成道具的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); itemNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.items.push(itemNode); // 随机生成道具的生成时间间隔 this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } // ... } ``` 这只是一个简单的示例代码,包含了地图的生成、玩家、敌人和道具的生成。你可以根据自己的需求进一步完善游戏逻辑和其他功能。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值