(UVA) Naughty Sleepy Boys - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1368

The first step to solve this problem is to discover there index where each number composed by a specific number of digits start. Then, we try to increase the number and we check if the index related to the start of the number increased is valid (it did not exceed the desired index). The order of increasing the number is decreased by 10 every time this limit is exceeded.


import java.io.*;
import java.util.*;

class Main {
public static void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        String line = br.readLine();
        while (line != null) {
            int index = Integer.parseInt(line);
            
            int currIndex = 1;
            int currNumber = 1;
            int numDigits = 1;
            if (index > 9 && index <= 189) {
                currIndex = 10;
                currNumber = 10;
                numDigits = 2;
            }
            else if (index > 189 && index <= 2889) {
                currIndex = 190;
                currNumber = 100;
                numDigits = 3;
            }
            else if (index > 2889 && index <= 38889) {
                currIndex = 2890;
                currNumber = 1000;
                numDigits = 4;
            }
            else if (index > 38889 && index <= 488889) {
                currIndex = 38890;
                currNumber = 10000;
                numDigits = 5;
            }
            else if (index > 488889 && index <= 5888889) {
                currIndex = 488890;
                currNumber = 100000;
                numDigits = 6;
            }
            else if (index > 5888889 && index <= 68888889) {
                currIndex = 5888890;
                currNumber = 1000000;
                numDigits = 7;
            }
            else if (index > 68888889 && index <= 788888889) {
                currIndex = 68888890;
                currNumber = 10000000;
                numDigits = 8;
            }
            
            int multiplicator = currNumber;
            while (multiplicator != 0) {
           int totalDigits = multiplicator*numDigits;
            
                if (currIndex+totalDigits <= index) {
                    currIndex += totalDigits;
                    currNumber += multiplicator;
                    if (currIndex == index) {
                        break;
                    }
                }
                else {
                    multiplicator /= 10;
                }
            }
            
            int indexArray = numDigits-1;
            int currNumberTmp = currNumber;
            int[] currNumberArray = new int[numDigits];
            // convert the number into an array
            while (currNumberTmp != 0) {
                currNumberArray[indexArray--] = currNumberTmp%10;
                currNumberTmp /= 10;
            }
            for (int j = 0; j < numDigits; j++) {
                if (currIndex == index) {
                    bw.write(currNumberArray[j]+"\n");
                    break;
                }
                currIndex++;
            }
            currNumber++;                

            line = br.readLine();
        }
        
        bw.flush();
        bw.close();
               
        return;
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução