1.java代码实现将100分拆成四个整数之和,一共有多少种拆法?
java代码实现将100分拆成四个整数之和,一共有多少种拆法?
如果没有负整数,一共有 种拆法。mysqljson源码
源码:
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public final static int Sum = ;
static Set<String> Combinations = new TreeSet<>();
public static void main(String[] args) throws IOException {
for (int i1 = 0; i1 <= Sum; i1++) {
for (int i2 = 0; i2 <= Sum; i2++) {
for (int i3 = 0; i3 <= Sum; i3++) {
for (int i4 = 0; i4 <= Sum; i4++) {
String s = GetString(new int[] { i1,云管家婆 源码 i2, i3, i4});
if (CheckNumbers(i1, i2, i3, i4)) {
Combinations.add(s);
}
}
}
}
}
System.out.println(Combinations);
System.out.println(Combinations.size());
FileWriter writer = new FileWriter("results.json");
writer.write(Combinations.toString());
writer.flush();
writer.close();
}
public static String GetString(int[] numbers) {
sort(numbers, 0, 3);
return Arrays.toString(numbers);
}
public static void sort(int[] arr,int begin,int end) {
int a = begin;
int b = end;
if (a >= b) return;
int x = arr[a];
while (a < b) {
while (a < b && arr[b] >= x) b--;
if (a < b) {
arr[a] = arr[b];
a++;
}
while (a < b && arr[a] <= x) a++;
if (a < b) {
arr[b] = arr[a];
b--;
}
}
arr[a] = x;
sort(arr,begin,a-1);
sort(arr,a+1,end);
}
public static boolean CheckNumbers(int i1, int i2, int i3, int i4) {
return (i1 + i2 + i3 + i4) == Sum;
}
}