SDNU 1259.Problem G. play the guitar Java大数

1259.Problem G. play the guitar
Time Limit: 1000 MS    Memory Limit: 32768 KB

Description
lmh like playing the guitar, but he don’t satisfy others’ GTP. He want to write guitar score by himself. And he don’t want to use 53231323, it’s boring! He want to find a new rhythm. So he come up with a idea: get a GTP by computer and math. So he choose “n^k” this expression. But we know, the guitar just have only six chords. So we have to change all 7,8,9 to 1,2,3(7 to 1, 8 to 2, 9 to 3, 0 means open string).


Now give you n and k, can you give lmh a prefect GTP?


Input
Input contains multiple test cases.


Each test case starts with two numbers n and k (0 < N <= 300, 0 < k <= 300).


Output
Calculate n^k and change all 7,8,9 to 1,2,3(7 to 1, 8 to 2, 9 to 3).


Sample Input
2 2
2 4
2 7
Sample Output
4
16

122


    其实第一场新生赛的时候我就想出个Java大数的题,大鹏没让我出......但是我感觉每次省赛都会有个Java大数的题,所以在我的强烈要求下,这道题诞生了。

    题意很好理解,就是求n^k,然后把这个数里面的7换成1,8换成2,9换成3输出就可以了。

    下面AC代码:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
	public static void main(String[] args)
	{
		Scanner cin=new Scanner(System.in);
		//BigDecimal ZERO=new BigDecimal(0);
		BigDecimal ONE=new BigDecimal(1);
		BigDecimal TWO=new BigDecimal(2);
		BigDecimal THREE=new BigDecimal(3);
		BigDecimal SEVEN=new BigDecimal(7);
		BigDecimal EIGHT=new BigDecimal(8);
		BigDecimal NINE=new BigDecimal(9);
		BigDecimal TEN=new BigDecimal(10);
		while(cin.hasNext())
		{
			BigDecimal n;
			BigDecimal t;
			BigDecimal jud;
			BigDecimal ten=new BigDecimal(1);
			BigDecimal ans=new BigDecimal(0);
			int k;
			//System.out.println("start");
			n=cin.nextBigDecimal();
			k=cin.nextInt();
			t=n.pow(k);
			while(t.compareTo(TEN)>0)
			{
				jud=t.remainder(TEN);
				if(jud.compareTo(SEVEN)==0)
					jud=ONE;
				else if(jud.compareTo(EIGHT)==0)
					jud=TWO;
				else if(jud.compareTo(NINE)==0)
					jud=THREE;
				ans=ans.add(ten.multiply(jud));
				t=t.divideToIntegralValue(TEN);
				ten=ten.multiply(TEN);
				//System.out.println(t + " " + ans + " " + jud);
			}
			if(t.compareTo(SEVEN)==0)
				t=ONE;
			else if(t.compareTo(EIGHT)==0)
				t=TWO;
			else if(t.compareTo(NINE)==0)
				t=THREE;
			ans=ans.add(ten.multiply(t));
			System.out.println(ans);
		}
	}
}

你可能感兴趣的:(SDNU)