c++输入一个整数判断是否为完全平方数_四平方和 剪枝+枚举 【蓝桥真题】(c++)...

b7af73d00e34b3d0b28f13c5c71f31ac.png

四平方和

四平方和定理,又称为拉格朗日定理:

每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。

比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法

程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 3000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

我的思路

  • 首先分析出题目要求,四个数a、b、c、d的平方和要等于n并且输出时要按照大小a<=b<=c<=d输出,根据该题目要求,建立四个枚举循环,每两个循环中都进行一次保存,最后通过平方和比较记录结果,注释已附在算法中。

算法展示

#include <iostream>
#include <map>
#include <string>
using namespace std;
int N,a,b,c,d;
//存储c,d的值
struct cd{
    int c;
    int d;
};

map <int,cd>cache;//int存储a、b、c、d中cd的平方和,cd存储c、d的值

int main() {
    cin>>N;
    //存储c、d值
    for(c=0;c*c<=(N/2);c++)//因为a<=b<=c<=d,假设a==b==0,则c的平方最大等于d的平方,即c的平方小于等于N的二分之一
    {
        for(d=c;d*d<=N;d++)
        {
            cd c_d = {c,d};
            cache[N-c*c-d*d]= c_d;
        }
    }
    
    //比较并打印结果
    for(a=0;a<=(N/4);a++)
    {
        for(b=0;b<=(N/3);b++)
        {
            if(cache.find(a*a+b*b)!=cache.end())//找到N-c*c-d*d==a*a+b*b,且此时不是map末尾
            {
                cd cd = cache[a*a+b*b];
                cout<<a<<" "<<b<<" "<<cd.c<<" "<<cd.d<<endl;
                return 0;
            }
        }
    }
        
    return 0;
}

