前 我想要 1^1 到 n^n 的总和: 1^1 + 2^2 + 3^3 + .........+n^n 我知道怎么做,但问题是我只想要总和的最后十个数字,但我只需要使用原始数据。如何仅使用原始数据来计算大数。
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
short n = in.nextShort();
if(n < 0) {
System.out.println("please give a positive number");
return;
}
long l = rechnung(n);
System.out.println(l);
String str = Objects.toString(l, null);
String s = ziffer(str);
System.out.println(s);
}
public static long rechnung(short j) {
long summe = 0;
for(int i = 1; i <= j; i++) {
summe += Math.pow(i,i);
}
return summe;
}
public static String ziffer(String s) {
String str = "";
int k =s.length() - 10;
int cond = k + 9;
if(s.length() <= 10) {
return s;
}
for(int j = k; j <= cond; j++) {
str = str + s.charAt(j);
}
return str;
}
最佳答案
由于您只需要保留较低的 10 位数字,因此可以使用 % 10_000_000_000L 来保留每次计算所需的数字。
关于java - java中如何计算大数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213961/