#include <stdio.h>
int rLookupAr(int *ar, int n, int target);
int main()
{
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("rLookupAr() with 0: %d\n", rLookupAr
(a
, 10, 0)); printf("rLookupAr() with 3: %d\n", rLookupAr
(a
, 10, 3)); printf("rLookupAr() with 9: %d\n", rLookupAr
(a
, 10, 9)); printf("rLookupAr() with 20: %d\n", rLookupAr
(a
, 10, 20)); printf("rLookupAr() with -1: %d\n", rLookupAr
(a
, 10, -1));
return 0;
}
int rLookupAr(int *ar, int n, int target)
{
if (n == 0) return -1;
if (ar[0] == target) return 0;
int _ret = rLookupAr(ar + 1, n - 1, target);
if(_ret != -1) return ++_ret;
else return -1;
}
I2luY2x1ZGUgIDxzdGRpby5oPgogCmludCByTG9va3VwQXIoaW50ICphciwgaW50IG4sIGludCB0YXJnZXQpOwogCmludCBtYWluKCkKewogICAgaW50IGFbMTBdID0gezAsIDEsIDIsIDMsIDQsIDUsIDYsIDcsIDgsIDl9OwogICAgICAgICAgICAKICAgIHByaW50Zigickxvb2t1cEFyKCkgd2l0aCAwOiAlZFxuIiwgIHJMb29rdXBBcihhLCAxMCwgMCkpOwogICAgcHJpbnRmKCJyTG9va3VwQXIoKSB3aXRoIDM6ICVkXG4iLCAgckxvb2t1cEFyKGEsIDEwLCAzKSk7CiAgICBwcmludGYoInJMb29rdXBBcigpIHdpdGggOTogJWRcbiIsICByTG9va3VwQXIoYSwgMTAsIDkpKTsKICAgIHByaW50Zigickxvb2t1cEFyKCkgd2l0aCAyMDogJWRcbiIsIHJMb29rdXBBcihhLCAxMCwgMjApKTsKICAgIHByaW50Zigickxvb2t1cEFyKCkgd2l0aCAtMTogJWRcbiIsIHJMb29rdXBBcihhLCAxMCwgLTEpKTsKICAgICAgICAgICAgCiAgICByZXR1cm4gMDsKfQogCmludCByTG9va3VwQXIoaW50ICphciwgaW50IG4sIGludCB0YXJnZXQpCnsKICAgIGlmIChuID09IDApIHJldHVybiAtMTsKICAgIGlmIChhclswXSA9PSB0YXJnZXQpIHJldHVybiAwOwogCiAgICBpbnQgX3JldCA9IHJMb29rdXBBcihhciArIDEsIG4gLSAxLCB0YXJnZXQpOwogCiAgICBpZihfcmV0ICE9IC0xKSByZXR1cm4gKytfcmV0OwogICAgZWxzZSAgICAgICAgICByZXR1cm4gLTE7Cn0K