CS106B
Handout 08
Winter 2010
January 11
th
, 2010
Section Handout
We haven’t learned so much C++ yet that I can really push you on C++ constructs.
The
problems below are algorithmically nontrivial, but once you find an algorithm, the solution
is near trivial to implement.
Think of these as advanced CS106A problems where you’re
now required to write in C++ instead of Java.
Problem 1: Look And Say
The lookandsay sequence is the sequence of numbers beginning as:
1
11
21
1211
111221
312211
13112221
Each number in the sequence is generated by "reciting" its predecessor, and then capturing
what was said in a number format that should be clear from the example below.
Reciting 111221 out loud, you’d look and say: 3 ones, followed by 2 twos, followed by 1
one, or 312211.
Reading 312211 aloud, you’d say: 1 three, 1 one, 2 twos, 2 ones, or
13112221.
Even if a number doesn’t fall in the actual sequence, it should be clear how to generate its
lookandsay successor.
Write a function called
LookAndSay
, which takes a number in
string form, and returns its lookandsay form.
You can assume the incoming string
contains only digits, and that the longest run of any single digit will be less than or equal to
9.
/**
* Accepts the incoming string and generates the
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '09
 SelimAksoy
 Computer Science, Numerical digit, Natural number, returns, Numeral system, URL redirection

Click to edit the document details