#include #include #include // rand and srand for ex. 5 using namespace std; /* * Exercise 1 */ double f(unsigned int k, int x) { double sum = x; for (unsigned int i = 2; i <= k; i++) { if (i & 1) // if odd sum = 3 * sum + 1; else sum /= 2; } return sum; } void ex1() { int x; cin >> x; cout << f(35, x) << endl; } /* * Exercise 2 */ void setMarmaladeType(int type, int *blueberryMarmalades, int *peachMarmalades, int *rosehipMarmalades) { if (type % 7 == 0) *blueberryMarmalades += 1; else if (type % 13 == 0) *peachMarmalades += 1; else if (type % 17 == 0) *rosehipMarmalades += 1; } void ex2() { int archiveLength; cin >> archiveLength; int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0; for (int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++) { cin >> digit; cout << digit; if (i % 8 == 0) { countryDigits = 1; typeDigits = type = 0; } else if (countryDigits < 3) { countryDigits++; } else if (typeDigits < 4) { typeDigits++; (type *= 10) += digit; } else if (typeDigits == 4) { cout << endl; setMarmaladeType(type, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades); } } cout << "Мармалад от боровинки: " << blueberryMarmalades << endl; cout << "Мармалад от праскови: " << peachMarmalades << endl; cout << "Шипков мармалад: " << rosehipMarmalades << endl; } /* * Exercise 4 */ bool isInteresting(int n) { int lastDigit; do { lastDigit = n % 10; n /= 10; if ((lastDigit & 1) == ((n % 10) & 1)) return false; } while (n != 0); 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 ex4() { int a, b; cin >> a >> b; cout << sumOfInterestingNumsBetween(a, b) << endl; } /* * Exercise 5 */ 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; for (int i = 0; i < NUMBER_LENGTH;) { if (rnd % 10 != 0) { (result *= 10) += rnd % 10; i++; } else if (rnd == 0) { rnd = rand(); } rnd /= 10; } return result; } bool digitInsideNum(int digit, int num) { while (num != 0) { if (digit == num % 10) return true; num /= 10; } return false; } 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; /* Find bulls and separate the numbers that dont make a bull */ for (int i = 0; i < NUMBER_LENGTH; i++) { if (guess % 10 == secretNum % 10) *bulls += 1; else { (secretCows *= 10) += secretNum % 10; // Getting unique cows prevents the later cow loop from incrementing cows // multiple times when we have duplicate digits in secretNum and in guessCows if (!digitInsideNum(guess % 10, 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; cout << secNum << endl; for (int i = 0; i < N; i++) { cin >> guess; if (guess == secNum) { cout << "Позна!" << endl; return; } else { countBullsAndCows(secNum, guess, &bulls, &cows); cout << bulls << " бикове и " << cows << " крави" << endl; } } cout << "Не позна в определения брой ходове!" << endl; } void ex5() { playBullsAndCows(); } int main() { ex5(); }