adding cp3

This commit is contained in:
Jidong Xiao
2023-11-05 01:16:09 -04:00
parent 1c30e6f4c8
commit 730c2d1d9d
2 changed files with 56 additions and 3 deletions

View File

@@ -24,9 +24,38 @@ Finally, implement and test the *resize* function. This function is automaticall
**To complete this checkpoint**: Show a TA these additions and the test output. **To complete this checkpoint**: Show a TA these additions and the test output.
## Checkpoint 3 ## Checkpoint 3: Using std::unordered_map
*estimate: remainder of lab time* *estimate: 20-30 minutes*
To be added. Complete the *isHappy* function in this [program](test_happy_number.cpp). This function determines if a number n is happy or not. You can assume 1<=n<50000. Do not include extra libraries.
A happy number is a number defined by the following process:
- Starting with any positive integer, replace the number by the sum of the squares of its digits.
- Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
- Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not. Here are some examples:
```console
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
```
```console
Example 2:
Input: n = 2
Output: false
```
**To complete this checkpoint**: Show a TA your program and the test results.

View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <unordered_map>
bool isHappy(int n) {
}
int main() {
// Test cases
// 2, 4, 5, 6, 17, 18, 20 are not happy numbers.
// 1, 7, 10, 13, 19, 23, 28, 68 are not happy numbers.
int testCases[] = {2,4,5,6,17,18,20,68,1,7,10,13,19,23,28};
for (int n : testCases) {
if (isHappy(n)) {
std::cout << n << " is a happy number." << std::endl;
} else {
std::cout << n << " is not a happy number." << std::endl;
}
}
return 0;
}