CS 60 Slides - PowerPoint PPT Presentation

About This Presentation
Title:

CS 60 Slides

Description:

converts C strings to doubles. atoi('100.0') == 100.0. case-insensitive C string comparison ... arbitrary conversion from a string in bases (2-36) to a long int ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: Kel7163
Learn more at: https://www.cs.hmc.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 60 Slides


1
ACM notes
  • Can work on the problems anytime throughout the
    term
  • Contest conflict -- The GRE subject test is
    Nov. 10th!

Digroot problem
int x while (cin gtgt x) cout ltlt ( (x9) ?
(x9) 9 )
2
ACM style
while (nlines-- gt 0) queueltchargt q
string s getline(cin, s) for
(int i 0 si i) if (!q.empty()
si q.front()) // found
q.pop() else
q.push(si) if (q.size() gt 10)
cout ltlt "Not consistent with the theory\n"
goto done if
(q.empty()) cout ltlt "An echo string with buffer
size ten\n" else cout ltlt "Not an echo
string, but still consistent with the theory\n"
done
Echo problem
3
Storing results in tables
www.cs.hmc.edu/ACM/
dynamic programming
Find the number of prime factors in N! (1 is not
prime.)
...
2
3
4
5
6
7
8
...
4
Other problems
  • Change counting

input
output
1.00 0.06 0
There are 292 ways to make 1.00 There are 2 ways
to make 0.06
  • Sigma series

What?
input
output
3 4 87 99 -1
1 2 3 1 2 4 1 2 4 8 16 24 28 29 58 87 1 2 4 8 16
32 33 66 99
5
Other problems
  • Change counting

input
output
1.00 0.06 0
There are 292 ways to make 1.00 There are 2 ways
to make 0.06
  • Sigma series

Shortest sequences from 1 to N such that each
element is the sum of two previous elements.
input
output
3 4 87 99 -1
1 2 3 1 2 4 1 2 4 8 16 24 28 29 58 87 1 2 4 8 16
32 33 66 99
6
C STL
www.dinkumware.com/htm_cpl/index.html
www.sgi.com/tech/stl/
vectorltintgt v // basically an int
array v.reserve(10) // assure 10
spots v.push_back(42) // adds 42 to the
end v.back() // returns
42 v.pop_back() // removes 42 v.size()
// of elements vi //
ith element sort( v.begin(), v.end() ) //
default sort sort( v.begin(), v.end(), mycompare
) dequeltintgt d // double-ended
queue d.push_front(42) // add to
front d.front(42) // return front
element d.push_front(42) // remove from front
vector
include ltvectorgt
sort
include ltalgorithmgt
last time
deque
include ltdequegt
7
Useful C functions
int atoi(char s) double atof(char s) int
strcasecomp(char s1, char s2) long
strtol(char s, NULL, int base)
converts C strings to ints
atoi(100) 100
converts C strings to doubles
atoi(100.0) 100.0
case-insensitive C string comparison
strcasecmp(aCm,ACm) 0
arbitrary conversion from a string in bases
(2-36) to a long int
strtol(Charlie, NULL, 36) 2147483647L
use man for more...
8
sprintf
int sprintf(char str, char format, ...)
prints anything to the string str
char str100 sprintf(str,d,42) // str
is 42 sprintf(str,f,42.0) // str is
42.0 sprintf(str,10d,42) // str is
42 sprintf(str,-10d,42) // str is
42
flexible formatting
right/left justify
9
Two ACM programming skills
A chance to improve your C/C
Preparation for the ACM competition ...
Problem Insight and Execution ...
2
1
Anxiety!
Get into the minds of the judges
10
Key Skill 1 mindreading
100
0
What cases should I handle? spectrum
11
Key Skill 2 anxiety
Anxiety!
12
Dynamic Programming
Strategy create a table of partial results
build on it.
divis.cc
T(n) number of steps yet to go
T(n) T(3n1) 1 if n odd
T(n) T(n/2) 1 if n even
13
Dynamic Programming
Keys create a table of partial results,
articulate what each table cell means, then
build it up...
divis.cc
j items considered so far
Table T
0
1
2
3
4
5
6
1 1 6 2 -3
0
the list
1
4
i possible remainder
the divisor
2
3
Tij is 1 if i is a possible remainder using
the first j items in the list.
14
Dynamic programs can be short
include ltcstdiogt include ltiostreamgt include
ltvectorgt vectorltintgt v(10000) vectorltboolgt
m(100) // old mods vectorltboolgt m2(100) //
new mods int n, k bool divisible()
fill(m.begin(),m.end(),false) m0 true
for (int i0 iltn i) / not
giving away all of the code / / here
the table is built (6 lines) /
return m0
int main() cin gtgt n // garbage
while (cin gtgt n) cin gtgt k for
(int i0 iltn i) cin gtgt vi
vi abs(vi) vi
k cout ltlt (divisible() ? "D"
"Not d") ltlt "ivisible\n"
cout ltlt endl
acknowledgment Matt Brubeck
STL http//www.sgi.com/Technology/STL
15
General ACM Programming
Try brute force first (or at least consider
it) -- sometimes it will work fine --
sometimes it will take a _bit_ too long --
sometimes it will take _way_ too long
Best bugs from last week
filling in the table in the divis problem
getting the input in the pea problem
for (int j1 jltN j) cin gtgt Arrayi
Tablei n k 1 Tablei - n k 1
16
New Problem
Word Chains
doze aplomb ceded dozen envy ballistic yearn
hertz jazz hajj zeroth
Input A list of words
Output yes or no -- can these words be chained
together such that the last letter of one is
the first letter of the next ?
17
Knapsack Problem
object wt. val. 1 3 8
2 2 5 3 1 1 4
2 5
Maximize loot w/ weight limit of 4.
w
Weight available for use
Number of objects considered
0
1
2
3
4
1
V(n,w) max value stealable w/ n objects
w weight
2
3
4
n
V(n,w)
18
C Output
printf, fprintf, sprintf(char s, const char
format, )
the destination
the format string
the values
possible format strings
h
.4
12
-0

d
d decimal integers u unsigned (decimal)
ints o octal integers x hexadecimal
integers f doubles (floats are cast) e
doubles (exp. notation) g f or e, if exp lt -3
or -4 c character s string n outputs of
chars written !! two of these print a
type
size modifier
minimum field width
precision
allowed size modifiers
flags
- left-justify 0 pad w/ zeros
use sign ( or -) (space) use sign ( or -)
deviant operation
h short l long (lowercase L) L long double
start character
19
C Output
value 42
value -42
10.4d
0042
-0042
-12x
0x2a
0xffffffd6
value 42
value -42.419
10.4g
42
-42.42
- 10.4g
42
-42.42
-10.4g
42.00
-42.42
value forty-two
10.5s
forty
Write a Comment
User Comments (0)
About PowerShow.com