small calculator



Title: Alphabet Game


Xiao Ming is often addicted to playing LOL games. Once he wanted to challenge Master K, but Master K said:
"Let's play a game of filling in letters in the blanks first. If you can't beat me, don't play LOL again."


Master K drew a line of n grids on the paper, and asked Xiaoming and him to alternately fill in letters in them.


And:


1. When it's someone's turn to fill in, they can only fill in L or O in a certain space
2. Whoever makes the letter "LOL" first wins.
3. If all the squares are filled and still can't form LOL, then it will be a draw.


Xiao Ming has failed several times in the experiment. He is very ashamed. I hope you can use the computer to help him solve this mystery.


The input format of this question is:
the first line, the number n (n<10), indicating that there are n initial situations below.
Next, n lines, each with a string, represent the starting position.
  For example: "******", which means there are 6 spaces.
  "L****", which means there is a letter L on the left and 4 spaces on the right.


It is required to output n numbers, indicating that for each position, if Xiao Ming fills in first, when Master K always uses the strongest move, Xiao Ming's best result. 1 means
win -1 means must
lose 0 means
draw _ _
















1
1


Resource convention:
Peak memory consumption < 256M
CPU consumption < 1000ms




Please output strictly according to the requirements, and do not superfluous to print redundant content like: "Please enter...".


All code is placed in the same source file, after debugging, copy and submit the source code.

Note: Do not use the package statement. Do not use features of jdk1.7 and above.


package Main;
/**
 * This question is more troublesome
 * Four operations can convert the number of operations into a decimal String, and then use BigInteger to calculate
 * Then the decimal is converted to n-base, and n-base is converted to decimal
 * There is also the calculation of a^b, which can be accelerated by using fast power
 */
import java.math.BigInteger;
import java.util. *;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		//-----------startTime
//			Long start = System.currentTimeMillis();
		//
		int t = in.nextInt();
		String cur = "";
		Long num = 0L;
		int jin = 10;//The default is decimal
		while (t-- != 0) {
			String a = in.next();
			if (a.equals("CLEAR")) {
				cur = "";
			} else if (a.equals("EQUAL")) {
				System.out.println(cur);
			} else if (a.equals("CHANGE")) {
				int k = in.nextInt();
				jin = k;
				cur = ten_changeTo_N (num, k);
//					System.out.println(cur);
			} else if (a.equals("NUM")) {
				Long tmp = in.nextLong();
				num = tmp;
				cur = String.valueOf(tmp);
			} //Operation
			else if (a.equals("ADD")) {
				String ctmp = N_changeTo_TEN(cur, jin);
				String b = in.next();//"NUM"
				String addb = in.next();//jin base number
				addb = N_changeTo_TEN(addb, jin);
//					System.out.println(addb);
				cur = add(ctmp, addb);
				//Convert to jin base number
				num = Long.valueOf(cur);//Convert to Long value
				cur = ten_changeTo_N(num, jin);
				t--;
			} else if (a.equals("SUB")) {
				String ctmp = N_changeTo_TEN(cur, jin);
				String b = in.next();//"NUM"
				String subb = in.next();//jin base number
				subb = N_changeTo_TEN(subb, jin);
				cur = sub(ctmp, subb);
				//Convert to jin base number
				num = Long.valueOf(cur);//Convert to Long value
				cur = ten_changeTo_N(num, jin);
				t--;
			} else if (a.equals("MUL")) {
				String ctmp = N_changeTo_TEN(cur, jin);
				String b = in.next();//"NUM"
				String mulb = in.next();//jin base number
				mulb = N_changeTo_TEN(mulb, jin);
				cur = sub (ctmp, mulb);
				//Convert to jin base number
				num = Long.valueOf(cur);//Convert to Long value
				cur = ten_changeTo_N(num, jin);
				t--;
			} else if (a.equals("DIV")) {
				String ctmp = N_changeTo_TEN(cur, jin);
				String b = in.next();//"NUM"
				String divb = in.next();//jin base number
				divb = N_changeTo_TEN(divb, jin);
				cur = sub(ctmp, divb);
				//Convert to jin base number
				num = Long.valueOf(cur);//Convert to Long value
				cur = ten_changeTo_N(num, jin);
				t--;
			} else if (a.equals("MOD")) {
				String ctmp = N_changeTo_TEN(cur, jin);
				String b = in.next();//"NUM"
				String modb = in.next();//jin base number
				modb = N_changeTo_TEN (modb, jin);
				cur = sub(ctmp, modb);
				//Convert to jin base number
				num = Long.valueOf(cur);//Convert to Long value
				cur = ten_changeTo_N(num, jin);
				t--;
			}
		}
		
		//--------endTime
