Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
public class Mult{
|
||||
public static void main(String[] args){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int first = sc.nextInt();
|
||||
int second = sc.nextInt();
|
||||
|
||||
if(first < 0){
|
||||
first = -first;
|
||||
second = -second;
|
||||
}
|
||||
|
||||
Map<Integer, Integer> columns = new HashMap<Integer, Integer>();
|
||||
columns.put(first, second);
|
||||
int sum = isEven(first)? 0 : second;
|
||||
do{
|
||||
first = halveInt(first);
|
||||
second = doubleInt(second);
|
||||
columns.put(first, second);
|
||||
if(!isEven(first)){
|
||||
sum += second;
|
||||
}
|
||||
}while(first > 1);
|
||||
|
||||
System.out.println(sum);
|
||||
}
|
||||
|
||||
public static int doubleInt(int doubleMe){
|
||||
return doubleMe << 1; //shift left
|
||||
}
|
||||
|
||||
public static int halveInt(int halveMe){
|
||||
return halveMe >>> 1; //shift right
|
||||
}
|
||||
|
||||
public static boolean isEven(int num){
|
||||
return (num & 1) == 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* This method will use ethiopian styled multiplication.
|
||||
* @param a Any non-negative integer.
|
||||
* @param b Any integer.
|
||||
* @result a multiplied by b
|
||||
*/
|
||||
public static int ethiopianMultiply(int a, int b) {
|
||||
if(a==0 || b==0) {
|
||||
return 0;
|
||||
}
|
||||
int result = 0;
|
||||
while(a>=1) {
|
||||
if(!isEven(a)) {
|
||||
result+=b;
|
||||
}
|
||||
b = doubleInt(b);
|
||||
a = halveInt(a);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is an improved version that will use
|
||||
* ethiopian styled multiplication, and also
|
||||
* supports negative parameters.
|
||||
* @param a Any integer.
|
||||
* @param b Any integer.
|
||||
* @result a multiplied by b
|
||||
*/
|
||||
public static int ethiopianMultiplyWithImprovement(int a, int b) {
|
||||
if(a==0 || b==0) {
|
||||
return 0;
|
||||
}
|
||||
if(a<0) {
|
||||
a=-a;
|
||||
b=-b;
|
||||
} else if(b>0 && a>b) {
|
||||
int tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
int result = 0;
|
||||
while(a>=1) {
|
||||
if(!isEven(a)) {
|
||||
result+=b;
|
||||
}
|
||||
b = doubleInt(b);
|
||||
a = halveInt(a);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue