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);
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值