// // ++===================++ // || Оценка: 5.45/6.00 || // ++===================++ // // +-----------+ // | !Бележки! | // +-----------+ // Трета ти е грешна, не проверяваш правилно (тествай с други числа) -0.50 // И на първа грешно проверяваш сивото, пробвай с 0 и - 6.5 примерно -0.05 // #include // cin, cout, endl #include // fabs, sqrt #include // rand, srand using namespace std; /* * Exercise 0 */ /* Floating point arithmetic */ double const FLOAT_EPSILON = 0.001; bool eq(double a, double b) { return fabs(a - b) < FLOAT_EPSILON; } bool lt(double a, double b) { return !eq(a, b) && (a - b) < FLOAT_EPSILON; } bool mt(double a, double b) { return lt(b, a); } /* Planimetry */ double distanceBetweenPoints(double x1, double y1, double x2, double y2) { double sideA = x1 - x2, sideB = y1 - y2; return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem } void printPointLocation(double x, double y) { // The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828, // because BC and DE make 45 degrees angle with the abscissa float const CROSS_PNT = 2.828, SLOPE_BC = CROSS_PNT / CROSS_PNT, SLOPE_DE = -CROSS_PNT / CROSS_PNT; char const NOWHERE[] = "Nowhere!"; if (eq(x, 0)) cout << NOWHERE << endl; // On a line else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0)) { double pointSlope = y / x; // Added brackets in some places for improved readability if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT) || eq(pointSlope, SLOPE_BC) || (eq(pointSlope, SLOPE_DE) && mt(y, 0))) { cout << NOWHERE << endl; // On a line } else if (mt(pointSlope, SLOPE_BC)) { cout << "Inside " << ((x > 0) ? "green" : "blue" ) << " area!" << endl; } else if (lt(pointSlope, SLOPE_DE) && x < 0) { cout << "Inside red area!" << endl; } else if (mt(x, CROSS_PNT)) { cout << "Inside purple area!" << endl; } else if (mt(x, -CROSS_PNT) && x < 0) { cout << "Inside pink area!" << endl; } else if (x > 0) cout << "Inside yellow area!" << endl; else cout << NOWHERE << endl; } else if (lt(-2, x) && lt(x, 2) && mt(-6, y) && mt(y, -7)) cout << "Inside grey area!" << endl; else cout << NOWHERE << endl; } /* Exercise calculations */ void ex0() { double x, y; cin >> x >> y; printPointLocation(x, y); } /* * Exercise 1 */ double f(unsigned int k, int x) { unsigned int sum = x; for (unsigned int i = 2; i <= k; i++) { if (sum & 1) sum = 3 * sum + 1; else sum /= 2; } return sum; } void ex1() { int x; cin >> x; cout << f(35, x) << endl; } /* * Exercise 2 */ void setMarmaladeType(unsigned int type, unsigned int* const blueberryMarmalades, unsigned int* const peachMarmalades, unsigned int* const rosehipMarmalades) { if (type % 7 == 0) *blueberryMarmalades += 1; else if (type % 13 == 0) *peachMarmalades += 1; else if (type % 17 == 0) *rosehipMarmalades += 1; } void readCodesAndCountMarmalades(unsigned int archiveLength, unsigned int* const blueberryMarmalades, unsigned int* const peachMarmalades, unsigned int* const rosehipMarmalades) { archiveLength -= archiveLength % 8; // The last code could be invalid (shorter than 8 digits) for (unsigned int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++) { cin >> digit; if (i % 8 == 0) // Reset at start of every code { countryDigits = 1; typeDigits = type = 0; } else if (countryDigits < 3) { countryDigits++; } else if (typeDigits < 4) { typeDigits++; type = type * 10 + digit; } else if (typeDigits == 4) { setMarmaladeType(type, blueberryMarmalades, peachMarmalades, rosehipMarmalades); } } } void ex2() { unsigned int archiveLength; cin >> archiveLength; unsigned int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0; readCodesAndCountMarmalades(archiveLength, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades); cout << "Blueberry marmalades: " << blueberryMarmalades << endl; cout << "Peach marmalades: " << peachMarmalades << endl; cout << "Rosehip marmalades: " << rosehipMarmalades << endl; } /* * Exercise 3 */ bool isInteresting(int n) { while (n > 9) { if ((n & 1) == !!(n & 2)) return false; n /= 10; } return true; } int sumOfInterestingNumsBetween(int a, int b) { int sum = 0; for (int i = a; i <= b; i++) { if (isInteresting(i)) sum += i; } return sum; } void ex3() { int a, b; cin >> a >> b; cout << sumOfInterestingNumsBetween(a, b) << endl; } /* * Exercise 4 */ const int NUMBER_LENGTH = 4, N = 10; int genSecretNum() { // Initializes random seed (required for getting a random number on every program launch) // Taken from included reference: https://www.cplusplus.com/reference/cstdlib/rand#example srand(time(NULL)); int rnd = rand(), result = 0; bool takenDigits[10] = { 0 }; takenDigits[0] = true; // we can't have 0s in secret for (int i = 0; i < NUMBER_LENGTH; i++) { while (takenDigits[rnd % 10]) { rnd /= 10; if (rnd == 0) rnd = rand(); } result = result * 10 + rnd % 10; takenDigits[rnd % 10] = true; } return result; } bool digitInsideNum(int digit, int num) { while (num != 0) { if (digit == num % 10) return true; num /= 10; } return false; } bool isValidGuess(int guess) { if (guess < 0) return false; bool recordedDigits[10] = { 0 }; recordedDigits[0] = true; // We don't have zeroes in secret number for (int i = 0; i < NUMBER_LENGTH; i++) { if (guess == 0 || recordedDigits[guess % 10]) return false; recordedDigits[guess % 10] = true; guess /= 10; } return guess == 0; // False if wider than NUMBER_LENGTH digits } void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows) { *bulls = *cows = 0; // If our secret num can contain zeroes, this (and the corresponding checks) should be changed to some other digit int secretCows = 0, guessCows = 0; for (int i = 0; i < NUMBER_LENGTH; i++) { if (guess % 10 == secretNum % 10) *bulls += 1; else { secretCows = secretCows * 10 + secretNum % 10; // If the guess (and secret) can have duplicate digits, this should be done only with the unique ones guessCows = guessCows * 10 + guess % 10; } secretNum /= 10; guess /= 10; } while (secretCows != 0) { if (digitInsideNum(secretCows % 10, guessCows)) *cows += 1; secretCows /= 10; } } void playBullsAndCows() { int secNum = genSecretNum(), guess = 0, bulls = 0, cows = 0; for (int i = 0; i < N; i++) { cin >> guess; if (!isValidGuess(guess)) { cout << "Your guess is invalid!" << endl; } else if (guess == secNum) { cout << "You win!" << endl; return; } else { countBullsAndCows(secNum, guess, &bulls, &cows); cout << bulls << " bulls and " << cows << " cows" << endl; } } cout << "You lose! The number was " << secNum << " and you couldn't guess it in " << N << " guesses!" << endl; } void ex4() { playBullsAndCows(); } int main() { ex4(); }