#### Archived

This topic is now archived and is closed to further replies.

# complicated

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Sounds like homework. I had this exact assignment in my second CS class.

"First we are asked in input the first line:"

The plural 'we' kind of gives me the impression its homework for you too.

[edited by - haro on June 10, 2004 8:07:49 PM]

##### Share on other sites
perhaps not quite what you are looking for, but google around for basic "Genetic Algorithms". They do what you are describing, but in a "more interesting" way.

##### Share on other sites
lol its not
this is something i want to do in my own time
im actually just getting our of grade 9 this year, and my math teacher has put up these types of questions before.
the idea is for my learning of c++, and im integrating my obsession with riddles

##### Share on other sites
ok, i shall look for Algorithms, in the meatime does anyone have any ideas?? its mostly the coding i need, i dont know how to code a lot of that stuff

##### Share on other sites
int a, b, c, d, e, f; for(a = 0; a <= 9; a++){	for(b = 0; b <= 9; b++)	{		if(b == a)			continue; 		for(c = 0; c <= 9; c++)		{			if((c == a) || (c == b))				continue; 			for(d = 0; d <= 9; d++)			{				if((d == a) || (d == b) || (d == c))					continue; 				for(e = 0; e <= 9; e++)				{					if((e == a) || (e == b) || (e == c) || (e == d))						continue; 					for(f = 0; f <= 9; f++)					{						if((f == a) || (f == b) || (f == c) || (f == d) || (f == e))							continue; 						if((1000 * a + 100 * b + 10 * c + d) + (100 * b + 10 * d + c) == (1000 * a + 100 * e + 10 * a + f))						{							cout << "Solution is a, b, c, d, e, f = " << a << ", " << b << ", " << c << ", " << d << ", " << e << ", " << f << endl;						}					}				}			}		}	}}

##### Share on other sites
quote:
Original post by Aprosenf
..code dump..

Hmmm.. that code doesn''t really work at all, but more importantly is the fact that your idea is not really correct either. The idea is to make a general purpose solution as stated in the OP''s original post. This means that you should be able to take up to 10 digits of input in arbitrarily long blocks:

AABGHDCEFGHASSE
+
DCBAHIEFDCBA
+
AAAAAABBBBBCCCDDDDEEEFFFGGGHHHIII
=
ABCDEFGHIJABCDEFGJHEAFDCGHACCCCEE

Obviously you can see that brute forcing everything will leave you old and grey in some cases ( imagine that only with more and longer keys ). A decent implementation should be able to solve most of any reasonable input set in a matter of milliseconds. A bad implementation would take years for the same data sets.

Anyhow, there is a term for these sorts of problems. I cannot recall it at all.

##### Share on other sites
quote:
Original post by leiavoia
perhaps not quite what you are looking for, but google around for basic "Genetic Algorithms". They do what you are describing, but in a "more interesting" way.

Problems can have hundreds of solutions or even zero. Also it is really difficult to determine "how far off" an answer is. Even if a solution has all digits correct except for one, it is possible that every single value is actually incorrect. A genetic algorithm approach seems to probably be a bad idea here.

##### Share on other sites
hmmm, well, the idea with this was to return only the ones that matched exactly
so if theres a ton of solutions it returns all of them, and if none it returns none of them

##### Share on other sites
AFAIK, the most efficient way to solve this problem is using a Constraint Satisfaction algorithm. Example:

she
+ he
====
haho

Set up constraints:

1. e + e = o (or o + 10)
2. either h + h = h (or h + 10) or h + h + 1 = h (or h + 10)
3. s + 1 = ha

- For each value 0..9 of s:
- For each value 0..9 of h not violating constraints:
- For each value 0..9 of e not violating constraints:
- For each value 0..9 of o not violating constraints:
- test if equation holds, and if it does, return the bindings

There are heuristics (such as most-constrained-variable first) which govern the order in which you assign values, and these can make the answer appear much faster.

Good luck!

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009584
• ### Who's Online (See full list)

There are no registered users currently online

×