如题:有一个有字符组成的等式:WWWDOT-GOOGLE = DOTCOM,每个字符代表一个0-9之间的数字,WWWDOT、GOOGLE和DOTCOM都是合法的数字,不能以0开头,请找出一组字符和数字的对应关系,使得它们互相转换,并且替换后的数字都能满足等式。
总共可能性:10*9*8*······*2(不考虑0是开头数字的情况)-3*9*8*····*2(所有0是开头数字的情况,W、G、D可能为0)=2540160;
数据模型
1、建立字符数据模型,由字符,代表数值,以及是否为高位组成;
public class TagCharItem
{
public char C;
public int Value;
public bool Leading;
}
2、建立数值数据模型,有是否被占用,数字组成;
public class TagCharValue
{
public bool Used;
public int Value;
}
3、核心算法:使用递归的方式进行组合枚举;
public void SearchingResult(TagCharItem[] tagCharItems, TagCharValue[] tagCharValues, int index)
{
if (index == tagCharItems.Length)
{
IsOK(tagCharItems);
return;
}
for (int i = 0; i < tagCharValues.Length; i++)
{
if (IsValueValid(tagCharItems[index], tagCharValues[i]))
{
tagCharValues[i].Used = true;
tagCharItems[index].Value = tagCharValues[i].Value;
SearchingResult(tagCharItems, tagCharValues, index + 1);
tagCharValues[i].Used = false;
}
}
}
4、所有代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunOfAlgorithms.Chapter03
{
public class GoogleX
{
public void Main()
{
TagCharItem[] tagCharItem =
{
new TagCharItem() { C = 'W', Value = -1, Leading = true },//
new TagCharItem() { C = 'D', Value = -1, Leading = true },//
new TagCharItem() { C = 'O', Value = -1, Leading = false },
new TagCharItem() { C = 'T', Value = -1, Leading = false },
new TagCharItem() { C = 'G', Value = -1, Leading = true },//
new TagCharItem() { C = 'L', Value = -1, Leading = false },
new TagCharItem() { C = 'E', Value = -1, Leading = false },
new TagCharItem() { C = 'C', Value = -1, Leading = false },
new TagCharItem() { C = 'M', Value = -1, Leading = false }
};
TagCharValue[] tagCharValues =
{
new TagCharValue(){ Used = false, Value = 0},
new TagCharValue(){ Used = false, Value = 1},
new TagCharValue(){ Used = false, Value = 2},
new TagCharValue(){ Used = false, Value = 3},
new TagCharValue(){ Used = false, Value = 4},
new TagCharValue(){ Used = false, Value = 5},
new TagCharValue(){ Used = false, Value = 6},
new TagCharValue(){ Used = false, Value = 7},
new TagCharValue(){ Used = false, Value = 8},
new TagCharValue(){ Used = false, Value = 9},
};
SearchingResult(tagCharItem, tagCharValues, 0);
}
/// <summary>
/// 使用递归方式进行组合枚举
/// </summary>
/// <param name="tagCharItems"></param>
/// <param name="tagCharValues"></param>
/// <param name="index"></param>
public void SearchingResult(TagCharItem[] tagCharItems, TagCharValue[] tagCharValues, int index)
{
if (index == tagCharItems.Length)
{
IsOK(tagCharItems);
return;
}
for (int i = 0; i < tagCharValues.Length; i++)
{
if (IsValueValid(tagCharItems[index], tagCharValues[i]))
{
tagCharValues[i].Used = true;
tagCharItems[index].Value = tagCharValues[i].Value;
SearchingResult(tagCharItems, tagCharValues, index + 1);
tagCharValues[i].Used = false;
}
}
}
/// <summary>
/// 判定数据是否有效,是否可以被赋值
/// </summary>
/// <param name="tagCharItem"></param>
/// <param name="tagCharValue"></param>
/// <returns></returns>
public bool IsValueValid(TagCharItem tagCharItem, TagCharValue tagCharValue)
{
if (tagCharItem.Leading)
{
if (!tagCharValue.Used)
{
if (tagCharValue.Value == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
else
{
if (!tagCharValue.Used)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判定现有数据是否符合题目要求
/// </summary>
/// <param name="tagCharItems"></param>
public void IsOK(TagCharItem[] tagCharItems)
{
string WWWDOT = "WWWDOT";
ReplacaMethods(ref WWWDOT, tagCharItems);
int _WWWDOT = Convert.ToInt32(WWWDOT);
string GOOGLE = "GOOGLE";
ReplacaMethods(ref GOOGLE, tagCharItems);
int _GOOGLE = Convert.ToInt32(GOOGLE);
string DOTCOM = "DOTCOM";
ReplacaMethods(ref DOTCOM, tagCharItems);
int _DOTCOM = Convert.ToInt32(DOTCOM);
if (_WWWDOT - _GOOGLE == _DOTCOM)
{
Console.WriteLine($"WWWDOT = {WWWDOT},GOOGLE = {GOOGLE},DOTCOM = {DOTCOM}");
}
}
public void ReplacaMethods(ref string Example, TagCharItem[] tagCharItems)
{
for (int i = 0; i < tagCharItems.Length; i++)
{
Example = Example.Replace(tagCharItems[i].C.ToString(), tagCharItems[i].Value.ToString());
}
}
}
public class TagCharItem
{
public char C;
public int Value;
public bool Leading;
}
public class TagCharValue
{
public bool Used;
public int Value;
}
算法结果:
WWWDOT = 777589,GOOGLE = 188103,DOTCOM = 589486
WWWDOT = 777589,GOOGLE = 188106,DOTCOM = 589483
总结:该核心算法时一种解决字符方程问题的通用算法,遇见不同问题时,组合不同传入数据,修改数据判定方法、数据有效性方法即可;