如题。

解决方案 »

  1.   

    要啥大整数:
    直接搞:int a=123456789;
    int b=987654321;
    System.out.println((long)a*b);
      

  2.   

    1.java.math.biginteger
    BigInteger b1 = new BigInteger("123456789");
    BigInteger b2 = new BigInteger("987654321");
    System.out.println(b1.multiply(b2).toString());
    2.利用大数组或string都行,下面给出一个string的实现
    ac poj 1001的片断 public String add(String s1,String s2) throws Exception{
    if(s1 == null) throw new Exception("输入错误");
    if(s2 == null) throw new Exception("输入错误");
    String regex = "[\\d]+$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(s1);
    if(!matcher.find()) throw new Exception("输入错误");
    matcher = pattern.matcher(s2);
    if(!matcher.find()) throw new Exception("输入错误");
    String result1 = "";
    int minLen =0;
    int maxLen = 0;
    String longString = "";
    String shortString = "";
    if(s1.length() > s2.length()) {
    maxLen = s1.length();
    minLen = s2.length();
    longString = s1;
    shortString = s2;
    }else {
    maxLen = s2.length();
    minLen = s1.length();
    longString = s2;
    shortString = s1;
    }
    int carry = 0;
    StringBuffer result = new StringBuffer();
    int tempResult = 0;
    for(int i = maxLen-1,j=minLen-1;j>-1;i--,j--) {
    tempResult = Integer.parseInt(longString.substring(i, i+1)) + Integer.parseInt(shortString.substring(j, j+1)) + carry;
    result.append(tempResult%10);
    carry = (int)(tempResult/10);
    }
    for(int i=maxLen-minLen-1;i>-1;i--) {
    tempResult = Integer.parseInt(longString.substring(i, i+1)) + carry;
    result.append(tempResult%10);
    carry = (int)(tempResult/10);
    }
    if(carry>0) {
    result.append(carry);
    }
    return result.reverse().toString();
    }
    private String multi(String s1,String s2) throws Exception {
    if(s1 == null) throw new Exception("输入错误");
    if(s2 == null) throw new Exception("输入错误");
    if(s2.length()!=1) throw new Exception("输入错误");
    String regex = "[\\d]+$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(s1);
    if(!matcher.find()) throw new Exception("输入错误");
    matcher = pattern.matcher(s2);
    if(!matcher.find()) throw new Exception("输入错误");
    String result1 = "";
    int tempResult = 0;
    int carry = 0;
    StringBuffer result = new StringBuffer();
    int multi = Integer.parseInt(s2);
    for(int i=s1.length()-1;i>-1;i--) {
    tempResult = Integer.parseInt(s1.substring(i, i+1)) * multi + carry;
    result.append(tempResult%10);
    carry = (int)(tempResult/10);
    }
    if(carry>0) {
    result.append(carry);
    }
    return result.reverse().toString();
    }
    public String multiple(String s1,String s2) throws Exception {
    if(s1 == null) throw new Exception("输入错误");
    if(s2 == null) throw new Exception("输入错误");
    String regex = "[\\d]+$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(s1);
    if(!matcher.find()) throw new Exception("输入错误");
    matcher = pattern.matcher(s2);
    if(!matcher.find()) throw new Exception("输入错误");
    String result = "0";
    String tempResult = "";
    StringBuffer tempBuffer;
    for(int i=0;i<s2.length();i++) {
    tempResult = this.multi(s1, s2.substring(i, i+1));
    tempBuffer = new StringBuffer(tempResult);
    for(int j=i;j<s2.length()-1;j++) {
    tempBuffer.append("0");
    }
    result = this.add(tempBuffer.toString(), result);
    }
    return result;
    }
      

  3.   


    import java.math.BigInteger;
    /**
     *
     * @author wubaochuan
     */
    public class Main {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            BigInteger a = new BigInteger("123456789");
            BigInteger b = new BigInteger("987654321");
            System.out.println(a.multiply(b));
            // TODO code application logic here
        }}
      

  4.   

    那究竟有啥要求,  有啥限制,  说清楚啊,  难道不能用long型, 哪个语言都支持这个吧
      

  5.   

    直接整说明你对long的取值范围很敏感。
    他的结果是:121932631112635269
    Long最大值:9223372036854775807
    没错,是可以的。 只有 2G*2G*2 以上的结果才会溢出。要是我出题,又想考算法的话,我就会附加一句(不能使用long数据类型)
    要是我出题,是想考你们对数字范围的概念的话,我就像帖子标题这样讲。拜托,1234567890 还不到2G哩,987654321*987654321都不会溢出哦。我看直接整才是考官想要的吧
      

  6.   

    估计是要这样的吧  求的是 (123456789123456*98765432134567899)class big
    {  
     public static void main(String[] args)
     {
       int[] n1={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};
       int[] n2={9,8,7,6,5,4,3,2,1,3,4,5,6,7,8,9,9};     
       int[] result=multiply(n1,n2);
       for(int i = 0; i < result.length; i++)
       {
          System.out.print(result[i]);
       }
      }
      private static int[] multiply(int[] numbers1, int[] numbers2) 
      {   
            int[] result = new int[numbers1.length + numbers2.length];   
            for (int i = 0; i < numbers1.length; i++) {   
                for (int j = 0; j < numbers2.length; j++) {   
                    int tenth = i + j;   
                    int cellResult = numbers1[i] * numbers2[j];   
                    put(result, tenth + 1, cellResult % 10);   
                    put(result, tenth, cellResult / 10);   
                }   
            }   
            return result;   
        } 
         private static void put(int[] result, int index, int number) {   
            result[index] += number;   
            carryFrom(result, index);   
        }   
        private static void carryFrom(int[] result, int index) {   
            if (index < 0) {   
                return;   
            }   
        
            if (result[index] >= 10) {   
                result[index - 1]++;   
                result[index] = (int)(result[index] - 10);   
                carryFrom(result, index - 1);   
            }   
        }   }
      

  7.   

    数组。。用String不行么。还算法这题目出的真纠结。。
      

  8.   

    那估计没啥好选的了,就是考大数相乘的算法了。 输入是 string*string,输出还是string,里面用byte[], 算法还是标准乘法。
      

  9.   

    哈哈哈,被他欺骗啦,int很牛的,别小看它啦!!!
      

  10.   


    //用String好了
        public static String cm(String o, String t) {
            StringBuffer o1 = new StringBuffer(o).reverse();
            StringBuffer t1 = new StringBuffer(t).reverse();
            StringBuilder jg = new StringBuilder("0");
            int sum = 0;//当前位
            for (int i = 0, j; i < o1.length(); i++) {
                for (j = 0; j < t1.length(); j++) {
                    sum += (o1.charAt(i) - '0') * (t1.charAt(j) - '0');
                    if (jg.length() - 1 < i + j) {
                        jg.append('0');
                    }
                    sum += jg.charAt(i + j) - '0';
                    jg.replace(i + j, i + j + 1, "" + (char) (sum % 10 + '0'));
                    sum /= 10;
                }
                while (sum > 0) {
                    if (jg.length() - 1 < i + j) {
                        jg.append('0');
                    }
                    sum += jg.charAt(i + j) - '0';
                    jg.replace(i + j, i + j + 1, "" + (char) (sum % 10 + '0'));
                    sum /= 10;
                    j++;
                }
            }
            return jg.reverse().toString();
        }