#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int factorialCalc(int n);
// returns factorial value
double powerCalc(int base, int pow);
// returns power value
int digitMatch(int first, int second, int third);
// The function has three integer parameters first , second and
// third that are positive and have the same number of digits.
// It returns the number of positions where their digits
// match.
int digitsOpposite(int x, int y);
// It returns the number of positions where one number has an even
// digit and the other has an odd one.
bool startsWith(int x, int y) ;
// It determines whether the second number starts with the
// first number.
int digitDifferences(int x, int y);
// It prints the number formed from digits obtained as (positive)
// differences between corresponding digits in the parameters
int lucky7(int x);
// It calculatesan answer by turning the first 7 (from the left)
// in the parameter to 77.
int interlaceDigits(int x, int y);
// returns an integer that begins with the first digit of the
// first parameter, then the first digit
// of the second parameter, then the second digits of
// the parameters, and so on until all digits are used.
int gce(int x, int y) ;
// returns the greatest common ending to the two number
int biggerDigits(int a, int b) ;
// returns an integer whose digit in each position is the bigger of
// the two digits in that position in the input parameters.
int doubleDigit(int n) ;
//makes each digit of an input parameter repeat twice.
int main() {
srand(time(NULL));
cout<<factorialCalc(6)<<endl;
cout <<powerCalc(3,4)<<endl;
cout << digitMatch(168, 567, 767) << " matches !" << endl;
cout << digitMatch(143, 243, 343) << " matches !"<< endl;
cout << startsWith(7, 747) << endl; // prints true
cout << startsWith(74, 74) << endl; // prints true
cout << startsWith(747, 74) << endl; // prints false
cout << digitDifferences(162, 538) << endl;
cout << lucky7(747) << endl; // prints 7747
cout << lucky7(7) << endl; // prints 77
cout << lucky7(1234) << endl; // prints 1234
cout << lucky7(172737) << endl; // prints 1772737
cout << interlaceDigits(1, 2) << endl; // prints 12
cout << interlaceDigits(117, 302) << endl; // prints 131072
cout << interlaceDigits(1357, 2468) << endl; // prints 123456780
cout << gce(123, 123) << endl; // prints 123
cout << gce(123, 223) << endl; // prints 23
cout << gce(117, 119) << endl; // prints 0
cout << gce(1357, 13657) << endl; // prints 57
cout << biggerDigits(567, 765) << endl; // prints 767
cout << biggerDigits(123456, 444444) << endl; // prints 444456
cout << biggerDigits(999, 111) << endl; // prints 999
cout << doubleDigit(9) << endl; // prints 99
cout << doubleDigit(81) << endl; // prints 8811
cout << doubleDigit(243) << endl; // prints 224433
cout << doubleDigit(244) << endl; // prints 224444
}
int factorialCalc(int i){
if (i==1) return 1;
if (1<i)
{
i = i * (factorialCalc(i-1));
}
return i;
}
double powerCalc(int base, int pow){
if (pow>1)
{
base = base * powerCalc(base, pow-1);
}
return base;
}
int digitMatch(int first, int second, int third) {
if (first <= 0)
return 0;
if (first % 10 == second % 10 && first % 10 == third % 10)
return 1 + digitMatch(first / 10, second / 10, third / 10);
return digitMatch(first / 10, second / 10, third / 10);
}
int digitsOpposite(int x, int y)
{
if (x <= 0) return 0;
if (x % 2 == y % 2) return digitsOpposite(x/ 10, y / 10);
return digitsOpposite(x/ 10, y / 10) + 1;
}
bool startsWith(int x, int y)
{
if (x == y)
return true;
if (x > y)
return false;
return startsWith(x, y/10);
}
int digitDifferences(int x, int y)
{
if (x == 0) return 0;
int diff = x % 10 - y % 10;
if (diff < 0) diff = -diff;
return 10 * digitDifferences(x/10,y/10) + diff;
}
int lucky7(int x) {
if (x <= 0) return 0;
int ans = lucky7(x / 10);
if ((ans == x / 10) && (x % 10 == 7)) return 100 * ans + 77;
return 10 * ans + x % 10;
}
int interlaceDigits(int x, int y) {
if (x == 0) return 0;
return 100 * interlaceDigits(x / 10, y / 10) + 10 * (x % 10) + y % 10;
}
int gce(int x, int y) {
if (x == 0) return 0;
if (x % 10 != y % 10) return 0;
return 10 * gce(x / 10, y / 10) + x % 10;
}
int biggerDigits(int a, int b) {
if (a == 0 && b == 0) return 0;
if (a % 10 > b % 10) return 10 * biggerDigits(a/10, b/10) + a % 10;
else return 10 * biggerDigits(a/10, b/10) + b % 10;
}
int doubleDigit(int n) {
if (n < 10) return n * 11;
return 100 * doubleDigit(n / 10) + doubleDigit(n % 10);
}
0 comments:
Post a Comment