参考:
题目简介:
第一题:求获胜者,如果票数相当,按照字母排序,a>b>c,A>B>C,如果字母相同,则字母少的在前面,比如Luc>Lucy
输入:
Tom,Lily,Tom,Lucy,Lucy,Jack
输出:
Lucy
第二题:字符串匹配
输入:
read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
输出:
0x17 0xff 0x7
0xf0 0xff 0x80
1. 第一题代码:
1. 代码一:(通过率62.5%)
检验输入合法性就可以AC了,首字母大写,后边的字母小写。
import java.util.*;
public class Main_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(sc.hasNextLine())
{
String str = sc.nextLine();
String s[] = str.split(",");
Arrays.sort(s);
if(s.length == 1)
{
System.out.println(s[0]);
return;
}
// for(int i = 0; i < s.length; i++)
// {
// System.out.print(s[i] + " ");
// }
// System.out.println();
int count = 1;
int max = 0;
int index = 0;
for(int i = 0; i < s.length - 1; i++)
{
if((s[i]).equals(s[i + 1]))
{
count++;
}
else
{
count = 1;
}
if(count > max)
{
max = count;
index = i;
}
}
// System.out.println(max);
// System.out.println(index);
if(count == 1)
{
System.out.println(s[0]);
return;
}
else
{
System.out.println(s[index]);
}
}
else
{
System.out.println("error.0001");
// System.exit(0);
}
}
}
// 输入
// 输出
// Tom,Lily,Tom,Lucy,Lucy,Jack
// Lucy
// Tom,Lily,Tom,Lucy,Lucy,Tom,Jack
// Tom
// Tom,Lily,Tom,Lucy,Lucy,Jack,Tom,Tomy,Tomy,Tomy
// Tom
// Lily,Lily,Tom,Tom
// Lily
2. 代码二:(AC)
import java.util.*;
public class Main_1_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
if (!str.matches("([A-Z][a-z]*,)*([A-Z][a-z]*)"))
{
System.out.println("error.0001");
}
else
{
String[] strings = str.split(",");
HashMap<String, Integer> map = new HashMap<String, Integer>();
int max = 1;
for (String s : strings) {
map.put(s, map.getOrDefault(s, 0) + 1);
max = Math.max(max, map.get(s));
}
ArrayList<String> arrayList = new ArrayList<String>();
for (String s : map.keySet()) {
if (map.get(s) == max) {
arrayList.add(s);
}
}
// System.out.println(arrayList.toString());
arrayList.sort((s1, s2) -> s1.compareTo(s2));
// System.out.println(arrayList.toString());
System.out.println(arrayList.get(0));
}
sc.close();
}
}
// 输入
// 输出
// Tom,Lily,Tom,Lucy,Lucy,Jack
// Lucy
// Tom,Lily,Tom,Lucy,Lucy,Tom,Jack
// Tom
// Tom,Lily,Tom,Lucy,Lucy,Jack,Tom,Tomy,Tomy,Tomy
// Tom
// Lily,Lily,Tom,Tom
// Lily
3. 代码三:(AC)
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
bool cmp(pair<string, int> a, pair<string, int>b)
{
if(a.second != b.second)
{
return a.second > b.second;
}
else
{
int len = min(a.first.size(), b.first.size());
for(int i = 0; i < len; i++)
{
if(a.first[i] == b.first[i])
{
continue;
}
else
{
return a.first[i] < b.first[i];
}
}
return a.first.size() < b.first.size();
}
}
int main()
{
string s;
cin >> s;
bool legal = true;
string tmp = "";
unordered_map<string, int> cnt;
for(int i = 0;i < s.length(); i++)
{
if(s[i] == ',')
{
cnt[tmp]++;
tmp.clear();
}
else if(s[i] >= 'a' && s[i] <= 'z')
{
if(tmp.size() != 0)
{
tmp += s[i];
}
else
{
legal = false;
break;
}
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
if(tmp.size() == 0)
{
tmp += s[i];
}
else
{
legal = false;
break;
}
}
else
{
legal = false;
break;
}
}
if(legal == false)
{
cout << "error.0001";
}
else
{
cnt[tmp]++;
vector< pair<string, int> > buf(cnt.begin(), cnt.end());
sort(buf.begin(), buf.end(), cmp);
cout << buf[0].first << endl;
}
return 0;
}
2. 第二题代码:
1. 代码一:(较全)
import java.util.*;
// 输入1:
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
// 输出1:
// 0x17 0xff 0x7
// 0xf0 0xff 0x80
// 输入2:
// read read_his[addr=0xff,mask=0xff,val=0x1]
// 输出2:
// FAIL
// 思路:
// 先比对最前面那个字符串,然后是三个属性,顺序不能错,然后是数值,必须0x或者0X开头,然后还得有数,以上全通过才行。
// 切分的时候容易弄混,多输出,弄一步输出一下就行。(需要注意检验数值是否十六进制)
public class Main_2_1 {
public static boolean isHexNumber(String str) // 判断字符串是否为十六进制数字
{
boolean flag = true;
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
{
continue;
}
else
{
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
{
String string = sc.nextLine();
// System.out.println();
string += ',';
String a[] = string.split(" ");
String key = a[0];
String s = a[1];
// System.out.println(key + " " + s);
String str[] = s.split("],");
// for(int i = 0; i < str.length; i++)
// {
// System.out.println(str[i]);
// }
boolean flag = false;
for(int i = 0; i < str.length; i++)
{
String sub[] = str[i].split("\\[");
// for(int j = 0; j < sub.length; j++)
// {
// System.out.print(sub[j] + " ");
// }
// System.out.println();
for(int j = 0; j < sub.length - 1; j++)
{
if((sub[j]).equals(key))
{
// System.out.println(sub[j + 1]);
flag = true;
String res[] = sub[j + 1].split("addr=|,mask=|,val=");
// System.out.println(res.length);
boolean mark = true;
for(int k = 1; k < res.length; k++)
{
if((res[k].startsWith("0x") && isHexNumber(res[k].substring(2))) || (res[k].startsWith("0X") && isHexNumber(res[k].substring(2))))
{
continue;
}
else
{
mark = false;
}
}
if(mark == true)
{
for(int k = 1; k < res.length; k++)// k == 0时,res[0]输出的是空格:" ",所以要从k == 1开始输出
{
if(k != res.length - 1)
{
System.out.print(res[k] + " ");
}
else
{
System.out.println(res[k]);
}
}
}
else if(mark == false) // 三个地址中只要有一个不合法,就打印 FAIL
{
System.out.println("FAIL");
}
}
}
}
if(flag == false)
{
System.out.println("FAIL");
}
}
sc.close();
}
}
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
// read read_his[addr=0xff,mask=0xff,val=0x1]
// read read[addr=0x17,mask=0xff,val=0z7]
// read read[addr=0x17,mask=0xff,val=0xh]
// read read[addr=0x17,mask=0xff,val=0xH]
2. 代码二:(AC)
import java.util.*;
import java.util.regex.*;
// import java.util.regex.Matcher;
// import java.util.regex.Pattern;
// 输入1:
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
// 输出1:
// 0x17 0xff 0x7
// 0xf0 0xff 0x80
// 输入2:
// read read_his[addr=0xff,mask=0xff,val=0x1]
// 输出2:
// FAIL
// 思路:
// 先比对最前面那个字符串,然后是三个属性,顺序不能错,然后是数值,必须0x或者0X开头,然后还得有数,以上全通过才行。
// 切分的时候容易弄混,多输出,弄一步输出一下就行。(需要注意检验数值是否十六进制)
public class Main_2_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
String str = sc.next();
Pattern pattern = Pattern.compile(str + "\\[addr=(0[xX][0-9a-fA-F]+),mask=(0[xX][0-9a-fA-F]+),val=(0[xX][0-9a-fA-F]+)\\]?");
String[] strings = sc.next().split("],");
boolean flag = false;
for (String s : strings) {
// System.out.println(s);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
flag = true;
System.out.println(matcher.group(1) + " " + matcher.group(2) + " " + matcher.group(3));
}
}
if(flag == false)
{
System.out.println("FAIL");
}
}
sc.close();
}
}
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
// read read_his[addr=0xff,mask=0xff,val=0x1]
// read read[addr=0x17,mask=0xff,val=0z7]
// read read[addr=0x17,mask=0xff,val=0xh]
// read read[addr=0x17,mask=0xff,val=0xH]
3. 代码三:(通过率67%)
再加上16进制的判断,应该就可以AC了。
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
String in = sc.next();
in = in + "[";
String string[] = sc.next().split(",");
// System.out.println(string.length);
String inArr[] = new String[4];
int count = 0;
for(int i = 0; i < string.length - 2; i = i + 3)
{
inArr[0] = string[i].split("addr=")[0];
inArr[1] = string[i].split("addr=")[1];
inArr[2] = string[i + 1].split("=")[1];
inArr[3] = string[i + 2].split("=")[1];
inArr[3] = inArr[3].split("\\]")[0];
// System.out.println("inArr[0]: " + inArr[0]);
// System.out.println("inArr[1]: " + inArr[1]);
// System.out.println("inArr[2]: " + inArr[2]);
// System.out.println("inArr[3]: " + inArr[3]);
if(in.equals(inArr[0]))
{
System.out.print(inArr[1] + " " + inArr[2] + " " + inArr[3] + "\r\n");
count++;
}
// System.out.println();
}
if(count == 0)
{
System.out.println("FAIL");
}
}
}
}
知识点:
- 计算机的十六进制数如何表示?
十六进制数
数的表示方法:十进制、二进制、八进制、十六进制等,以及二进制数的运算 - java中startsWith与endsWith的用法
java中startsWith与endsWith的用法
Java startsWith() 方法
Java endsWith() 方法 - Java substring() 方法
Java substring() 方法
java关于substring(a)与substring(a,b)的用法 - java 判断字符串是否为数字 十进制 十六进制
java 判断字符串是否为数字 十进制 十六进制 - java分割字符串 Unclosed character class near index 错误
java分割字符串 Unclosed character class near index 错误
Java中的坑之方括号
Unclosed Character Class Error?
java.util.regex.PatternSyntaxException: Unclosed character class near index 0 - Map.getOrDefault()方法
Map.getOrDefault()方法 - Map.keyset() 使用详解
Map.keyset() 使用详解
Map集合中value()方法与keySet()、entrySet()区别 - arrayList.sort((s1, s2) -> s1.compareTo(s2));的用法
java中排序函数sort()使用,Arrays.sort()和Collections.sort()
Java8 - 使用 Comparator.comparing 进行排序 - java中判断两个字符串是否相等
java中判断两个字符串是否相等的问题
Java compareTo() 方法
吐槽两句:
面向结果编程,学到了。。。