PAT 2-08. 用扑克牌计算24点(25):

本文介绍了一种解决24点游戏的算法实现方案,通过暴力枚举的方式,利用五种不同的运算组合来寻找能够得到24的计算路径。代码采用C语言编写,并详细展示了如何通过递归和条件判断来实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://www.patest.cn/contests/ds/2-08

解题思路:思路参考24点游戏技巧http://www.24game.com.cn/articles/points24-game-tips-grade6.html

     方法为:暴力枚举每次所选的数字和运算符的五种不同运算方式

五种不同运算方式如下(括号的五种不同组合):

  ((a1 op1 a2) op2 a3) op3 a4

  (a1 op1 (a2 op2 a3)) op3 a4

  (a1 op1 a2) op2 (a3 op3 a4)

  a1 op1 ((a2 op2 a3) op3 a4)

  a1 op1 (a2 op2 (a3 op3 a4))

代码如下:

#include <cstdio>  
  
char op[5]= {'#','+','-','*','/',};  
  
double cal(double x,double y,int op)  
{  
    switch(op)  
    {  
    case 1:  
        return x+y;  
    case 2:  
        return x-y;  
    case 3:  
        return x*y;  
    case 4:  
        return x/y;  
    }  
}  
  
double cal_m1(double i,double j,double k,double t,int op1,int op2,int op3)  
{  
    double r1,r2,r3;  
    r1 = cal(i,j,op1);  
    r2 = cal(r1,k,op2);  
    r3 = cal(r2,t,op3);  
    return r3;  
}  
  
double cal_m2(double i,double j,double k,double t,int op1,int op2,int op3)  
{  
    double r1,r2,r3 ;  
    r1 = cal(i,j,op1);  
    r2 = cal(k,t,op3);  
    r3 = cal(r1,r2,op2);  
    return r3;  
}  
  
double cal_m3(double i,double j,double k,double t,int op1,int op2,int op3)  
{  
    double r1,r2,r3;  
    r1 = cal(j,k,op2);  
    r2 = cal(i,r1,op1);  
    r3 = cal(r2,t,op3);  
    return r3;  
}  
  
double cal_m4(double i,double j,double k,double t,int op1,int op2,int op3)  
{  
    double r1,r2,r3 ;  
    r1 = cal(k,t,op3);  
    r2 = cal(j,r1,op2);  
    r3 = cal(i,r2,op1);  
    return r3;  
}  
  
double cal_m5(double i,double j,double k,double t,int op1,int op2,int op3)  
{  
    double r1,r2,r3;  
    r1 = cal(j,k,op2);  
    r2 = cal(r1,t,op3);  
    r3 = cal(i,r2,op1);  
    return r3;  
}  
  
