Last active
December 23, 2015 08:23
-
-
Save jjlumagbas/0273a993bad290ca50f5 to your computer and use it in GitHub Desktop.
My solution to CMSC11 Final last 4 questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| void display(int arr[], int length) { | |
| for (int i = 0; i < length; i++) { | |
| printf("%d ", arr[i]); | |
| } | |
| printf("\n"); | |
| } | |
| // 1 | |
| void rotate(int arr[], int length) { | |
| int last = arr[length - 1]; | |
| for (int i = length - 2; i >= 0; i--) { | |
| arr[i + 1] = arr[i]; | |
| } | |
| arr[0] = last; | |
| } | |
| // 2 | |
| int length(char str[]) { | |
| int i = 0; | |
| while (str[i] != '\0') { | |
| i++; | |
| } | |
| return i; | |
| } | |
| int sliceGoodness(char str[], int sliceLength, int startIndex) { | |
| int goodness = 0; | |
| for (int i = startIndex, count = 0; str[i] != '\0' && count < sliceLength; i++, count++) { | |
| if (str[i] == 'u') { | |
| return 0; | |
| } else if (str[i] == 'g') { | |
| goodness++; | |
| } | |
| } | |
| return goodness; | |
| } | |
| int highestGoodness(char str[], int n) { | |
| if (n == 0) { | |
| return -1; | |
| } | |
| int highest = 0; | |
| int highestIndex = 0; | |
| int strLength = length(str); | |
| for (int i = 0; i + n <= strLength; i++) { | |
| int goodness = sliceGoodness(str, n, i); | |
| if (goodness > highest) { | |
| highest = goodness; | |
| highestIndex = i; | |
| } | |
| } | |
| return highestIndex; | |
| } | |
| // 3 | |
| int convertToNumber(char num) { | |
| if ('0' <= num && num <= '9') { | |
| return num - '0'; | |
| } else if ('A' <= num && num <= 'C') { | |
| return 2; | |
| } else if ('D' <= num && num <= 'F') { | |
| return 3; | |
| } else if ('G' <= num && num <= 'I') { | |
| return 4; | |
| } else if ('J' <= num && num <= 'L') { | |
| return 5; | |
| } else if ('M' <= num && num <= 'O') { | |
| return 6; | |
| } else if ('P' <= num && num <= 'S') { | |
| return 7; | |
| } else if ('T' <= num && num <= 'V') { | |
| return 8; | |
| } else if ('W' <= num && num <= 'Z') { | |
| return 9; | |
| } | |
| return 0; | |
| } | |
| int numericPhone(char num[]) { | |
| int numeric = 0; | |
| for (int i = 0; num[i] != '\0'; i++) { | |
| numeric *= 10; | |
| numeric += convertToNumber(num[i]); | |
| } | |
| return numeric; | |
| } | |
| // 4 | |
| int isPrime(int n) { | |
| for (int i = 2; i <= n / 2; i++) { | |
| if (n % i == 0) { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int isHumble(int n) { | |
| for (int i = 2; i <= n; i++) { | |
| if (n % i == 0 && i != 2 && i != 3 && i != 5 && i != 7 && isPrime(i)) { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int main() { | |
| printf("%d = 1", isPrime(13)); | |
| for (int i = 0; i < 100; i++) { | |
| if (isHumble(i)) { | |
| printf("%d ", i); | |
| } | |
| } | |
| printf("\n"); | |
| printf("%d\n", numericPhone("243NERD")); | |
| printf("%d\n", length("abcde")); | |
| printf("%d = 0\n", highestGoodness("ggg", 3)); | |
| printf("%d = 1\n", highestGoodness("bggg", 3)); | |
| printf("%d = 2\n", highestGoodness("bbgggu", 3)); | |
| printf("%d = 0\n", highestGoodness("ggg", 3)); | |
| printf("%d = 3\n", sliceGoodness("ggg", 3, 0)); | |
| printf("%d = 0\n", sliceGoodness("bbb", 3, 0)); | |
| printf("%d = 0\n", sliceGoodness("gug", 3, 0)); | |
| printf("%d = 1\n", sliceGoodness("gbb", 3, 0)); | |
| printf("%d = 1\n", sliceGoodness("gbb", 2, 0)); | |
| printf("%d = 0\n", sliceGoodness("bbg", 2, 0)); | |
| printf("%d = 0\n", highestGoodness("ggg", 3)); | |
| int nums[5] = {1, 2, 3, 4, 5}; | |
| display(nums, 5); // 1 2 3 4 5 | |
| rotate(nums, 5); | |
| display(nums, 5); // 5 1 2 3 4 | |
| int num[1] = {1}; | |
| display(num, 1); // 1 2 3 4 5 | |
| rotate(num, 1); | |
| display(num, 1); // 5 1 2 3 4 | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A positive whole number n > 2 is prime if no number between 2 and n / 2 (inclusive) evenly divides n. A prime factor is a factor of a number that is a prime number.
A number whose only prime factors are 2, 3, 5, or 7 is called a humble number. The sequence 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27 shows the first 19 humble numbers. 39 is not humble, because while it has 3 as a prime factor, it also has 13 as another prime factor, which is neither 2, 3, 5, nor 7.