穷举法应用示例:解Google方程式

本文通过介绍《算法的乐趣》中关于穷举法的应用,详细解析了如何使用JAVA编程解决一个有趣的数字替换问题——Google方程式。该方程式要求找到一组数字,使得等式WWWDOT - GOOGLE = DOTCOM成立,同时保证所有数都是0-9间的合法数字,不以0开头。
摘要由CSDN通过智能技术生成

最近正在阅读《算法的乐趣》,该书讲解穷举法时举例Google方程式,题目:

 有一个字符组成的等式:WWWDOT - GOOGLE = DOTCOM,每个字符代表一个0-9之间的数字,WWWDOT、GOOGLE和DOTCOM都是合法的数字,不能以0开头。请找出一组字符和数字的对应关系,使它们互相替换,并且替换后的数字能够满足等式。

书中给出了关键的搜索函数和和整体思路,这里我把程序补充成完整的JAVA程序:

package Not.HelloWorld;


/**
 * Created by workstation on 2016/10/31.
 */
public class NotHelloWorld {
    static int MAX_CHAR_COUNT=0;
    static int MAX_NUM_COUNT=0;
    public static void main(String[] args) {
        TagCharItem[] ci=new TagCharItem[]{new TagCharItem('W',-1,true),new TagCharItem('D',-1,true),
                new TagCharItem('O',-1,true),new TagCharItem('T',-1,false),new TagCharItem('G',-1,false),
                new TagCharItem('L',-1,false),new TagCharItem('E',-1,false), new TagCharItem('C',-1,false),new TagCharItem('M',-1,false)};
        TagCharValue[] cv=new TagCharValue[]{
                new TagCharValue(false,0), new TagCharValue(false,1), new TagCharValue(false,2),
                new TagCharValue(false,3), new TagCharValue(false,4), new TagCharValue(false,5),
                new TagCharValue(false,6), new TagCharValue(false,7), new TagCharValue(false,8),new TagCharValue(false,9)
        };
        MAX_CHAR_COUNT = ci.length;
        MAX_NUM_COUNT=cv.length;
        SearchingResult(ci,cv,0);
    }

    public  static int MakeIntergerValue(TagCharItem[] ci,String string){
        int convertResult=0;
        for(int i =0;i<string.length();i++){
            char c = string.charAt(i);
            for(int j =0;j<MAX_CHAR_COUNT;j++){
                if(c == ci[j].c){
                    convertResult = convertResult*10+ci[j].value;
                }
            }
        }
        return convertResult;
    }

    public static void callback(TagCharItem[] ci){
        String minuend = "WWWDOT";
        String subtrahend = "GOOGLE";
        String diff = "DOTCOM";

        int m = MakeIntergerValue(ci,minuend);
        int s = MakeIntergerValue(ci,subtrahend);
        int d = MakeIntergerValue(ci,diff);

        if((m-s)==d){
            System.out.println(m+"-"+s+"="+d);
        }

    }

    public static boolean IsValueValid(TagCharItem charItem,TagCharValue charValue){
        boolean flag = false;
        if(!charValue.used){
            flag = (!charItem.leading)||(charValue.value!=0);
        }
        return flag;
    }

    public static void SearchingResult(TagCharItem[] ci,TagCharValue[] cv,int index){
        if (index == MAX_CHAR_COUNT){
            callback(ci);
            return;
        }

        for (int i =0 ;i<MAX_NUM_COUNT;++i){
            if (IsValueValid(ci[index],cv[i])){
                cv[i].used = true;
                ci[index].value=cv[i].value;
                SearchingResult(ci,cv,index+1);
                cv[i].used= false;
            }
        }
    }
}
class TagCharItem{
    char c;
    int value;
    boolean leading;

    TagCharItem(char c,int value,boolean leading){
        this.c=c;
        this.value=value;
        this.leading=leading;
    }
}

class TagCharValue{
    boolean used;
    int value;
    TagCharValue(boolean used,int value){
        this.used=used;
        this.value=value;
    }
}



程序具有通用性,只用将TagCharItem[] ci和TagCharValue[] cv进行修改就可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值