Write a function named *foo* that takes two integer arguments and returns 1
if the arguments satisfy the following two conditions. Otherwise it returns
0.
1. the arguments contain the same number of digits.
2. a comparison of the digits in the arguments from right to left will
find only one digit that is different.
For example foo(2134, 2154) returns 1 because
1. both arguments are four digits long.
2. they agree on all digits except the third one.
Other examples:
- foo(-2134, 2154) returns 1 (minus sign is ignored).
- foo(2, 1) returns 1 (they agree on zero digits and differ in one).
- foo(213, 2134) returns 0 because condition a is not satisfied.
- foo(2134, 2134) returns 0 because condition b is not satisfied. (no
digits differ).
- foo(3234, 2134) returns 0 because condition b is not satisfied. (two
digits differ).
- foo(1234, 4322) returns 0 because condition b is not satisfied (digits
have to be in the same order).
The function signature is
int foo (int n1, int n2);
You must call the following two functions in your answer
You are not allowed to use any data structure such as array, hashmap etc.
// *returns the number of digits in n.*
static int length(int n) {
int numDigits = 0;
while (n > 0) { // Assumes n is >= 0
numDigits++;
n = n / 10;
}
if (numDigits == 0) {
numDigits = 1;
}
return numDigits;
}
*// returns the nth digit of number. Rightmost digit is digit 0. *
static int nthDigit(int number, int n) {
int digit = 0;
for (int i = 0; i <= n; i++) { // Assumes n is >= 0
digit = number % 10;
number /= 10;
}
return digit;
}
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/CAEvz5E%3DFV_eTthBUOyZhGaK5-%2BzGf5TQ%2BbR2t3P8o4DcBH6GkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.