第一题
匹配ip地址,如果是ipv4地址则输出IPv4,如果是ipv6地址则输出IPv6,否则输出Neither
要求ipv4地址格式 172.94.52.7
以下格式不正确:
172.2.2
127.0.0.01
127.0…5
ipv6格式
aaaa:0:Fa21:0:8888:2234:7777:0123
不对大小写做限制,不对0开头做限制
样例:
172.94.52.7
IPv4
aaaa:0:Fa21:0:8888:2234:7777:0123
IPv6
172.94.52.07
Neither
/**Ac代码*/
public class Main {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
String ip = sc.next();
Pattern ip4Reg = Pattern.compile(
"^([0-9]|[1-9][0-9]|1[0-9]{2}|25[0-5]|2[0-4][0-9])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|25[0-5]|2[0-4][0-9])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|25[0-5]|2[0-4][0-9])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|25[0-5]|2[0-4][0-9])$");
Matcher matcher = ip4Reg.matcher(ip);
Pattern ip6Reg = Pattern.compile(
"^([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0):([0-9a-zA-Z]{4}|0)$");
Matcher matcher2 = ip6Reg.matcher(ip);
if (matcher.find()) {
// System.out.println(matcher.group(4));
System.out.println("IPv4");
} else if (matcher2.find()) {
System.out.println("IPv6");
} else {
System.out.println("Neither");
}
}
}
}
第二题
模拟手机9键
1 [] 2 abc 3 def
4 ghi 5 jkl 6 mno
7 pqrs 8 tuv 9 wxyz
按键后按字典序输出对应字符组合
如
23
[ad, ae, af, bd, be, bf, cd, ce, cf]
93
[wd, we, wf, xd, xe, xf, yd, ye, yf, zd, ze, zf]
223
[aad, aae, aaf, abd, abe, abf, acd, ace, acf, bad, bae, baf, bbd, bbe, bbf, bcd, bce, bcf, cad, cae, caf, cbd, cbe, cbf, ccd, cce, ccf]
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**Ac代码*/
public class Main {
private static Map map = new HashMap<>();
static {
map.put(1, "");
map.put(2, "abc");
map.put(3, "def");
map.put(4, "ghi");
map.put(5, "jkl");
map.put(6, "mno");
map.put(7, "pqrs");
map.put(8, "tuv");
map.put(9, "wxyz");
}
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
String nums = sc.next();
int len = nums.length();
List candidates = new ArrayList<>();
for (char num : nums.toCharArray()) {
String chars = map.get(Integer.valueOf(num + ""));
candidates.add(chars);
}
// Collections.sort(candidates);
List retList = new ArrayList<>();
process(candidates, 0, len, "", retList);
System.out.print("[");
for (int i = 0, length = retList.size(); i < length; ++i) {
System.out.print(retList.get(i));
if (i < length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
}
private static void process(List candidates, int i, int len, String comp, List retList) {
if (comp.length() == len) {
retList.add(comp);
// System.out.println(comp);
return;
}
if (i == candidates.size()) {
return;
}
String candidate = candidates.get(i);
for (String c : candidate.split("")) {
String newComp = comp + c;
process(candidates, i + 1, len, newComp, retList);
}
process(candidates, i + 1, len, comp, retList);
}
}
第三题
输入n个数字,将其分为两组, 求最小|s1-s2|,其中s1,s2分别为两组数据之和
例如:
5
4 3 8 5 10
0
4
1 1 1 997
996
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**Ac代码*/
public class Main {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
int n = sc.nextInt();
if (n == 0) {
System.out.println(0);
return;
}
List nums = new ArrayList<>();
int sum = 0;
while (n-- > 0) {
int num = sc.nextInt();
nums.add(num);
sum += num;
}
int half = sum / 2;
Collections.sort(nums);
Collections.reverse(nums);
int current = 0;
int index = 0;
int len = nums.size();
while (current < half && index < len) {
Integer num = nums.get(index);
if (current + num <= half) {
current += num;
}
index++;
}
System.out.println(Math.abs(current - (sum - current)));
}
}
}
第四题
求输入一组数据最长等差数列的长度,(数字可以自行任意排列,最长即可)
例如
5
2 3 4 5 1
输出
5
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**Ac代码*/
public class Main {
private static int maxLen = 2;
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
int n = sc.nextInt();
List nums = new ArrayList<>();
Set numSet = new HashSet<>();
while (n-- > 0) {
int num = sc.nextInt();
nums.add(num);
numSet.add(num);
}
Collections.sort(nums);
int length = nums.size();
if (length == 0) {
System.out.println(0);
} else if (length == 1) {
System.out.println(1);
} else {
int lastCha = nums.get(1) - nums.get(0);
int curLen = 2;
int lastNum = nums.get(1);
List> list = new ArrayList<>();
List lastList = new ArrayList<>();
lastList.add(nums.get(0));
lastList.add(nums.get(1));
list.add(lastList);
for (int i = 2; i < length; ++i) {
int num = nums.get(i);
int cha = num - lastNum;
if (cha == lastCha) {
curLen++;
lastList.add(num);
} else {
lastList = new ArrayList<>();
list.add(lastList);
lastList.add(lastNum);
lastList.add(num);
curLen = 2;
}
lastCha = cha;
lastNum = num;
if (curLen > maxLen) {
maxLen = curLen;
}
}
for (List l : list) {
Integer num = l.get(0);
Integer num1 = l.get(1);
int cha = num1 - num;
if (cha == 0) {
int current = 0;
for (int o : nums) {
if (num == o) {
current++;
}
}
} else {
int current = 2;
while (numSet.contains(num1 + cha)) {
num1 += cha;
current++;
}
while (numSet.contains(num - cha)) {
num -= cha;
current++;
}
if (current > maxLen) {
maxLen = current;
}
}
}
System.out.println(maxLen);
}
}
}
}