超大数相乘的java代码,java容易实现大数相乘

java简单实现大数相乘

面试的一道题 ,让我实现大数相乘的算法,晕倒。

package test;

import java.math.BigInteger;

public class BigNumberHandler {

public int[] a = new int[100];

public int[] b = new int[100];

public BigNumberHandler() {

initData(a);

initData(b);

print(a);

print(b);

}

private void initData(int[] t) {

for (int i = 0; i < t.length; i++) {

t[i] = (int) (Math.random() * 10);

}

}

private void print(int[] t) {

for (int i = 0; i < t.length; i++) {

System.out.print(t[t.length - i - 1]);

}

System.out.println("");

}

public void process() {

int[] result = new int[a.length + b.length];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < b.length; j++) {

int number = b[j] * a[i];

int index = i + j;

result[index] += number;

}

}

for (int i = 0; i < result.length - 1; i++) {

int t = result[i];

if (t > 9) {

result[i + 1] += t / 10;

result[i] = t - t / 10 * 10;

}

}

print(result);

}

public static String getString(int[] t) {

StringBuffer sBuffer = new StringBuffer();

for (int i = 0; i < t.length; i++) {

sBuffer.append(t[t.length - i - 1]);

}

return sBuffer.toString();

}

public static void main(String[] args) {

BigNumberHandler t = new BigNumberHandler();

t.process();

BigInteger a = new BigInteger(getString(t.a));

BigInteger b = new BigInteger(getString(t.b));

System.out.println(a);

System.out.println(b);

System.out.println(a.multiply(b));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 MapReduce 实现矩阵相乘Java 代码: Map 阶段: ```java public static class Map extends Mapper<LongWritable, Text, Text, Text> { private Text outputKey = new Text(); private Text outputValue = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); int m = Integer.parseInt(conf.get("m")); int p = Integer.parseInt(conf.get("p")); String line = value.toString(); String[] tokens = line.split(","); if (tokens[0].equals("A")) { for (int k = 0; k < p; k++) { outputKey.set(tokens[1] + "," + k); outputValue.set("A," + tokens[2] + "," + tokens[3]); context.write(outputKey, outputValue); } } else { for (int i = 0; i < m; i++) { outputKey.set(i + "," + tokens[2]); outputValue.set("B," + tokens[1] + "," + tokens[3]); context.write(outputKey, outputValue); } } } } ``` Reduce 阶段: ```java public static class Reduce extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String[] value; HashMap<Integer, Float> hashA = new HashMap<Integer, Float>(); HashMap<Integer, Float> hashB = new HashMap<Integer, Float>(); for (Text val : values) { value = val.toString().split(","); if (value[0].equals("A")) { hashA.put(Integer.parseInt(value[1]), Float.parseFloat(value[2])); } else { hashB.put(Integer.parseInt(value[1]), Float.parseFloat(value[2])); } } int n = Integer.parseInt(context.getConfiguration().get("n")); float result = 0.0f; float a_ij, b_jk; for (int j = 0; j < n; j++) { a_ij = hashA.containsKey(j) ? hashA.get(j) : 0.0f; b_jk = hashB.containsKey(j) ? hashB.get(j) : 0.0f; result += a_ij * b_jk; } context.write(null, new Text(key.toString() + "," + Float.toString(result))); } } ``` 驱动程序: ```java public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); int m = 2; int n = 2; int p = 2; conf.set("m", String.valueOf(m)); conf.set("n", String.valueOf(n)); conf.set("p", String.valueOf(p)); Job job = new Job(conf, "MatrixMultiply"); job.setJarByClass(MatrixMultiply.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } ``` 输入数据格式为: ``` A,0,0,1 A,0,1,2 B,0,0,3 B,1,0,4 ``` 输出数据格式为: ``` null 0,0,11.0 null 0,1,16.0 null 1,0,23.0 null 1,1,34.0 ``` 其中第一列为 A 矩阵的行号,第二列为 B 矩阵的列号,第三列为两个矩阵相乘的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值