累乘问题

饺子里来点醋 2019-08-26 05:19:38
1-100累乘,用数组如何解决?
...全文
294 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
引用 11 楼 淡青の月的回复:
public static void main(String[] args) {

int[] arr = factorial(100);

print(arr);
}

public static void print(int[] arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
}

public static int[] factorial(int num) {
int[] result = { 1 };
if (num < 0) {
throw new RuntimeException();
}
if (num == 0) {
return result;
}
for (int i = 0; i < num; i++) {
result = multiplyPlus(result, i + 1);
}
return result;
}

public static int[] multiplyPlus(int[] arr, int num) {
int[] a = parseArray(num);
int[] result = new int[arr.length + a.length];
for (int i = a.length - 1; i >= 0; i--) {
int carry = 0;
for (int j = arr.length - 1; j >= 0; j--) {
int product = arr[j] * a[i] + carry;
int theUnit = product % 10;
result[i + j + 1] += theUnit;
carry = product / 10;
}
result[0] = carry;
}
int index = 0;
for (int i = 0; i < result.length; i++) {
if (result[i] != 0) {
break;
}
index++;
}
int[] array = new int[result.length - index];
System.arraycopy(result, index, array, 0, array.length);
return array;
}

public static int[] parseArray(int num) {
int count = 0;
int copy = num;
while (copy > 0) {
count++;
copy /= 10;
}
int[] result = new int[count];
for (int i = count - 1; i >= 0; i--) {
result[i] = num % 10;
num /= 10;
}
return result;
}
摩拜大神,抽空我看下~~~~~~☒
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
引用 12 楼 qybao的回复:
自己写个数组乘法器

public class Sample {
public static void main(String[] args) {
try {
int max = 100;
int[] result = multiply(new int[] {1}, new int[] {2});
for (int i=3; i<=max; i++) {
int[] tmp = new int[i>99 ? 3 : (i>9 ? 2 : 1)]; //整数变为数组,这里为了省事,可以抽出写成一个方法
for (int j=tmp.length-1, k=i; k>0; j--, k /= 10) {
tmp[j] = k % 10;
}
result = multiply(result, tmp);
}
System.out.print("result=");
for (int i=0, j=0; i<result.length; i++) {
if (result[i] > 0) { //为了去除前面多余的0
j++;
}
if (j == 0) continue;
System.out.print(result[i]); //从不为0的数开始输出
}
System.out.println();

} catch (Throwable e) {
e.printStackTrace();
}
}

public static int[] multiply(int[] a, int[] b) {//数组乘法
int[] result = new int[a.length+b.length]; //相乘结果的最大位数不超过乘数的长度+被乘数的长度
for (int i=a.length-1, k=result.length-1; i>=0; i--, k--) {
for (int j=b.length-1; j>=0; j--) {
result[k] += a[i] * b[j] % 10;
result[k-1] += a[i] * b[j] / 10;
}
}
for (int k=result.length-1; k>0; k--) { //进位整理
if (result[k] > 9) {
result[k-1] += result[k] / 10;
result[k] %= 10;
}
}

return result;
}
}
摩拜大神,抽空我看下~~~~~~☒
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
摩拜大神,抽空我看下~~~~~~
weixin_40290083 2019-08-27
  • 打赏
  • 举报
回复
以前写过一个大数的阶乘,如果使用语言所定义的数据类型,会存在溢出问题,就使用数组或者集合,每个元素代表一位数。这样就需要编写代码模拟手工计算乘法的方法,然后再循环调用该方法计算阶乘。
Swear_ls 2019-08-27
  • 打赏
  • 举报
回复
引用 3 楼 qq_39285019的回复:
[quote=引用 2 楼 Swear_ls的回复:]int a=1; for(int i=0;i<arr.length;i++){ a=a*arr[i]; }
内存溢出吧,这也不是用数组解决啊[/quote] 那就用bigdecimal,对于你说的数组,我没读懂题意。
淡青の月 2019-08-27
  • 打赏
  • 举报
回复
BigInteger sum = BigInteger.ONE;
BigInteger[] arr = new BigInteger[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = new BigInteger(i+1+"");
sum = sum.multiply(arr[i]);
}
System.out.println(sum);
求这个没啥用啊
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
引用 2 楼 Swear_ls的回复:
int a=1; for(int i=0;i<arr.length;i++){ a=a*arr[i]; }
内存溢出吧,这也不是用数组解决啊
qybao 2019-08-27
  • 打赏
  • 举报
