本文目錄一覽:
如何在java中對一個數開n次方
用pow,第一行是開2的2次方,第二行使用pow的方法開2的二次方
System.out.println(Math.sqrt(2));
System.out.println(Math.pow(2, (double)1/2));
你如你想開n次方,就把
(double)1/2
換成
(double)1/n
如何使用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編寫程序計算x的n次冪
import
java.util.Scanner;
/*
*
用java編寫程序計算x的n次冪
*
*/
public
class
Test40003
{
public
static
void
main(String[]
args)
{
int
repeat;//定義要冪次
//int
i,
n;
double
x,
mypow=1;
Scanner
in=new
Scanner(System.in);//從控制台輸入數字,比如
2
1.5,
//前面是整數,後面是要做冪次運算的數,中間用空格隔開
repeat=in.nextInt();//獲取輸入的冪次
x=in.nextDouble();//獲取要進行冪次運算的數
System.out.println(“現在要做”+x+”的”+repeat+”次冪運算!”);
for(int
ri=1;
ri=repeat;
ri++){
mypow
=
mypow*x;
}
System.out.println(x+”的”+repeat+”次冪運算的結果是:”+mypow);
}
}
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;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/189629.html