Week 1 - C
Welcome to CS50! This is Week 1 and I will be going to talk about my summary of Week 1's content.
Problem Set 1
This problem serves as the beginning of this course. Welcome to CS50!
Things to notice in the problem statement:
"Re-prompt the user, again and again as needed, if their input is not greater than 0 or not an int altogether."
Divide and Conquer
Useful Snippets
Prompt the user until the input is valid.
Make the pyramid right-aligned
Take-aways
Use
do-while loop
to prompt until the input is valid.Divide the problem into smaller parts by finding the patterns, just keep in mind if something is duplicate in your program, then it's likely to have a more compact way to do that.
Using the same settings from the problem above, now the bricks pattern we want to print is different. But, we can still using the design structure and the only thing we need to change is the print_row()
function.
Useful Snippets
Change the
print_row
function to meet our requirements
Take-aways:
In this hard version of Mario, we can clearly see the importance of finding a good "structure" to solve a problem. Since this good structure can be reused, which will make our code flexible. And one way to make your code flexible is to write good functions.
Before the problem
There are only four types of cents:
1(pennies)
,5(nickles)
,10(dimes)
,25(quarters)
Introduction to Greedy Algorithm A greedy algorithm is one “that always takes the best immediate, or local, solution while finding an answer. Due to this characteristic, greedy algorithm can find the overall optimal solution of an optimization problem, but sometimes it may find less-than-optimal solutions.
Things to notice in the problem statement
Reprompt the user until the input is valid.
Divide and Conquer
Use Greedy algorithm to understand the problem. Given an initial owing (a certain number), we always try the first biggest cent until the remaining owing is smaller than this biggest-first cent. Then we move on to the secondest cent until the owing is 0. (Since our smallest cent is
1
, which means we can always find a solution to this problem)Design our structure
Useful snippets
Implement the calculation using a specific function
Take-aways
The idea of greedy algorithm and its application.
If we use greedy algorith, we must update our parameter for the next greedy use.
Before the problem
Luhn's Algorithm: This algorithm is used to check whether a credit card is valid or not by using a checksum. The procedure of this algorithm is below:
Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid! Notice that sometimes hackers will take use of this property, so we still need to look through the database to make sure whether the credit card number is valid or not.
Things to notice in the problem statement
The input should be
long
Divide and Conquer
Useful Snippets
One
while
loop version
Calculate the first and second digit: We have two methods to implement this, which are shown as follows:
Method 1
In this method, we will use the length of the number.
Method 2
In this method, we divide the number until the num is smaller than 100. And the remaining number is composed by the first and second digit.
Take-aways
num % 10
will get the last digit of a number.num / 10
will discard the last digit if num is an integer(includingint
andlong
).
Last updated