本文目录一览:
求编个java程序要求:编写一个求大数的阶乘,不能用biginteger,并且使用数组不能浪费,不
做毕业设计用的吧?我两年前帮哥们写过一个,乘法加法和一些简单的用法都写好了。照着这个改改就行了
public class MyBigInt {
public static final int[] ten = { 10 };
public static final int[] zero = { 0 };
public static final int[] one = { 1 };
int[] myInt;
public int[] getMyInt() {
return myInt;
}
public void setMyInt(int[] myInt) {
this.myInt = myInt;
}
public MyBigInt() {
myInt = zero;
}
public MyBigInt(String a) {
myInt = new int[a.length()];
for (int i = 0; i myInt.length; i++) {
myInt[i] = Integer.parseInt(String.valueOf(a.charAt(myInt.length
- i - 1)));
}
}
public MyBigInt chengFa(MyBigInt myBigInt) {
int[] myIntA = myInt;
int[] myIntB = myBigInt.getMyInt();
int a, b;
int s = 0;
int c = 0;
int[] tmp, result;
tmp = new int[myIntA.length + myIntB.length];
result = new int[myIntA.length + myIntB.length];
for (int i = 0; i myIntA.length; i++) {
for (int j = 0; j myIntB.length; j++) {
a = myIntA[i];
b = myIntB[j];
s = (a * b);
tmp[i + j] += s;
}
}
for (int i = 0; i tmp.length - 1; i++) {
c = tmp[i] / 10;
tmp[i] = tmp[i] % 10;
tmp[i + 1] = tmp[i + 1] + c;
}
for (int i = 0; i tmp.length; i++) {
result[i] = tmp[i];
}
while (result[result.length - 1] == 0) {
tmp = new int[result.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = result[i];
}
result = tmp;
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public void show(int[] i) {
String s = "";
int index = i.length - 1;
for (int j = 0; j i.length; j++) {
if (index == i.length - 1 i[index] == 0) {
index--;
continue;
} else {
s += i[index];
index--;
}
}
System.out.println(s);
}
public String toString() {
int[] i = getMyInt();
String s = "";
int index = i.length - 1;
if (i.length != 1) {
for (int j = 0; j i.length; j++) {
if (index == i.length - 1 i[index] == 0) {
index--;
continue;
} else {
s += i[index];
index--;
}
}
} else {
s += i[index];
}
return s;
}
public boolean mod2() {
boolean flag = false;
int[] i = myInt;
if (i[0] % 2 == 0) {
flag = true;
}
return flag;
}
public MyBigInt jiaFa(MyBigInt myBigInt) {
int[] myIntA = myInt;
int[] myIntB = myBigInt.getMyInt();
int[] tmp, result;
int a;
if (myIntA.length = myIntB.length) {
tmp = new int[myIntA.length + 1];
for (int i = 0; i myIntB.length; i++) {
a = myIntA[i] + myIntB[i] + tmp[i];
tmp[i] = a % 10;
tmp[i + 1] = a / 10;
}
for (int i = myIntB.length; i myIntA.length; i++) {
a = tmp[i] + myIntA[i];
tmp[i] = a % 10;
tmp[i + 1] = a / 10;
}
} else {
tmp = new int[myIntB.length + 1];
for (int i = 0; i myIntA.length; i++) {
a = myIntA[i] + myIntB[i] + tmp[i];
tmp[i] = a % 10;
tmp[i + 1] = a / 10;
}
for (int i = myIntA.length; i myIntB.length; i++) {
a = tmp[i] + myIntB[i];
tmp[i] = a % 10;
tmp[i + 1] = a / 10;
}
}
result = tmp;
while (result[result.length - 1] == 0) {
tmp = new int[result.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = result[i];
}
result = tmp;
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public MyBigInt jianYi() {
int[] myIntA = myInt;
myIntA[0] -= 1;
MyBigInt r = new MyBigInt("");
r.setMyInt(myIntA);
return r;
}
public boolean eqOne() {
boolean flag = false;
int[] myIntA = myInt;
int[] tmp;
while (myIntA[myIntA.length - 1] == 0) {
tmp = new int[myIntA.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = myIntA[i];
}
myIntA = tmp;
}
if (myIntA.length == 1 myIntA[0] == 1) {
flag = true;
}
return flag;
}
public MyBigInt chu2() {
int[] myIntA = myInt;
int num;
int[] tmp, result;
tmp = new int[myIntA.length];
for (int i = 0; i tmp.length; i++) {
tmp[i] = myIntA[myIntA.length - i - 1];
}
myIntA = tmp;
result = new int[myIntA.length];
for (int i = 0; i myIntA.length - 1; i++) {
num = myIntA[i] / 2;
result[i] = num;
myIntA[i + 1] += ((myIntA[i] % 2) * 10);
}
result[result.length - 1] = (myIntA[result.length - 1] / 2);
tmp = new int[result.length];
for (int i = 0; i result.length; i++) {
tmp[i] = result[result.length - i - 1];
}
result = tmp;
while (result[result.length - 1] == 0) {
tmp = new int[result.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = result[i];
}
result = tmp;
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public MyBigInt jianFa(MyBigInt myBigInt) {
int[] myIntA = myInt;
int[] myIntB = myBigInt.getMyInt();
int[] tmp = new int[myIntA.length];
int[] result;
int c = 0;
for (int i = 0; i tmp.length; i++) {
if (myIntA.length == myIntB.length) {
if (myIntA[i] = (myIntB[i] + c)) {
tmp[i] = myIntA[i] - myIntB[i] - c;
c = 0;
} else {
tmp[i] = myIntA[i] + 10 - myIntB[i] - c;
c = 1;
if (i == tmp.length - 1 c == 1) {
return new MyBigInt();
}
}
} else if (myIntA.length myIntB.length) {
if (myIntB.length - i 0) {
if (myIntA[i] = (myIntB[i] + c)) {
tmp[i] = myIntA[i] - myIntB[i] - c;
c = 0;
} else {
tmp[i] = myIntA[i] + 10 - myIntB[i] - c;
c = 1;
}
} else {
if (myIntA[i] - c = 0) {
tmp[i] = myIntA[i] - c;
c = 0;
} else {
tmp[i] = myIntA[i] + 10 - c;
c = 1;
}
}
} else if (myIntA.length myIntB.length) {
return new MyBigInt();
}
}
result = tmp;
while (result[result.length - 1] == 0) {
if (result.length != 1) {
tmp = new int[result.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = result[i];
}
result = tmp;
} else {
return new MyBigInt();
}
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public MyBigInt youbian(int n) {
int[] myIntA = myInt;
int[] result = new int[n / 2];
for (int i = 0; i result.length; i++) {
result[i] = myIntA[i];
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public MyBigInt zuobian(int n) {
int[] myIntA = myInt;
int x;
int[] result;
if (n % 2 == 0) {
x = n / 2;
result = new int[x];
for (int i = 0; i result.length; i++) {
result[i] = myIntA[x + i];
}
} else {
x = (n + 1) / 2;
result = new int[x];
for (int i = 0; i result.length; i++) {
result[i] = myIntA[x + i - 1];
}
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
public MyBigInt cheng2() {
int[] myIntA = myInt;
int[] result = new int[myIntA.length + 1];
int[] tmp;
for (int i = 0; i myIntA.length; i++) {
result[i] += (myIntA[i] * 2) % 10;
result[i + 1] = (myIntA[i] * 2) / 10;
}
while (result[result.length - 1] == 0) {
tmp = new int[result.length - 1];
for (int i = 0; i tmp.length; i++) {
tmp[i] = result[i];
}
result = tmp;
}
MyBigInt r = new MyBigInt("");
r.setMyInt(result);
return r;
}
}