算法林林总总,此处仅供参考
第一题:(rh)
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static int stringCount(String str) {
String[] array = str.split(",");
int lengthArray=Integer.parseInt(array[0]);
HashMap strList = new HashMap();
String regex="([A-Z]|[a-z]|[0-9]|-|_){0,}";
for (int x = 0; x < lengthArray; x++) {
if (!strList.containsKey(array[1].charAt(x))) {
if((String.valueOf(array[1].charAt(x))).matches(regex)){
strList.put(array[1].charAt(x), 1);
}
} else {
int count = strList.get(array[1].charAt(x)) + 1;
strList.put(array[1].charAt(x), count);
}
}
return strList.size();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println(stringCount(s));
}
}
第二题:(Felix)
import java.util.Scanner;
public class Main {
//思路:逐个匹配log2^N判断是否为整数即可
public static int countLog2N(String str) {
String[] list = str.split(",");
int count = 0;
for (int i = 0; i < list.length; i++) {
int temp = new Integer(list[i]);
if (((Math.log(temp) / Math.log(2)) % 1 == 0))
count++;
}
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
System.out.println(countLog2N(s));
}
}
第三题:(Felix)
import java.util.Scanner;
public class Main {
// 思路:借鉴了某牛的C版本:数组循环主次:1、剩一个就跳出;2、排队到3就拉出去枪毙;3、重置flag确保不超过3
public static int getLastMan(int n) {
int i;
int[] a = new int[n];
i = 0;
int out = 0;
int num = 0;
while (out != n - 1) {
if (a[i] == 0) {
num++;
}
if (num == 3) {
a[i] = 1;
num = 0;
out++;
}
i++;
if (i == n) {
i = 0;
}
}
for (i = 0; i < n; i++) {
if (a[i] == 0) {
return i + 1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int s = input.nextInt();
System.out.println(getLastMan(s));
}
}