本文目錄一覽:
java中如何表示a的b次方
java中乘方用Math.pow來實現,舉例如下:
public
static
void
main(String[]
args)
{
int
a=2;
/*底數*/
int
b=3;
/*乘方*/
double
f=Math.pow(a,b);
/*a和b套用到此行的程式(a的b次方等於f)*/
System.out.println(「2的3次方等於」+f);
}
java中10的n次方怎麼表示
java中10的n次方的表示方式:
方法聲明:Math.pow(double m, double n)
參數說明:m為要求方的數,n為次方數
當然如果你願意也可以自己寫個方法來實現m的n次方,實現起來也相當簡單。
下面是自己寫的例子,我覺得用整數做參數就行了,一般都是整數去求方的。
public static long pow(long m, long n){
long result = 1L; //0次方時為1
for(int=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中算式的次方怎樣表達
不能直接寫的
只能 Q*Q*Q(Q的3次方)
或者你自己寫個方法
public class MyMath{
public static int Math(int a,int j){
int b=1;
for(int i = 0;i j; i++){
b=b*a;
}
if(a==0j==0) return 1;
else return b;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/289096.html