19S-Section-Solution

19S-Section-Solution - CS106X Autumn 2010 Handout 19S...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 19S Autumn 2010 October 13 th – 15 th , 2010 Section Solution Solution 1: Generating Expressions void addToPossibilities(string digitOrder, int start, int end, Set<int>& possibilities) { if (start == end) { possibilities.add(digitOrder[start] - '0'); return; } for (int mid = start; mid < end; mid++) { Set<int> left, right; addToPossibilities(digitOrder, start, mid, left); addToPossibilities(digitOrder, mid + 1, end, right); foreach (int l in left) { foreach (int r in right) { possibilities.add(l + r); possibilities.add(l - r); possibilities.add(l * r); if (r != 0) possibilities.add(l / r); } } } } void addToPossibilities(string digitOrder, Set<int>& possibilities) { addToPossibilities(digitOrder, 0, digitOrder.size() - 1, possibilities); } void computeAllPossibilities(string fixedPrefix, string remaining, Set<int>& possibilities) { if (remaining.empty()) { // fixedPrefix is a full ordering addToPossibilities(fixedPrefix, possibilities); return; } for (int i = 0; i < remaining.size(); i++) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

19S-Section-Solution - CS106X Autumn 2010 Handout 19S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online