java如何实现e的次方_java 大数据次方运算

该博客介绍了一种在Java中实现大数次方运算的方法,通过输入一个基数和指数,程序会计算基数的指数次方。文章提供了一个名为`Run`的类,包含了计算大数次方的主要逻辑,包括`Power`方法用于计算结果,`GetResult`方法处理进位,以及`CheckCarry`方法检查并处理进位。
摘要由CSDN通过智能技术生成

/**引用网络文档*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class Run

{

private static int radix = 307;

public static void main(String[] args) throws Exception

{

Run run = new Run();

int [] result = run.Power(radix);

logger.debug("++++ Caculate Result: +++++");

for(int i = result.length - 1; i >= 0; i--){

System.out.print(result[i]);

if(i % 50 ==0){

System.out.print(" ");

}

}

}

public static int input() {

String inline = null;

try {

BufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Please enter a power number for the radix 2: ");

inline = inStream.readLine();

int number = Integer.parseInt(inline);

return number;

} catch (NumberFormatException ex) {

System.out.println("Invalid!!");

return 0;

} catch (IOException e) {

System.out.println("Error!! ");

return 0;

}

}

int[] Power(int radix) throws Exception {

int[] result;

int n=input();

int size = (int) (n * Math.log10(radix)) + 1;

result = new int[size];

result[0] = 1;

if(n<0){

System.out.println("Sorry! Please enter a positive number!");

}

if (n == 0) {

result = new int[1];

result[0] = 1;

return result;

}

// get the result

for (int i = 0; i < n; i++) {

GetResult(result, radix);

}

return result;

}

void GetResult(int[] result, int n) {

int length = 0;

for (int i = result.length - 1; i >= 0; i--) {

if (result[i] > 0) {

length = i + 1; //get the highest bit

break;

}

}

for (int i = length - 1; i >= 0; i--) {

// calculate from the highest bit to the lowest

int temp = result[i] * n;

// get a carry

if (temp >= 10) {

// put a carry to a higher bit

result[i + 1] += temp / 10;

// check this bit weather need carry or not

CheckCarry(result, i + 1);

temp %= 10;

}

result[i] = temp;

}

}

void CheckCarry(int[] m, int index) {

if (m[index] >= 10) {

// put a carry to a higher bit

m[index + 1] += m[index] / 10;

// set the current bit

m[index] %= 10;

// check the next bit weather need carry or not

CheckCarry(m, index + 1);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值