(UVA) Number Sequence - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1647
The solution below find the beginning of each sequence as the first step. When we have the position of the first element of the sequence immediately before the required position, it is only necessary to iterate over this sequence until we find the position we want.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, Integer> map;
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
map = new HashMap<>();
map.put(1, 1);
map.put(10, 2);
map.put(100, 3);
map.put(1000, 4);
map.put(10000, 5);
map.put(100000, 6);
map.put(1000000, 7);
map.put(10000000, 8);
map.put(100000000, 9);
map.put(1000000000, 10);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
long position = sc.nextLong();
long iPosition = 1;
long increment = 0;
long start = 1;
// find the beginning of each sequence
for (int j = 1; ; j++) {
if (j%(start*10) == 0) {
start *= 10;
}
increment += map.get((int)start);
if (iPosition+increment > position) {
break;
}
iPosition += increment;
}
start = 1;
boolean foundPos = false;
for (long j = iPosition; !foundPos; ) {
String s = start+"";
for (int k = 0; k < s.length(); k++) {
if (j == position) {
System.out.println(s.charAt(k));
foundPos = true;
break;
}
j++;
}
start++;
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
The solution below find the beginning of each sequence as the first step. When we have the position of the first element of the sequence immediately before the required position, it is only necessary to iterate over this sequence until we find the position we want.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, Integer> map;
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
map = new HashMap<>();
map.put(1, 1);
map.put(10, 2);
map.put(100, 3);
map.put(1000, 4);
map.put(10000, 5);
map.put(100000, 6);
map.put(1000000, 7);
map.put(10000000, 8);
map.put(100000000, 9);
map.put(1000000000, 10);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
long position = sc.nextLong();
long iPosition = 1;
long increment = 0;
long start = 1;
// find the beginning of each sequence
for (int j = 1; ; j++) {
if (j%(start*10) == 0) {
start *= 10;
}
increment += map.get((int)start);
if (iPosition+increment > position) {
break;
}
iPosition += increment;
}
start = 1;
boolean foundPos = false;
for (long j = iPosition; !foundPos; ) {
String s = start+"";
for (int k = 0; k < s.length(); k++) {
if (j == position) {
System.out.println(s.charAt(k));
foundPos = true;
break;
}
j++;
}
start++;
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment