/***********************************************************************
Problem: Numbers are serialized increasingly into a sequence in the format
of 0123456789101112131415..., which each digit occupies a position in the
sequence. For instance, the digit in the position 5 is 5, in the position
13 is 1, in the position 19 is 4, and so on.
Please write a function/method to get the digit on any given position.
***********************************************************************/
#include <iostream>
using namespace std;
int countOfIntegers(int digits);
int digitAtIndex(int index, int digits);
int beginNumber(int digits);
int digitAtIndex(int index)
{
if(index < 0)
return -1;
int digits = 1;
while(true)
{
int numbers = countOfIntegers(digits);
if(index < numbers * digits)
return digitAtIndex(index, digits);
index -= digits * numbers;
digits++;
}
return -1;
}
int countOfIntegers(int digits)
{
if(digits == 1)
return 10;
int count = 1;
for(int i = 1; i < digits; ++i)
count *= 10;
return 9 * count;
}
int digitAtIndex(int index, int digits)
{
int number = beginNumber(digits) + index / digits;
int indexFromRight = digits - index % digits;
for(int i = 1; i < indexFromRight; ++i)
number /= 10;
return number % 10;
}
int beginNumber(int digits)
{
if(digits == 1)
return 0;
int begin = 1;
for(int i = 1; i < digits; ++i)
begin *= 10;
return begin;
}
//================================= Test Code =================================
void test( char* testName, int inputIndex, int expectedOutput)
{
if(digitAtIndex(inputIndex) == expectedOutput)
cout << testName << " passed." << endl;
else
cout << testName << " FAILED." << endl;
}
int main()
{
test("Test1", 0, 0);
test("Test2", 1, 1);
test("Test3", 9, 9);
test("Test4", 10, 1);
test("Test5", 189, 9); // last digit of 99
test("Test6", 190, 1); // first digit of 100
test("Test7", 1000, 3); // first digit of 370
test("Test8", 1001, 7); // middle digit of 370
test("Test9", 1002, 0); // last digit of 370
return 0;
}
LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqClByb2JsZW06IE51bWJlcnMgYXJlIHNlcmlhbGl6ZWQgaW5jcmVhc2luZ2x5IGludG8gYSBzZXF1ZW5jZSBpbiB0aGUgZm9ybWF0IApvZiAwMTIzNDU2Nzg5MTAxMTEyMTMxNDE1Li4uLCB3aGljaCBlYWNoIGRpZ2l0IG9jY3VwaWVzIGEgcG9zaXRpb24gaW4gdGhlIApzZXF1ZW5jZS4gRm9yIGluc3RhbmNlLCB0aGUgZGlnaXQgaW4gdGhlIHBvc2l0aW9uIDUgaXMgNSwgaW4gdGhlIHBvc2l0aW9uIAoxMyBpcyAxLCBpbiB0aGUgcG9zaXRpb24gMTkgaXMgNCwgYW5kIHNvIG9uLgoKUGxlYXNlIHdyaXRlIGEgZnVuY3Rpb24vbWV0aG9kIHRvIGdldCB0aGUgZGlnaXQgb24gYW55IGdpdmVuIHBvc2l0aW9uLgoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgY291bnRPZkludGVnZXJzKGludCBkaWdpdHMpOwppbnQgZGlnaXRBdEluZGV4KGludCBpbmRleCwgaW50IGRpZ2l0cyk7CmludCBiZWdpbk51bWJlcihpbnQgZGlnaXRzKTsKCgppbnQgZGlnaXRBdEluZGV4KGludCBpbmRleCkKewogICAgaWYoaW5kZXggPCAwKQogICAgICAgIHJldHVybiAtMTsKCgogICAgaW50IGRpZ2l0cyA9IDE7CiAgICB3aGlsZSh0cnVlKQogICAgewogICAgICAgIGludCBudW1iZXJzID0gY291bnRPZkludGVnZXJzKGRpZ2l0cyk7CiAgICAgICAgaWYoaW5kZXggPCBudW1iZXJzICogZGlnaXRzKQogICAgICAgICAgICByZXR1cm4gZGlnaXRBdEluZGV4KGluZGV4LCBkaWdpdHMpOwoKCiAgICAgICAgaW5kZXggLT0gZGlnaXRzICogbnVtYmVyczsKICAgICAgICBkaWdpdHMrKzsKICAgIH0KICAgIHJldHVybiAtMTsKfQoKaW50IGNvdW50T2ZJbnRlZ2VycyhpbnQgZGlnaXRzKQp7CiAgICBpZihkaWdpdHMgPT0gMSkKICAgICAgICByZXR1cm4gMTA7CgoKICAgIGludCBjb3VudCA9IDE7CiAgICBmb3IoaW50IGkgPSAxOyBpIDwgZGlnaXRzOyArK2kpCiAgICAgICAgY291bnQgKj0gMTA7CiAgICByZXR1cm4gOSAqIGNvdW50Owp9CgppbnQgZGlnaXRBdEluZGV4KGludCBpbmRleCwgaW50IGRpZ2l0cykKewogICAgaW50IG51bWJlciA9IGJlZ2luTnVtYmVyKGRpZ2l0cykgKyBpbmRleCAvIGRpZ2l0czsKICAgIGludCBpbmRleEZyb21SaWdodCA9IGRpZ2l0cyAtIGluZGV4ICUgZGlnaXRzOwogICAgZm9yKGludCBpID0gMTsgaSA8IGluZGV4RnJvbVJpZ2h0OyArK2kpCiAgICAgICAgbnVtYmVyIC89IDEwOwogICAgcmV0dXJuIG51bWJlciAlIDEwOwp9CgppbnQgYmVnaW5OdW1iZXIoaW50IGRpZ2l0cykKewogICAgaWYoZGlnaXRzID09IDEpCiAgICAgICAgcmV0dXJuIDA7CgoKICAgIGludCBiZWdpbiA9IDE7CiAgICBmb3IoaW50IGkgPSAxOyBpIDwgZGlnaXRzOyArK2kpCiAgICAgICAgYmVnaW4gKj0gMTA7CiAgICByZXR1cm4gYmVnaW47Cn0KCi8vPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09IFRlc3QgQ29kZSA9PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0Kdm9pZCB0ZXN0KCBjaGFyKiB0ZXN0TmFtZSwgaW50IGlucHV0SW5kZXgsIGludCBleHBlY3RlZE91dHB1dCkKewogICAgaWYoZGlnaXRBdEluZGV4KGlucHV0SW5kZXgpID09IGV4cGVjdGVkT3V0cHV0KQogICAgICAgIGNvdXQgPDwgdGVzdE5hbWUgPDwgIiBwYXNzZWQuIiA8PCBlbmRsOwogICAgZWxzZQogICAgICAgIGNvdXQgPDwgdGVzdE5hbWUgPDwgIiBGQUlMRUQuIiA8PCBlbmRsOwp9CgoKaW50IG1haW4oKQp7CiAgICB0ZXN0KCJUZXN0MSIsIDAsID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