Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Multiplying Two Base-36 Numbers
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; /** * Input: * BASE (основание системы исчисления (2 .. 36)) * Строка A * Строка B * * Output: * A*B */ class Rextester { private static final String alpha = "0123456789abcdefghijklmnopqrstuvwxyz"; public static void main(String[] args) { Scanner in = new Scanner(System.in); int BASE = in.nextInt(); String A = in.next(); String B = in.next(); int lenA = A.length(); int lenB = B.length(); int lenC = lenA + lenB + 1; // maximum length for result char[] C = new char[lenC]; // initializing the char array for (int i = 0; i < lenC; i++) C[i] = '0'; // going from right to left (from lesser digit to greatest) for (int i = lenA - 1; i >= 0; i--) { for (int j = lenB - 1; j >= 0; j--) { char charA = A.charAt(i); // character of A at i-th position char charB = B.charAt(j); // character of B at i-th position char charC = C[i + j + 2]; char charHigher = C[i + j + 1]; char charHighest = C[i + j]; int digA = charToInt(charA); // A[i] in base-10 int digB = charToInt(charB); // B[i] in base-10 int digC = charToInt(charC); int digHigher = charToInt(charHigher); int digHighest = charToInt(charHighest); // sum of multiplication of both digits, existing value in current place // and overflow / super-overflow in the right places int mult = digA * digB + digC + digHigher * BASE + digHighest * BASE * BASE; int higher = mult / BASE; // setting overflow value int highest = higher / BASE; // setting super-overflow value mult = mult % BASE; // converting from base-10 to base-36 // up to three digits can be produced by multiplication // so we write result to three places C[i + j + 2] = alpha.charAt(mult); C[i + j + 1] = alpha.charAt(higher % BASE); C[i + j] = alpha.charAt(highest); } } // do not print first digits if they are 0 int z = 0; while (C[z] == '0') z++; for (int i = z; i < lenC; i++) System.out.print(C[i]); // WHY???? // 999 // 999 // 898001 //AND NOT 998001 } private static int charToInt(char digit) { return alpha.indexOf(digit); } }
run
|
edit
|
history
|
help
0
"encryption"
Sumod
jb11.0 threads tick tock
Kochergina_4
formuły
JAVA # Klavyeden Girilen cümlelerin en uzununu bulma
http://stackoverflow.com/questions/23175927/how-to-clone-object-defined-by-interface
Queue implementation
queue_using_stack
Binary Tree Max path Sum