zoukankan      html  css  js  c++  java
  • 马虎的算式|2013年蓝桥杯B组题解析第二题-fishers

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

    答案:142

    思路一:dfs全排列,筛选条件

    思路二:5层for循环,暴力枚举各个位置上的值,筛选条件

    #include<iostream>
    using namespace std;
    
    int visited[10];
    long ans = 0;
    
    
    void init(){
    	for(int i=0;i<=9;i++){
    		visited[i] = 0;
    	}
    }
    // 1 2 3 4 5
    bool test(int a[]){
    	if((a[1] * 10 + a[2]) * (a[3] * 100 + a[4] * 10 + a[5]) == (a[1] * 100 + a[4] * 10 + a[2]) * (a[3] * 10 + a[5])){
    		return true;
    	}	
    	return false;
    }
    
    void dfs(int k,int a[]){
    	
    	if(k == 6){
    		if(test(a)){
    			ans++;
    		}
    		return;
    	}
    	
    	for(int i=1;i<=9;i++){
    		if(!visited[i]){
    			a[k] = i;
    			visited[i] = 1;
    			dfs(k+1,a);
    			visited[i] = 0;
    			a[k] = 0;
    		}
    	}
    }
    
    int main(){
    	int a[10];
    	dfs(1,a);
    	cout<<ans<<endl;
    	//142
    } 
    

    代码二:

    #include <iostream>
    
    using namespace std;
    
    int main(int argc, const char *argv[]) {
        int ans=0;
        for (int a = 1; a < 10; ++a) {
            for (int b = 1; b < 10; ++b) {
                if (b != a)
                    for (int c = 1; c < 10; ++c) {
                        if (c != a && c != b)
                            for (int d = 1; d < 10; ++d) {
                                if (d != a && d != b && d != c)
                                    for (int e = 1; e < 10; ++e) {
                                        if (e != a && e != b && e != c && e != d) {
    //                                        ab * cde = adb * ce
                                            if ((a*10+b)*(c*100+d*10+e)==(a*100+d*10+b)*(c*10+e)) {
                                               printf ("(%d*10+%d)*(%d*100+%d*10+%d)==(%d*100+%d*10+%d)*(%d*10+%d)==%d
    ",a,b,c,d,e,a,d,b,c,e,(a*10+b)*(c*100+d*10+e));
                                                ans++;
                                            }
                                        }
    
                                    }
    
                            }
                    }
            }
        }
        printf("%d",ans);
        return 0;
    }
    
  • 相关阅读:
    #从零开始学Swift2.0# No.4 枚举, 元组, 数组和字典
    #从零开始学Swift2.0# No.3 基本数据类型
    #从零开始学Swift2.0# No.2 运算符和表达式
    #从零开始学Swift2.0# No.1 初识Swift
    MacOS下SVN的使用
    在Xcode中制作.a文件
    在Xcode中制作Framework
    Objective-C中的Runtime
    汉语字典或者词典的简单的ios小demo
    ios开发-UI进阶-核心动画-时钟动画小案例
  • 原文地址:https://www.cnblogs.com/fisherss/p/10326128.html
Copyright © 2011-2022 走看看