我遇到数字倍增的问题。
从我的输入中我得到:
示例:如果我从输入3,2
获得,则表示我需要将所有两个位数乘以三次
现在我得到了一些代码,只有当我得到两个带两位数的数字时才有效。
如何实现一个与用户需要的数量相乘的方法?我只做了这种打印输入数字的方法。我可以处理多次乘法的方法,因为变量乘数表示......
public static void test(int digits, int multiplier) {
int number = 0;
int result = 0;
ArrayList<String> numbers = new ArrayList<String>();
number = (int) Math.pow(10, digits);
for (int i = (int) (Math.pow(10, digits - 1)); i < number; i++) {
System.out.println(i);
for (int j = (int) (Math.pow(10, digits - 1)); j < number; j++) {
result = j * i;
}
}
System.out.println(number);
}
示例输入和输出
如果位数= 2,次数= 2
我们需要将所有2位数字10, 11, 12 .... 99
相乘两次。
10 * 10
10 * 11
.
.
10 * 99
.
.
.
99 * 98
99 * 98
同样地,如果数字的数字是2,则没有数字是3
10 * 10 * 10
10 * 10 * 11
.
.
10 * 99 * 99
.
.
.
99 * 99 * 98
99 * 99 * 99
答案 0 :(得分:1)
我不会使用递归,而是将for循环与 Java8流API 结合使用。
代码示例:
import java.util.stream.IntStream;
public class Multiplier {
private final int digits;
public Multiplier(int digits) {
this.digits = digits;
}
/**
* @param multiplificationSteps
* @return a stream, containing all numbers of the given digits, each multiplied several times with each other
*
* digits=1 with multiplificationSteps=1 => output [1;9]
* digits=2 with multiplificationSteps=1 => output [10;99]
* digits=1 with multiplificationSteps=2 => output [1;9] & [2;19] & ... & [9;81]
*/
public IntStream createFullStream(int multiplificationSteps) {
IntStream intStream = makeIntStream();
for (int i = 1; i < multiplificationSteps; i++)
intStream = intStream.flatMap(this::multiplyStream);
return intStream;
}
/**
* @param input a multiplier
* @return the stream, having each number multiplied by the multiplier
*
* input [1;9] and 1 => output [1;9]
* input [1;9] and 2 => output [2;18]
* input [10;99] and 3 => output [30;297]
*/
private IntStream multiplyStream(int input) {
return makeIntStream().map(k -> input * k);
}
/**
* @return a stream of all numbers with the given number of digits
*
* input 1 => output [1;9]
* input 2 => output [10;99]
* input 3 => output [100;999]
*/
private IntStream makeIntStream() {
int startNumber = (int) Math.pow(10, digits - 1);
int endNumber = (int) Math.pow(10, digits);
return IntStream.range(startNumber, endNumber);
}
public static void main(String[] args) {
new Multiplier(1).createFullStream(2).forEach(System.out::println);
}
}
示例输入和输出
对于输入1,1
,它输出:
1
2
3
4
5
6
7
8
9
对于1,2
,它输出:
1
2
3
4
5
6
7
8
9
2
4
6
8
10
12
14
16
18
3
6
9
12
15
18
21
24
27
4
8
12
16
20
24
28
32
36
5
10
15
20
25
30
35
40
45
6
12
18
24
30
36
42
48
54
7
14
21
28
35
42
49
56
63
8
16
24
32
40
48
56
64
72
9
18
27
36
45
54
63
72
81
它也可以没有Java8
import java.util.*;
public class PlainOldMultiplier {
private final int digits;
public PlainOldMultiplier(int digits) {
this.digits = digits;
}
public void printAllMultipliedNumbers(int multiplicationSteps) {
// define the result
Collection<Integer> numbers = new ArrayList<>();
numbers.add(1);// multiply by 1 in first iteration
// foreach multiplicationStep once multiply all existing stuff with [start;end[
for (int multiplicationStepCount = 0; multiplicationStepCount < multiplicationSteps; multiplicationStepCount++) {
numbers = multiplyEverything(numbers);
}
// output numbers to console
for (Integer number : numbers) {
System.out.println(number);
}
}
private Collection<Integer> multiplyEverything(Collection<Integer> numbersOfPreviousIteration) {
Collection<Integer> result = new ArrayList<>();
// define start and end by taking account "digits"
int start = (int) Math.pow(10, digits-1);
int end = (int) Math.pow(10, digits);
// foreach number within [start;end[ multiply existing stuff
for (int number = start; number < end; number++) {
for (Integer numberOfPreviousIteration : numbersOfPreviousIteration) {
result.add(numberOfPreviousIteration * number);
}
}
return result;
}
public static void main(String[] args) {
new PlainOldMultiplier(1).printAllMultipliedNumbers(1); // outputs [1;9]
new PlainOldMultiplier(2).printAllMultipliedNumbers(1); // outputs [10;99]
new PlainOldMultiplier(1).printAllMultipliedNumbers(2); // outputs [1;9] & [2;18] & ... & [9;81]
}
}