关于 i++ 和 ++i ,以及在函数中使用 ++ 的重要思考

关于i++和++i的重要思考

先看下面这个程序:

public class PPTest {
    public static void main(String[] args) {
        int x=0;
        System.out.println("函数外x为:"+x);
        test(x++);
        System.out.println("现在x是:"+x);
        test(++x);
        System.out.println("现在x是:"+x);
    }

    public static void test(int x) {
        System.out.println("函数内x为:"+x);
    }
}

image-20210317174231290

看出问题了吗?

没错,函数中,参数调用++,那外部数据也会变化

而且,如果是后++,那么内部函数,调用的是++前的值!

这样的问题,在写回溯相关的问题的时候,会产生重大的影响!!!!



如果要在函数里加,必须写成i+1的形式

public class PPTest {
    public static void main(String[] args) {
        int x=0;
        System.out.println("函数外x为:"+x);
        test(x+1);
        System.out.println("现在x是:"+x);
    }

    public static void test(int x) {
        System.out.println("函数内x为:"+x);
    }
}

image-20210317174601579

md,没想到这个时候还会被++的问题困扰

五子棋人工智能(AI)的C++代码通常会包含以下几个关键部分: 1. **游戏规则引擎**:用于判断当前棋盘状态是否满足胜利条件,如形成连续的五个同色棋子。 2. **搜索算法**:比如最小最大搜索(MiniMax)、Alpha-Beta剪枝,甚至是更复杂的像蒙特卡洛树搜索(MCTS),它模拟大量随机走法以评估潜在的最优策略。 3. **评估函数**:AI需要一种方法来给棋局打分,通常是基于棋子的位置、形状和对手可能的反击等。 4. **下棋决策**:结合搜索结果和评估函数,选择最佳下一步棋。 5. **用户界面**:接收用户的输入,展示棋盘状态,并显示AI的行动。 编写这样的代码需要对计算机科学和博弈论有深入理解,特别是对于搜索算法和数据结构的运用。这里提供一个简化版的框架: ```cpp #include <iostream> #include <vector> // 棋盘表示 struct Board { std::vector<std::string> grid; }; // AI函数原型 int minimax(Board& board, bool isPlayer); // 主函数示例 int main() { Board board = ... // 初始化棋盘 int move = minimax(board, false); // 开始AI思考并获取其移动 printMove(move); // 输出AI的移动 return 0; } // MiniMax核心算法 int minimax(Board& board, bool isPlayer) { if (isGameOver(board)) { return evaluateBoard(board); } int bestScore = -INFINITY; for (int i = 0; i < numMoves; i++) { makeMove(i, board); bestScore = std::max(bestScore, minimax(board, !isPlayer)); undoMove(); // 回溯到上一步 } return bestScore; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FARO_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值