「java求n次方」java求n次方函数

博主:adminadmin 2022-11-24 00:49:06 48

本篇文章给大家谈谈java求n次方,以及java求n次方函数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 中一个数的n次方怎么写

可以直接用现有的API,Math,pow(double m,double n)意思是m的n次方

另外你也可以自己写方法,

public static void main (String[] args ){

int m=3;//初始值

int n=3;//次方数

int result=1;//存放结果

while(n0) {

result*=m;

n--;

}

System.out.println(result);

}

希望能够帮助你,谢谢

java中一个数的n次方应该怎么写?

public class Test {

public static void main(String[] args){

double m = 2;

double n = 3;

//使用API,Math.pow(double m,double n) -- 'm' 的 'n' 次方

System.out.println("使用API:" + Math.pow(m, n));

//通过两种循环实现的 'm' 的 'n' 次方

System.out.println("使用while实现:" + MToThePowerOfNByWhile(m,n));

System.out.println("使用for实现:" + MToThePowerOfNByFor(m,n));

}

public static double MToThePowerOfNByWhile(double m,double n)

{

double result = 1;

while(n 0)

{

result *= m;

n--;

}

return result;

}

public static double MToThePowerOfNByFor(double m,double n)

{

double result = 1;

for(int i = 0;in;i++)

{

result *= m;

}

return result;

}

}

如何使用Java计算次方

计算2的N次方

时间限制: 1000ms内存限制: 65536kB

描述

任意给定一个正整数N(N=100),计算2的N次方的值。

输入

输入只有一个正整数N。

输出

输出2的N次方的值。

样例输入

5

样例输出

32

参考代码

[java] view plain copy print?

import java.util.*;

public class Main {

public final static int SIZE = 30;

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

Scanner cin = new Scanner(System.in);

int n = cin.nextInt();

int res[] = new int[SIZE + 1];

int i;

for(i = 0;i SIZE;++ i){

res[i] = 0;

}

res[0] = 1;

while(n 0){

for(i = 0;i SIZE;++ i){

res[i] *= 2;

}

for(i = 0;i SIZE;++ i){

if(res[i] 9){

res[i + 1] += res[i] / 10;

res[i] %= 10;

}

}

n --;

}

boolean bl = false;

StringBuffer bf = new StringBuffer();

for(i = SIZE;i = 0;-- i){

if(res[i] != 0 || bl){

bf.append(res[i]);

bl = true;

}

}

System.out.println(bf);

}

}

根据高位低位改进的代码:

[java] view plain copy print?

/*

* Title :power 2

* From :

* Time :2011-10-11 21:10PM

* Author :Eric Zhou,binfeihan

* Email :binfeihan@126.com

*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException{

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

int n = Integer.parseInt(cin.readLine().trim());

System.out.println(my_power_2(n));

//System.out.println(Long.MAX_VALUE);

//System.out.println(Long.MIN_VALUE);

}

public static StringBuffer my_power_2(int N){

StringBuffer v = new StringBuffer("");

long num[] = new long[2];

num[1] = 1;

if(N 62){

num[0] = 1;

num[0] = num[0](N - 62);

num[1] = num[1]62;

String s = String.valueOf(num[1]);

int size = 30,i = 0,j = 0;

long n[] = new long[size + 1];

//System.out.println(num[0]+" "+s);

for(i = s.length() - 1;i = 0;-- i){

n[j ++] = (long) (num[0] * (s.charAt(i) - '0'));

//System.out.println(n[j - 1]);

}

for(i = 0;i size;++ i){

while(n[i] 9){

n[i + 1] += n[i] / 10;

n[i] %= 10;

}

}

boolean bl = false;

for(i = size;i = 0;-- i){

if(n[i] != 0 || bl){

v.append(n[i]);

bl = true;

}

}

}else{

num[1] = num[1] N;

v.append(String.valueOf(num[1]));

}

return v;

}

}

关于java求n次方和java求n次方函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。