//			Long end = System.currentTimeMillis();
		//The all execute time
//			System.out.println(end - start);
	}
	
	//Arithmetic
	private static String add(String ctmp, String addb) {
		BigInteger a = new BigInteger(ctmp);
		BigInteger b = new BigInteger(addb);
		return String.valueOf(a.add(b));
	}
	private static String sub(String ctmp, String addb) {
		BigInteger a = new BigInteger(ctmp);
		BigInteger b = new BigInteger(addb);
		return String.valueOf(a.subtract(b));
	}
	private static String mul(String ctmp, String addb) {
		BigInteger a = new BigInteger(ctmp);
		BigInteger b = new BigInteger(addb);
		return String.valueOf(a.multiply(b));
	}
	private static String div(String ctmp, String addb) {
		BigInteger a = new BigInteger(ctmp);
		BigInteger b = new BigInteger(addb);
		return String.valueOf(a.divide(b));
	}
	private static String mod(String ctmp, String addb) {
		BigInteger a = new BigInteger(ctmp);
		BigInteger b = new BigInteger(addb);
		return String.valueOf(a.mod(b));
	}

	private static String ten_changeTo_N(Long a, int jin) {
		String s = "", result = "";
		if (jin <= 10) {
			while (a > 0) {
				Long k = a%jin;
				s = s + (char)(k + '0');
				a = a / jin;
			}
			for (int i = 0; i < s.length(); i++) {
				result += s.charAt(s.length() - i - 1);
			}
		} else {
			while (a > 0) {
				Long k = a%jin;
				if (k >= 10) {
					k -= 10;
					s = s + (char)('A' + k);
				} else {
					s = s + (char)(k + '0');
				}
				a = a / jin;
			}
			for (int i = 0; i < s.length(); i++) {
				result += s.charAt(s.length() - i - 1);
			}
		}
		return result;
	}
	private static String N_changeTo_TEN(String n, int jin) {
		Long sum = 0L;
		for (int i = 0; i < n.length(); i++) {
			char ch = n.charAt(n.length() - 1 - i);
			if (ch >= '0' && ch <= '9') {
				Long a = (long)(ch - '0');
				sum += a*fast_mod((long) jin, i);
			} else {
				Long a = (long) (ch - 'A' + 10);
				sum += a*fast_mod((long) jin, i);
			}
			
		}
		return String.valueOf(sum).toString();
	}
	//Fast power calculation a^b;
	public static Long fast_mod(Long a, int b) {
		Long years = 1L;
		Long base = a;
		while (b > 0) {
			if (b%2 != 0) {
				years = years*base;
			}
			base = base*base;
			b >>= 1;
		}
		return ans;
	}
	
	private static void test() {
		BigInteger a = new BigInteger("2");
		BigInteger time = new BigInteger("0");
		BigInteger end = new BigInteger("63");
		do {
			if (time.equals(end)) break;
			a = a.multiply(new BigInteger("2"));
			time = time.add(new BigInteger("1"));
		} while (true);
		System.out.println(a);
	}
}

Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597362&siteId=291194637