回复
数组乘法的地方修改一下
for (int i=a.length-1, k=result.length-1; i>=0; i--, k--) {
for (int j=b.length-1, p=k; j>=0; j--, p--) { //这里p的位置也要变动
result[p] += a[i] * b[j] % 10;
result[p-1] += a[i] * b[j] / 10;
}
}
qybao 2019-08-27
  • 打赏
  • 举报
回复
自己写个数组乘法器

public class Sample {
public static void main(String[] args) {
try {
int max = 100;
int[] result = multiply(new int[] {1}, new int[] {2});
for (int i=3; i<=max; i++) {
int[] tmp = new int[i>99 ? 3 : (i>9 ? 2 : 1)]; //整数变为数组,这里为了省事,可以抽出写成一个方法
for (int j=tmp.length-1, k=i; k>0; j--, k /= 10) {
tmp[j] = k % 10;
}
result = multiply(result, tmp);
}
System.out.print("result=");
for (int i=0, j=0; i<result.length; i++) {
if (result[i] > 0) { //为了去除前面多余的0
j++;
}
if (j == 0) continue;
System.out.print(result[i]); //从不为0的数开始输出
}
System.out.println();

} catch (Throwable e) {
e.printStackTrace();
}
}

public static int[] multiply(int[] a, int[] b) {//数组乘法
int[] result = new int[a.length+b.length]; //相乘结果的最大位数不超过乘数的长度+被乘数的长度
for (int i=a.length-1, k=result.length-1; i>=0; i--, k--) {
for (int j=b.length-1; j>=0; j--) {
result[k] += a[i] * b[j] % 10;
result[k-1] += a[i] * b[j] / 10;
}
}
for (int k=result.length-1; k>0; k--) { //进位整理
if (result[k] > 9) {
result[k-1] += result[k] / 10;
result[k] %= 10;
}
}

return result;
}
}
淡青の月 2019-08-27
  • 打赏
  • 举报
回复
public static void main(String[] args) {

int[] arr = factorial(100);

print(arr);
}

public static void print(int[] arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
}

public static int[] factorial(int num) {
int[] result = { 1 };
if (num < 0) {
throw new RuntimeException();
}
if (num == 0) {
return result;
}
for (int i = 0; i < num; i++) {
result = multiplyPlus(result, i + 1);
}
return result;
}

public static int[] multiplyPlus(int[] arr, int num) {
int[] a = parseArray(num);
int[] result = new int[arr.length + a.length];
for (int i = a.length - 1; i >= 0; i--) {
int carry = 0;
for (int j = arr.length - 1; j >= 0; j--) {
int product = arr[j] * a[i] + carry;
int theUnit = product % 10;
result[i + j + 1] += theUnit;
carry = product / 10;
}
result[0] = carry;
}
int index = 0;
for (int i = 0; i < result.length; i++) {
if (result[i] != 0) {
break;
}
index++;
}
int[] array = new int[result.length - index];
System.arraycopy(result, index, array, 0, array.length);
return array;
}

public static int[] parseArray(int num) {
int count = 0;
int copy = num;
while (copy > 0) {
count++;
copy /= 10;
}
int[] result = new int[count];
for (int i = count - 1; i >= 0; i--) {
result[i] = num % 10;
num /= 10;
}
return result;
}
zero dragon 2019-08-27
  • 打赏
  • 举报
回复
这个算不出是因为java的数据类型不够大
阶乘值太大了,大于long的最大值(9223372036854775807)
maradona1984 2019-08-27
  • 打赏
  • 举报
回复
面试的话一般不会考察api调用,那就模拟自己手算乘法,一位一位进位
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
引用 6 楼 weixin_40290083的回复:
以前写过一个大数的阶乘,如果使用语言所定义的数据类型,会存在溢出问题,就使用数组或者集合,每个元素代表一位数。这样就需要编写代码模拟手工计算乘法的方法,然后再循环调用该方法计算阶乘。
可以研究下下下下下下下
饺子里来点醋 2019-08-27
  • 打赏
  • 举报
回复
引用 4 楼 淡青の月的回复:
BigInteger sum = BigInteger.ONE;
BigInteger[] arr = new BigInteger[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = new BigInteger(i+1+"");
sum = sum.multiply(arr[i]);
}
System.out.println(sum);
求这个没啥用啊
面试被问到的(还得10个子)
Swear_ls 2019-08-26
  • 打赏
  • 举报
回复
int a=1; for(int i=0;i<arr.length;i++){ a=a*arr[i]; }
饺子里来点醋 2019-08-26
  • 打赏
  • 举报
回复
引用 楼主 qq_39285019的回复:
1-100累乘,用数组如何解决?
求~~~~~~~~~~~大神

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