上文链接:剪邮票 深搜+标记 【蓝桥真题】(c++实现)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个C++ 实现的五子棋 AI,使用了 Alpha-Beta 剪枝算法: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; const int BOARD_SIZE = 15; // 棋盘大小 const int MAX_DEPTH = 3; // 最大搜索深度 const int WIN_SCORE = 1000000; // 获胜得分 const int INF = 0x3f3f3f3f; // 无穷大 int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘 int player; // 玩家 int ai; // AI // 判断是否越界 bool isInRange(int x, int y) { return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE; } // 判断是否为空位 bool isEmpty(int x, int y) { return board[x][y] == 0; } // 判断是否为玩家棋子 bool isPlayer(int x, int y) { return board[x][y] == player; } // 判断是否为 AI 棋子 bool isAI(int x, int y) { return board[x][y] == ai; } // 在 (x,y) 处落子 void move(int x, int y, int chess) { board[x][y] = chess; } // 悔棋 void unmove(int x, int y) { board[x][y] = 0; } // 检查是否五子连珠 bool checkWin(int x, int y) { int dx[4] = {0, 1, 1, 1}; // 横、竖、斜1、斜2 int dy[4] = {1, 0, 1, -1}; for(int i = 0; i < 4; i++) { int cnt = 1; for(int j = 1; j <= 4; j++) { int nx = x + j * dx[i], ny = y + j * dy[i]; if(isInRange(nx, ny) && isAI(nx, ny)) cnt++; else break; } for(int j = 1; j <= 4; j++) { int nx = x - j * dx[i], ny = y - j * dy[i]; if(isInRange(nx, ny) && isAI(nx, ny)) cnt++; else break; } if(cnt >= 5) return true; } return false; } // 评估函数 int evaluate() { int score = 0; // 统计每个空位的得分 for(int i = 0; i < BOARD_SIZE; i++) { for(int j = 0; j < BOARD_SIZE; j++) { if(isEmpty(i, j)) { int cnt1 = 0, cnt2 = 0; int dx[4] = {0, 1, 1, 1}; // 横、竖、斜1、斜2 int dy[4] = {1, 0, 1, -1}; for(int k = 0; k < 4; k++) { int cnt = 1; for(int l = 1; l <= 4; l++) { int nx = i + l * dx[k], ny = j + l * dy[k]; if(isInRange(nx, ny) && isAI(nx, ny)) cnt++; else break; } for(int l = 1; l <= 4; l++) { int nx = i - l * dx[k], ny = j - l * dy[k]; if(isInRange(nx, ny) && isAI(nx, ny)) cnt++; else break; } if(cnt >= 5) cnt1++; else if(cnt == 4) cnt2++; } score += cnt1 * WIN_SCORE + cnt2 * (WIN_SCORE / 10); } } } return score; } // Alpha-Beta 剪枝搜索 int alphabeta(int depth, int alpha, int beta) { if(depth == 0) return evaluate(); // 叶节点,返回评估值 vector<pair<int, int>> moves; // 当前局面下所有可行的落子位置 for(int i = 0; i < BOARD_SIZE; i++) { for(int j = 0; j < BOARD_SIZE; j++) { if(isEmpty(i, j)) moves.push_back({i, j}); } } if(moves.empty()) return 0; // 没有可行的落子位置,返回 0 // 对可行的落子位置进行排序,以加速剪枝 sort(moves.begin(), moves.end(), [](const pair<int, int>& a, const pair<int, int>& b) { return evaluate() > evaluate(); }); int bestScore = -INF; // 最佳得分 for(const auto& move : moves) { int x = move.first, y = move.second; move(x, y, ai); // AI 落子 int score = -alphabeta(depth - 1, -beta, -alpha); // 对手的最佳反应是当前局面的最大负值 unmove(x, y); // 悔棋 if(score >= beta) return score; // Beta 剪枝 if(score > bestScore) { // 更新最佳得分和 Alpha 值 bestScore = score; if(score > alpha) alpha = score; } } return bestScore; } // AI 落子 void AIMove() { vector<pair<int, int>> moves; // 当前局面下所有可行的落子位置 for(int i = 0; i < BOARD_SIZE; i++) { for(int j = 0; j < BOARD_SIZE; j++) { if(isEmpty(i, j)) moves.push_back({i, j}); } } if(moves.empty()) return; // 没有可行的落子位置,直接返回 int bestScore = -INF; // 最佳得分 pair<int, int> bestMove; // 最佳落子位置 // 对可行的落子位置进行排序,以加速搜索 sort(moves.begin(), moves.end(), [](const pair<int, int>& a, const pair<int, int>& b) { return evaluate() > evaluate(); }); // 搜索最佳落子位置 for(const auto& move : moves) { int x = move.first, y = move.second; move(x, y, ai); // AI 落子 int score = -alphabeta(MAX_DEPTH - 1, -INF, INF); // 对手的最佳反应是当前局面的最大负值 unmove(x, y); // 悔棋 if(score > bestScore) { // 更新最佳得分和最佳落子位置 bestScore = score; bestMove = move; } } move(bestMove.first, bestMove.second, ai); // AI 落子 cout << "AI moves: (" << bestMove.first << ", " << bestMove.second << ")" << endl; } // 玩家落子 void playerMove() { int x, y; while(true) { cout << "Please enter your move (x y): "; cin >> x >> y; if(isInRange(x, y) && isEmpty(x, y)) break; else cout << "Invalid move, please try again." << endl; } move(x, y, player); // 玩家落子 } // 打印棋盘 void printBoard() { cout << " "; for(int i = 0; i < BOARD_SIZE; i++) { cout << i << " "; } cout << endl; for(int i = 0; i < BOARD_SIZE; i++) { cout << i << " "; for(int j = 0; j < BOARD_SIZE; j++) { if(isEmpty(i, j)) cout << "+ "; else if(isPlayer(i, j)) cout << "O "; else cout << "X "; } cout << endl; } } int main() { // 初始化 for(int i = 0; i < BOARD_SIZE; i++) { for(int j = 0; j < BOARD_SIZE; j++) { board[i][j] = 0; } } player = 1; ai = 2; // 开始游戏 while(true) { printBoard(); playerMove(); if(checkWin(player)) { cout << "You win!" << endl; break; } AIMove(); if(checkWin(ai)) { cout << "AI wins!" << endl; break; } } return 0; } ``` 这个五子棋 AI 使用 Alpha-Beta 剪枝算法进行搜索,评估函数采用了一种简单的方法,即统计每个空位的得分,得分越高的空位越有可能是最佳落子位置。这个 AI 的搜索深度可以通过修改 `MAX_DEPTH` 来调整,可以根据自己的需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值