zoukankan      html  css  js  c++  java
  • 15年蓝桥杯第3题

    题意:

    给出下式:
       祥 瑞 生 辉
    + 三 羊 献 瑞
    -----------------
    三 羊 生 瑞 气
    其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
    请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

    思路:dfs。依次深搜八个汉字代表的数字。注意不同的汉字代表的数字必须是不同得,保证数字合法的话判断两个数字的第一个数都不能是0。不检查也是正确答案。

    简单的dfs,我自己写应该也写不出来。

    附right代码:

     1 /*
     2 给出下式:
     3    祥 瑞 生 辉
     4 +  三 羊 献 瑞
     5 -----------------
     6 三 羊 生 瑞 气
     7 其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
     8 请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
     9 dfs。依次深搜八个汉字代表的数字。
    10  */
    11 
    12 #include <stdio.h>
    13 #include <string.h>
    14 #include <iostream>
    15 using namespace std;
    16 
    17 //祥瑞生辉三羊献气 分别代表 a[0] - a[7]
    18 
    19 int a[10];
    20 int ans;
    21 
    22 bool check() {
    23     for (int i=0; i<8; ++i) {
    24         for (int j=0; j<8; ++j) {
    25             if (a[i] == a[j] && i != j) return false;
    26         }
    27     }
    28     return true;
    29 }
    30 
    31 void dfs(int cnt) {
    32     if (cnt > 8) return;
    33     if (cnt == 8) {
    34         int num1 = a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
    35         int num2 = a[4]*1000 + a[5]*100 + a[6]*10 + a[1];
    36         int num3 = a[4]*10000 + a[5]*1000 + a[2]*100 + a[1]*10 + a[7];
    37         //if (a[0] == 0 || a[4] == 0 || !check()) return;
    38         if (!check()) return;
    39         if (num3 == num1 + num2) {
    40             ans = num2;
    41             return;
    42         }
    43     }
    44     for (int i=0; i<=9; ++i) {
    45         a[cnt] = i;
    46         dfs(cnt+1);
    47     }
    48     return;
    49 }
    50 
    51 int main() {
    52     dfs(0);
    53     cout << ans << endl;
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    [macOS] git忽略所有的.DS_Store文件
    [macOS] finder变慢提速
    [React Native] change port when running react native
    转载: 我如何使用 Django + Vue.js 快速构建项目
    MySQL Connector/NET 使用小结(踩坑之路)
    C# 控制台程序(Console Application )启动后隐藏
    解决 pycharm can not save setting
    ubuntu 16.04 LTS 安装 teamviewer 13

    Python 编程规范梳理
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5228938.html
Copyright © 2011-2022 走看看