• 马虎的算式


     
    小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。
    
    有一次,老师出的题目是:36 x 495 = ?
    
    他却给抄成了:396 x 45 = ?
    
    但结果却很戏剧性,他的答案竟然是对的!!
    
    因为 36 * 495 = 396 * 45 = 17820
    
    类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54
    
    假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)
    
    能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?

    请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

    满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。

    答案直接通过浏览器提交。

    注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

    能满足形如:ab * cde = adb * ce这样的算式一共有(5分)种

    简单dfs。

    代码:

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int c;
    int ans[6],vis[10];
    int check() {
        int a = (ans[0] * 10 + ans[1]) * (ans[2] * 100 + ans[3] * 10 + ans[4]);
        int b = (ans[0] * 100 + ans[3] * 10 + ans[1]) * (ans[2] * 10 + ans[4]);
        if(a == b) return 1;
        return 0;
    }
    void dfs(int k) {
        if(k >= 5) {
            c += check();
            return;
        }
        for(int i = 1;i <= 9;i ++) {
            if(!vis[i]) {
                vis[i] = 1;
                ans[k] = i;
                dfs(k + 1);
                vis[i] = 0;
            }
        }
    }
    int main() {
        dfs(0);
        printf("%d",c);
    }
    public class Main {
        private static boolean [] vis = new boolean[10];
        private static int [] ans = new int[5];
        private static int c = 0;
        private static void dfs(int k) {
            if(k == 5) {
                if((ans[0] * 100 + ans[1] * 10 + ans[2]) * (ans[3] * 10 + ans[4]) == 
                        (ans[0] * 10 + ans[2]) * (ans[3] * 100 + ans[1] * 10 + ans[4])) c ++;
                return;
            }
            for(int i = 1;i <= 9;i ++) {
                if(!vis[i]) {
                    vis[i] = true;
                    ans[k] = i;
                    dfs(k + 1);
                    vis[i] = false;
                }
            }
        }
        public static void main(String[] args) {
            dfs(0);
            System.out.println(c);
        }
    
    }
  • 相关阅读:
    小米2/2S 手机由 Smartisan OS ROM 刷回 MIUI 教程
    Java构造和解析Json数据的两种方法详解二
    python-数据类型(上):数字、布尔值、字符串、字典
    python的介绍与安装
    PyCharm快捷键
    PyCharm安装模块
    PyCharm安装
    mac如何获取文件路径
    mac常用快捷键
    linux常用命令
  • 原文地址:https://www.cnblogs.com/8023spz/p/10297801.html
Copyright © 2020-2023  润新知