求一到一百相乘java代码
时间: 2023-07-03 08:30:24 浏览: 126
这里是一到一百相乘的Java代码:
```java
public class MultiplicationTable {
public static void main(String[] args) {
int result = 1;
for (int i = 1; i <= 100; i++) {
result *= i;
}
System.out.println("1到100相乘的结果是:" + result);
}
}
```
这段代码使用了一个for循环来计算1到100的乘积,最后输出结果。
相关问题
矩阵相乘Java代码
在Java中,矩阵相乘通常涉及到两个二维数组的操作,可以使用嵌套循环来实现。这里是一个简单的示例,展示了如何通过两层循环计算两个方阵的乘积:
```java
public class MatrixMultiplication {
public static int[][] multiply(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int cols2 = matrix2[0].length;
// 检查矩阵是否可以相乘
if (cols1 != rows2) {
throw new IllegalArgumentException("Matrix dimensions do not allow multiplication");
}
// 创建一个新的矩阵用于存放结果
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) { // 第三层循环遍历第一个矩阵的列
result[i][j] += matrix1[i][k] * matrix2[k][j]; // 计算元素值
}
}
}
return result;
}
public static void main(String[] args) {
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{5, 6}, {7, 8}};
try {
int[][] product = multiply(matrix1, matrix2);
for (int[] row : product) {
System.out.println(Arrays.toString(row));
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
```
在这个例子中,`multiply`函数接收两个二维数组作为输入,然后返回它们的乘积。如果你想要测试这个代码,记得检查两个矩阵的维度是否适合相乘。
MapReduce实现矩阵相乘java代码
以下是使用MapReduce实现矩阵相乘的Java代码:
Mapper类:
```
public class MatrixMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text keyOut = new Text();
private Text valueOut = new Text();
private int k;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
Configuration configuration = context.getConfiguration();
k = configuration.getInt("k", 0);
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] values = value.toString().split(",");
if (values.length != 3) {
throw new IOException("Invalid input format");
}
int i = Integer.parseInt(values[0]);
int j = Integer.parseInt(values[1]);
int v = Integer.parseInt(values[2]);
if (i < 0 || i >= k || j < 0 || j >= k) {
throw new IOException("Invalid input format");
}
keyOut.set(String.valueOf(j));
valueOut.set("A," + i + "," + v);
context.write(keyOut, valueOut);
keyOut.set(String.valueOf(i));
valueOut.set("B," + j + "," + v);
context.write(keyOut, valueOut);
}
}
```
Reducer类:
```
public class MatrixReducer extends Reducer<Text, Text, Text, IntWritable> {
private IntWritable valueOut = new IntWritable();
private Map<Integer, Integer> aMap = new HashMap<>();
private Map<Integer, Integer> bMap = new HashMap<>();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for (Text value : values) {
String[] fields = value.toString().split(",");
if (fields.length != 3) {
throw new IOException("Invalid input format");
}
String matrixName = fields[0];
int i = Integer.parseInt(fields[1]);
int v = Integer.parseInt(fields[2]);
if ("A".equals(matrixName)) {
aMap.put(i, v);
} else if ("B".equals(matrixName)) {
bMap.put(i, v);
} else {
throw new IOException("Invalid input format");
}
}
int sum = 0;
for (Map.Entry<Integer, Integer> entry : aMap.entrySet()) {
int i = entry.getKey();
int a = entry.getValue();
Integer b = bMap.get(i);
if (b != null) {
sum += a * b;
}
}
valueOut.set(sum);
context.write(key, valueOut);
aMap.clear();
bMap.clear();
}
}
```
Driver类:
```
public class MatrixMultiplication {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err.println("Usage: MatrixMultiplication <inputPath> <outputPath> <k>");
System.exit(1);
}
Configuration configuration = new Configuration();
configuration.setInt("k", Integer.parseInt(args[2]));
Job job = Job.getInstance(configuration, "Matrix Multiplication");
job.setJarByClass(MatrixMultiplication.class);
job.setMapperClass(MatrixMapper.class);
job.setReducerClass(MatrixReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
这里假设输入文件的格式为每行三个用逗号分隔的数字,分别表示矩阵A或矩阵B的行、列和值。例如,输入文件中的一行可能是`0,0,1`,表示A矩阵的第0行第0列的值为1。
阅读全文