int get_24(int i,int j,int k,int t)  
{  
    for(int op1 = 1; op1 <= 4; op1++)  
    {  
        for(int op2 = 1; op2 <= 4; op2++)  
        {  
            for(int op3 = 1; op3 <= 4; op3++)  
            {  
                if(cal_m1(i,j,k,t,op1,op2,op3) == 24)  
                {  
                    printf("((%d%c%d)%c%d)%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);  
                    return 1;  
                }  
                if(cal_m2(i,j,k,t,op1,op2,op3) == 24)  
                {  
                    printf("(%d%c%d)%c(%d%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);  
                    return 1;  
                }  
                if(cal_m3(i,j,k,t,op1,op2,op3) == 24)  
                {  
                    printf("(%d%c(%d%c%d))%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);  
                    return 1;  
                }  
                if(cal_m4(i,j,k,t,op1,op2,op3) == 24)  
                {  
                    printf("%d%c(%d%c(%d%c%d))\n",i,op[op1],j,op[op2],k,op[op3],t);  
                    return 1;  
                }  
                if(cal_m5(i,j,k,t,op1,op2,op3) == 24)  
                {  
                    printf("%d%c((%d%c%d)%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);  
                    return 1;  
                }  
  
            }  
        }  
    }  
    return 0;  
}  
  
int main()  
{  
    int a[4];  
    int t1, t2, t3, t4;  
    int flag;  
    for(int i = 0; i < 4; i++)  
        scanf("%d",&a[i]);  
    for(int i = 0; i < 4; i++)  
    {  
        for(int j = 0; j < 4; j++)  
        {  
            if(j==i)  
                continue;  
            for(int k = 0; k < 4; k++)  
            {  
                if(i==k||j==k)  
                    continue;  
                for(int t = 0; t < 4; t++)  
                {  
                    if(t==i||t==j||t==k)  
                        continue;  
                    t1 = a[i], t2= a[j], t3= a[k], t4= a[t];  
  
                    flag = get_24(t1,t2,t3,t4);  
                    if(flag ==1)  
                        break;  
                }  
                if(flag == 1)  
                    break;  
            }  
            if(flag == 1)  
                break;  
        }  
        if(flag == 1)  
            break;  
    }  
    if(flag == 0)  
        printf("-1\n");  
  
    return 0;  
}  

  

暴力枚举代码转载自:http://blog.youkuaiyun.com/u012860063/article/details/40435363

  

  

 

          

转载于:https://www.cnblogs.com/CHLL55/p/4328365.html

<think>我们正在解决用户在使用 `git remote add` 命令时遇到错误提示 `fatal: remote origin already exists.` 的问题。根据引用内容,我们可以总结出以下步骤: 1. **删除已存在的远程仓库**:使用 `git remote rm origin` 命令删除名为 "origin" 的远程仓库。 2. **重新添加远程仓库**:使用 `git remote add origin <url>` 命令重新添加远程仓库(注意替换为正确的仓库URL)。 但是,引用[3]提到,如果执行 `git remote rm origin` 后仍然报错(`error: Could not remove config section 'remote.origin'`),则需要手动修改git配置文件(.git/config)来删除对应的配置。 因此,我们将解决步骤分为两种情况: **情况一:正常删除** - 步骤1: 执行 `git remote rm origin` - 步骤2: 执行 `git remote add origin <你的仓库URL>` **情况二:删除失败(报错无法删除配置节)** - 步骤1: 手动编辑项目目录下的 `.git/config` 文件,删除 `[remote "origin"]` 整个配置节。 - 步骤2: 再执行 `git remote add origin <你的仓库URL>` 另外,引用[2]也提供了相同的解决方案(先删除再添加)[^2]。 下面我们详细说明操作步骤: ### 解决步骤: 1. **检查当前远程仓库**(可选,用于确认): ```bash git remote -v ``` 如果已经存在名为 "origin" 的远程仓库,它会显示其URL。 2. **删除已存在的远程仓库**: ```bash git remote rm origin ``` 如果成功,不会有任何输出。再次运行 `git remote -v` 确认 "origin" 已被删除。 3. **如果步骤2报错**(如引用[3]所述): 报错信息:`error: Could not remove config section 'remote.origin'` 此时,需要手动编辑配置文件: - 进入项目根目录下的 `.git` 文件夹(注意:该文件夹默认隐藏) - 打开 `config` 文件(文本编辑器即可) - 找到类似以下的部分并删除: ```ini [remote "origin"] url = https://github.com/your-username/your-repo.git fetch = +refs/heads/*:refs/remotes/origin/* ``` 保存文件后,再执行步骤4。 4. **重新添加远程仓库**: ```bash git remote add origin <你的仓库URL> ``` 例如: ```bash git remote add origin git@github.com:your-username/your-repo.git ``` 或者使用HTTPS URL: ```bash git remote add origin https://github.com/your-username/your-repo.git ``` 5. **验证**: ```bash git remote -v ``` 应该显示新添加的远程仓库URL。 6. **推送代码**(如果需要): ```bash git push -u origin master # 或者你的分支名 ``` ### 注意事项: - 如果之前已经配置过远程仓库,并且你只是想更改URL(而不改变远程仓库名称),可以使用: ```bash git remote set-url origin <新的URL> ``` 这样就不需要先删除再添加了。 ### 引用说明: - 引用[2]提供了先删除后添加的解决方案[^2]。 - 引用[3]提供了当删除命令失败时手动修改配置文件的解决方案[^3]。 ### 常见问题: - **问**:为什么会出现这个错误? **答**:因为当前本地仓库已经配置了一个名为 "origin" 的远程仓库。每个远程仓库名称必须唯一。 - **问**:除了 "origin",我还能用其他名字吗? **答**:可以,例如你可以使用 `git remote add myorigin <url>`,但通常使用 "origin" 作为默认远程仓库名。 §§相关问题§§ 1. Git 如何修改现有远程仓库的URL? 2. Git 远程仓库管理常用命令有哪些? 3. 如何解决 Git 推送代码到远程仓库时出现的权限问题? 以上步骤应该能解决用户的问题。如果遇到其他问题,请检查网络连接、仓库URL是否正确以及是否有权限访问该仓库。</think>### 解决 `git remote add` 报错 `fatal: remote origin already exists` 当执行 `git remote add origin <URL>` 时出现此错误,说明本地仓库已存在名为 `origin` 的远程配置。以下是完整解决方案: --- #### 步骤 1:验证现有远程配置 ```bash git remote -v ``` 检查输出是否包含名为 `origin` 的远程仓库(如已有则需处理)[^2]。 --- #### 步骤 2:删除已存在的远程仓库 ```bash git remote remove origin ``` 或使用旧版本 Git 的等效命令: ```bash git remote rm origin ``` > ✅ 此操作仅删除本地配置,不影响远程仓库[^2]。 --- #### 步骤 3:手动修复配置文件(如删除失败) 当出现 `error: Could not remove config section 'remote.origin'` 时: 1. 打开项目目录下的 `.git/config` 文件 2. 删除包含 `[remote "origin"]` 的整个配置节: ```ini [remote "origin"] url = git@github.com:xxx/xxx.git fetch = +refs/heads/*:refs/remotes/origin/* ``` 3. 保存文件[^3]。 --- #### 步骤 4:重新添加远程仓库 ```bash git remote add origin <你的仓库URL> ``` 正确格式示例: ```bash # SSH 格式 git remote add origin git@github.com:yourname/repo.git # HTTPS 格式 git remote add origin https://github.com/yourname/repo.git ``` --- #### 步骤 5:验证并推送代码 ```bash git remote -v # 确认配置正确 git push -u origin main # 首次推送 ``` --- ### 关键注意事项 1. **URL 格式必须正确**: - GitHub 的 SSH URL 格式:`git@github.com:user/repo.git` - 斜杠 `/` 错误会导致推送失败[^1] 2. **避免重复操作**: ```bash # 直接修改现有远程 URL(无需删除) git remote set-url origin <新URL> ``` 3. **权限问题**: - SSH 方式需提前配置密钥 - HTTPS 方式需账户密码或 PAT 令牌 > 通过以上步骤可解决 99% 的 `remote origin already exists` 问题[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值