【问题标题】:Multiplying big numbers (arrays) without Java API在没有 Java API 的情况下乘以大数(数组)
【发布时间】:2016-12-24 16:19:46
【问题描述】:

编程的人们,你好,

数字向后存储在尽可能小的整数数组中:

int[] numberA = {7, 3, 6, 2, 1}; //representing number 12,637
int[] numberB = {7, 3, 3};       //representing number 337

现在我想编写一个函数来返回这两个数字的乘积:

static int[ ] times(int[ ]a, int[ ] b) {
    //numberA x numberB = product
    return product;
}

因为 12,637 x 337 = 4,258,669,所以返回的数组应该如下所示:

product = {9, 6, 6, 8, 5, 2, 4};

所有这些都应该在不使用 Java API 的帮助下工作(例如java.util.ArrayList)。 我已经在同一个脚本中创建了可以使用的类似函数:

  • 添加两个数字(数组)(int[] add(int[] int[]))
  • 乘以一位整数 (int[] timesInt(int[], int))
  • 复制一个数组 (int[] copy(int[]))
  • 从 int (int[] fromInt(int)) 创建数字(数组)


谢谢

【问题讨论】:

  • 到目前为止,您尝试过什么(代码方面,您自己)?这读起来像家庭作业
  • 您可以尝试提取int 值,即。 12637 使用循环从数组中获取。这是你想要的吗?
  • @Rogue 实际上我试图修改“add”函数以适应乘法,但我未能生成具有动态长度的数组。我还尝试通过滑入零来总结最终乘积来模仿用笔/纸(如小学)乘以数字的步骤
  • @TheRedRabbit - 假设字符串中没有尾随零,则乘积的长度将为 A.length + B.length - 1。在乘法运算期间将零插入字符串前面的方法并且使用您的添加功能应该可以工作。在查看 Karatsuba 之类的优化之前,您可能应该先完成这项工作。

标签: java arrays


【解决方案1】:

你知道 Karatsuba 或 Cook 算法吗?也许你可以实现它们? https://en.wikipedia.org/wiki/Karatsuba_algorithmhttps://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication

【讨论】:

  • 虽然链接可能会导致解决方案,但不如写答案好,因为链接可能会损坏。
  • 这些是众所周知的算法,所以如果链接被破坏,你可以在其他地方轻松找到它们。
  • 在这种情况下,你是对的。但是,通常情况并非如此。
  • @kleineLotti Vielen Dank!我没有听说过这样的算法,会通读文章
  • gern,玩得开心,他们真的很基础而且众所周知!
【解决方案2】:

我有这个程序可以创建你在问题中发布的结果,我没有使用任何 java API,如你指定的 arraylist,代码是:

public class Test
{

static int[ ] times(int[ ]a, int[ ] b) {

    String astr="",bstr="";

  for(int i=a.length-1;i>=0;i--)
  {
      astr=astr+a[i];//Converts the int array's to string to reverse them without any other than String methods.
  }

  for(int i=b.length-1;i>=0;i--)
  {
      bstr=bstr+b[i]; //Converts the int array's to string to reverse them without any other than String methods.
  }

  int var1=Integer.parseInt(astr); // Converting the reversed string back to int for computing result

  int var2=Integer.parseInt(bstr); // Converting the reversed string back to int for computing result

  long result=var1*var2; //Computing result


   String newstr=String.valueOf(result); // Converting the final result back to String


   int temp[]=new int[newstr.length()];// Creating the array to hold final result in it

    int counter=0;

    for (int i =newstr.length()-1;i>=0;i--) {

        String nwresult="";
        nwresult=nwresult+newstr.charAt(i);  //reversing the final answer string

         temp[counter++]= Integer.parseInt(nwresult); //Returning the final answer string by converting it to integer

    }

     return temp;

}


 public static void main(String args[])
 {
     int[] numberA = {7, 3, 6, 2, 1}; 
     int[] numberB = {7, 3, 3};  

     int array[]=times(numberA,numberB);

     for(int i=0;i<array.length;i++)
     {
         System.out.print(array[i]);
     }

     System.out.println("");

  }

}

【讨论】:

  • 我认为这里的目标是在不转换为整数的情况下将字符串相乘。
猜你喜欢
  • 2020-02-27
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
相关资源
最近更新 更多