[java]代码库
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
/*
* 5个问题
问题1:使用for循环、while循环和递归写出3个函数来计算给定数列的总和。
问题2:编写一个交错合并列表元素的函数。例如:给定的两个列表为[a,B,C]和[1,2,3],函数返回[a,1,B,2,C,3]。
问题3:编写一个计算前100位斐波那契数的函数。根据定义,斐波那契序列的前两位数字是0和1,随后的每个数字是前两个数字的和。例如,前10位斐波那契数为:0,1,1,2,3,5,8,13,21,34。
问题4:编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。
问题5:编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
例如:1 + 2 + 34–5 + 67–8 + 9 = 100。
*
*/
public class One2Five {
public void one() {
int[] arr = { 1, 2, 4, 3, 0, 1, 2, 3 };
int sum = 0;
for (int i : arr) {
sum += i;
}
System.out.println("sum=" + sum);
sum = 0;
int i = 0;
while (i < arr.length) {
sum += arr[i++];
}
System.out.println("sum=" + sum);
sum = 0;
sum = sum(arr, 0);
System.out.println("sum=" + sum);
}
private int sum(int[] arr, int i) {
if (i == arr.length)
return 0;
return arr[i] + sum(arr, i + 1);
}
public void two() {
int[] b = { 0, 2, 4, 6, 8 };
int[] a = { 1, 3, 5, 7, 9 };
int[] c = new int[a.length + b.length];
int bx = 0;
int ax = 0;
boolean f = true;
boolean bof = false;
boolean aof = false;
for (int i = 0; i < c.length; i++) {
if (f && !bof) {
if (bx < b.length) {
c[i] = b[bx++];
} else
bof = true;
f = false;
} else if (!aof) {
if (ax < a.length) {
c[i] = a[ax++];
} else
aof = true;
f = true;
}
}
for (int i : c) {
System.out.print(i + ",");
}
}
public long fboi(int n) {
if (n == 1)
return 0;
if (n == 2)
return 1;
long r = fboi(n - 1) + fboi(n - 2);
return r;
}
public void three() {
long all = fboi(10);
System.out.println(all);
}
public static Stream<Integer> range(final Integer start, Integer length, Integer step, List<Integer> except) {
Supplier<Integer> seed = new Supplier<Integer>() {
private Integer next = start;
@Override
public Integer get() {
Integer _next = next;
next += step;
return _next;
}
};
return Stream.generate(seed).limit(length).filter(new Predicate<Integer>() {
@Override
public boolean test(Integer t) {
if (except == null)
return true;
if (except.contains(t))
return false;
return true;
}
});
}
List<Integer> list = new ArrayList<Integer>();
AtomicInteger count = new AtomicInteger(0);
List<List<Integer>> WholeArrangementList = new ArrayList<>();
public void WholeArrangement(int N) {
Stream<Integer> range = range(0, N, 1, list);
range.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
list.add(t);
WholeArrangement(N);
list.remove(list.size() - 1);
}
});
if (list.size() == N) {
count.incrementAndGet();
List<Integer> arrangement = new ArrayList<Integer>(list);
WholeArrangementList.add(arrangement);
System.out.println(Arrays.toString(list.toArray()));
}
}
public void four() {
Integer[] arr = { 89, 12, 43, 23, 1, 8 };
WholeArrangement(arr.length);
Stream<String> sorted = WholeArrangementList.stream().map(new Function<List<Integer>, String>() {
@Override
public String apply(List<Integer> t) {
StringBuffer sf = new StringBuffer();
for (Integer idx : t) {
sf.append(arr[idx]);
}
return sf.toString();
}
}).sorted(Comparator.reverseOrder());
sorted.onClose(new Runnable() {
@Override
public void run() {
Optional<String> findFirst = sorted.findFirst();
System.out.println(findFirst.get());
}
});
Object[] array = sorted.toArray();
Stream.of(array).forEach(new Consumer<Object>() {
@Override
public void accept(Object t) {
System.out.println(t);
}
});
System.out.println("___________________________________________________max");
Optional<Object> findFirst = Stream.of(array).findFirst();
System.out.println(findFirst.get());
}
char[] operator = { ' ', '+', '-' };
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<Character> operatorList = new ArrayList<Character>();
private int joinNumCalc() {
int joinNum = 0;
char preOperate = 'S';// 模拟个开始符
int index = operatorList.size() - 1;
int count = 1;
if (operatorList.size() <= 1)
return 0;
if (operatorList.get(index) == ' ')
return 0;
for (int i = index - 1; i >= 0; i--) {
if (operatorList.get(i) == ' ') {
count++;
} else {
preOperate = operatorList.get(i);
break;
}
}
if (preOperate == 'S') {
count = count - 1;
}
for (int i = index - count; i < index; i++) {
joinNum = joinNum * 10 + nums[i];
}
switch (preOperate) {
case '+':
return joinNum;
case '-':
return -joinNum;
case 'S':
return joinNum;
default:
break;
}
return 0;
}
public void exp_calc(int value) {
if (operatorList.size() == nums.length) {
count.incrementAndGet();
operatorList.add('E');// 加一个结束符
value = value + joinNumCalc();
operatorList.remove(operatorList.size() - 1);
if (value == 100) {
for (int i = 0; i < operatorList.size(); i++) {
Character operate = operatorList.get(i);
switch (operate) {
case '+':
case '-':
if (i == 0 && operate == '+') {
} else {
System.out.print(operate);
}
System.out.print(nums[i]);
break;
case ' ':
System.out.print(nums[i]);
default:
break;
}
}
System.out.println();
// System.out.println("value = " + value + "\t" +
// Arrays.toString(operatorList.toArray(new Character[0])));
}
return;
}
for (int i = 0; i < operator.length; i++) {
if (operatorList.size() == 0 && operator[i] == ' ')
continue;
operatorList.add(operator[i]);
int valueAdd = value + joinNumCalc();
exp_calc(valueAdd);
operatorList.remove(operatorList.size() - 1);
}
}
public void five() {
exp_calc(0);
System.out.println(count);
}
public static void main(String[] args) {
One2Five five = new One2Five();
five.one();
five.two();
five.three();
five.four();
five.five();
}
}