一、相邻数之和
public static int test1(int a){
List<String> ss = new ArrayList<>();
String result = "";
int num = 1;
System.out.println(a + "=" + a);
for (int i = 1;i < a;i++){
String tmp = "" + a + "=" + i;
int sum = i;
for (int j = i + 1;sum < a;j++){
sum += j;
tmp = tmp + "+" + j;
if (sum == a){
num++;
ss.add(tmp);
}
}
}
ss.sort(Comparator.comparingInt(String::length));
ss.forEach(System.out::println);
return num;
}
二、猴子台阶问题
private static void test2(int n) {
int dp[] = new int[n+1];
dp[1]=1;
dp[2]=1;
dp[3]=2;
for (int i = 4; i <= n; i++) {
dp[i]=dp[i-1]+dp[i-3];
}
System.out.println(dp[n]);
}
private static long test21(int n) {
if (n == 1 || n == 2){
return 1;
}else if (n == 3){
return 2;
}else {
return test21(n-1) + test21(n-3);
}
}
三、骰(tou)子问题
static int[] test3(int[] a,char b){
int temp = 0;
switch (b){
case 'L': temp = a[1];
a[1] = a[5];
a[5] = a[2];
a[2] = a[6];
a[6] = temp;
break;
case 'R': temp = a[1];
a[1] = a[6];
a[6] = a[2];
a[2] = a[5];
a[5] = temp;
break;
case 'F': temp = a[3];
a[3] = a[5];
a[5] = a[4];
a[4] = a[6];
a[6] = temp;
break;
case 'B': temp = a[3];
a[3] = a[6];
a[6] = a[4];
a[4] = a[5];
a[5] = temp;
break;
case 'A': temp = a[1];
a[1] = a[4];
a[4] = a[2];
a[2] = a[3];
a[3] = temp;
break;
case 'C': temp = a[1];
a[1] = a[3];
a[3] = a[2];
a[2] = a[4];
a[4] = temp;
break;
}
return a;
}
四、字符串分割
static void test5(String S,int k){
String[] split = S.split("-");
String result = split[0];
for (int i = 1;i<split.length;i++){
String temp = "";
String s = split[i];
for (int j = 1;s.length() >= k;j++){
String substring = s.substring(0, k);
s = s.substring(k);
int upperNum = 0;
int lowerNum = 0;
for (int i1 = 0; i1 < substring.length(); i1++) {
if((int)substring.charAt(i1)>64 && (int)substring.charAt(i1)<91){
upperNum++;
}
else if((int)substring.charAt(i1)>96 && (int)substring.charAt(i1)<123){
lowerNum++;
}
}
if (upperNum > lowerNum) {
substring = substring.toUpperCase();
} else if (upperNum < lowerNum) {
substring = substring.toLowerCase();
}
temp = temp + "-" + substring;
}
if (s.length() != 0){
temp = temp + "-" + s;
}
result += temp;
}
System.out.println(result);
}
五、分糖果
static void test4(int a){
int count = 0;
for (int i = a; i != 1; i /= 2, count++) {
if (i == 3) {
System.out.println(count += 2);
return;
}
if (i % 2 != 0) {
if ((i + 1) / 2 % 2 == 0){
i++;
}
else {
i--;
}
count++;
}
}
}