SEND, MORE, and MONEY

advertisement
SEND
+
MORE
--------MONEY
Background information:
Cryptarithmetic is a family of problems in which numbers are encoded (encrypted) as special symbols. For
example, we could represent the integer number, 2836 as ABCD in which the letter A represents digit 2, B represents
digit 8, and so on.
Problem description:
In this problem, we try to add two 4-digit numbers resulting to a 5-digit number as follows:
SEND +
MORE
________________
MONEY
Rules:
Rules of the problem are as follows:
The unique letters represent unique digits. For example, if the letter E represents digit 5, then E
consistently represents 5 in MORE and in MONEY; if the letter O represents digit 3, then O
consistently represents 3 in MONEY, and so on.
Obviously, and by quick inspection, no digit is duplicated in SEND, MORE, and the MONEY
individually. However, some digits are duplicated in of the above 3 numbers.
General algorithm:
-
Declare and initialize all variables.
FOR EACH value of SEND
o Digitize SEND.
o Check the digitized components of SEND for uniqueness.
o IF SEND digits are unique THEN
 FOR EACH value of MORE.
 Digitize MORE.
 Check the digitized components of MORE for uniqueness.
 IF MORE digits are unique THEN
o Check for consistency of digits between SEND and MORE.
o IF SEND and MORE digits are consistent THEN
 Set MONEY to result of SEND + MORE
 Digitize MONEY.
 Check for consistency of digits in MONEY.
 IF MONEY digits are consistent THEN
 Check for consistency of digits among SEND, MORE, and
MONEY.
 IF SEND, MORE, and MONEY are consistent THEN
o Display SEND, MORE, and MONEY.
Pseudocode:
-
Create MONEY (type integer).
Create the digitized variables for SEND, MORE, and MONEY
(I would call them S1, E1, N1, D1, M2, O2, R2, E2 …(type integer))
Create SEND_UNIQUE, MORE_UNIQUE, SEND_MORE_UNIQE (type int)
For (SEND between 1000 and 9999)
{
digitize SEND using division and modulus operators.
set SEND_UNIQUE flag to false initially.
if (S1 not equal to rest of the digits)
{
if(E1 not equal to rest of the digits)
{
if(N1 not equal to rest of the digits)
{
set SEND_UNIQUE to true
break out of IF.
}
}
}
if (SEND_UNIQUE is equal to true)
{
for (MORE between 1000 and 9999)
{
digitize MORE.
set MORE_UNIQUE to false initially.
If (M2 not equal to rest of the digits)
{
if(O2 not equal to rest of the digits)
{
if(R2 not equal to rest of the digits)
{
set MORE_UNIQUE to true
}
}
}
if(SEND_UNIQUE and MORE_UNIQUE)
{
set SEND_MORE_UNIQUE to true
if(SEND_MORE_UNIQUE equals to true)
{
add SEND and MORE; store in MONEY
digitize MONEY
check for uniqueness of digits as in SEND
and MORE.
Check for consistency of digits among
MONEY, SEND, and MORE.
If (true (meaning it is consistent))
print SEND, MORE, and
MONEY
}
}
}
}
Development tips:
Although the problem deals with two-level “for” loop construct and number of IF-THEN-ELSE constructs,
one may get easily confused by the order in which the rules need to be checked (miss the view of forest for trees!!).
Here are few tips to keep your sanity (and keep your taught-process organized) while programming:
1 – Declare/define all your variables up top (after the opening brace) and initialize them (like zeros).
2 – Use commenting to break your code into logical segments (given in general algorithm). This will
let you keep track of where “you are” in the program logic. For example:
//Digititzing SEND
int remainder = SEND;
S1 = remainder /1000;
remainder = remainder %1000;
:
:
3 – Use proper indentations (specially with “for” loops and IF-THEN-ELSE statements). The
indentations should signify the “groupings” of code that should get executed based on a given
condition, etc.
4- Develop incrementally! This means: DO not attempt to write the entire code to find out that there
are tons of errors! (usually is the case). Instead, write small portions (snippets) of code and test (using
“printf” statement) to ensure that your algorithm is behaving as it suppose to; then move on.
Enjoy!
Download