diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-05 23:22:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-05 23:22:32 +0100 |
| commit | e55078398f8407594980c8daa982da68d7f079e6 (patch) | |
| tree | 9f2b1bf3b7e5b1522ee4cd3148cc15597640490e /challenge-120/paulo-custodio/cpp | |
| parent | c4867933e1efae9349f7e90e2a545b12456e6f4c (diff) | |
| parent | 95db0bb6b8cd9762c26d8392cda04b3553b38bb4 (diff) | |
| download | perlweeklychallenge-club-e55078398f8407594980c8daa982da68d7f079e6.tar.gz perlweeklychallenge-club-e55078398f8407594980c8daa982da68d7f079e6.tar.bz2 perlweeklychallenge-club-e55078398f8407594980c8daa982da68d7f079e6.zip | |
Merge pull request #4441 from pauloscustodio/paulo-custodio
Add solutions to challenge 120
Diffstat (limited to 'challenge-120/paulo-custodio/cpp')
| -rw-r--r-- | challenge-120/paulo-custodio/cpp/ch-1.cpp | 46 | ||||
| -rw-r--r-- | challenge-120/paulo-custodio/cpp/ch-2.cpp | 57 |
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-120/paulo-custodio/cpp/ch-1.cpp b/challenge-120/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..7d25a239ec --- /dev/null +++ b/challenge-120/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,46 @@ +/* +Challenge 120 + +TASK #1 - Swap Odd/Even bits +Submitted by: Mohammad S Anwar +You are given a positive integer $N less than or equal to 255. + +Write a script to swap the odd positioned bit with even positioned bit and +print the decimal equivalent of the new binary representation. + +Example +Input: $N = 101 +Output: 154 + +Binary representation of the given number is 01 10 01 01. +The new binary representation after the odd/even swap is 10 01 10 10. +The decimal equivalent of 10011010 is 154. + +Input: $N = 18 +Output: 33 + +Binary representation of the given number is 00 01 00 10. +The new binary representation after the odd/even swap is 00 10 00 01. +The decimal equivalent of 100001 is 33. +*/ + +#include <iostream> +using namespace std; + +int swap_bits(int n) { + int out = 0; + int shift = 0; + while (n > 0) { + if ((n & 1) != 0) { out |= 2 << shift; } + if ((n & 2) != 0) { out |= 1 << shift; } + n >>= 2; + shift += 2; + } + return out; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + cout << swap_bits(n) << endl; +} diff --git a/challenge-120/paulo-custodio/cpp/ch-2.cpp b/challenge-120/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..c3325269ed --- /dev/null +++ b/challenge-120/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,57 @@ +/* +Challenge 120 + +TASK #2 › Clock Angle +Submitted by: Mohammad S Anwar +You are given time $T in the format hh:mm. + +Write a script to find the smaller angle formed by the hands of an analog +clock at a given time. + +HINT: A analog clock is divided up into 12 sectors. One sector represents 30 +degree (360/12 = 30). + +Example +Input: $T = '03:10' +Output: 35 degree + +The distance between the 2 and the 3 on the clock is 30 degree. +For the 10 minutes i.e. 1/6 of an hour that have passed. +The hour hand has also moved 1/6 of the distance between the 3 and the 4, +which adds 5 degree (1/6 of 30). +The total measure of the angle is 35 degree. + +Input: $T = '04:00' +Output: 120 degree +*/ + +#include <iostream> +#include <sstream> +#include <string> +using namespace std; + +bool parse_time(const string& hhmm, int* hh, int* mm) { + *hh = *mm = 0; + istringstream iss(hhmm); + char c; + if (!(iss >> *hh)) return false; + if (!(iss >> c) || c != ':') return false; + if (!(iss >> *mm)) return false; + return true; +} + +int clock_angle(int hh, int mm) { + int mm_angle = mm*360/60; + int hh_angle = (hh % 12)*360/12 + mm_angle/12; + int angle = abs(hh_angle - mm_angle); + if (angle > 180) { angle = 360 - angle; } + return angle; +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + int hh, mm; + if (!parse_time(argv[1], &hh, &mm)) return EXIT_FAILURE; + int angle = clock_angle(hh, mm); + cout << angle << endl; +} |
