荣耀9.23笔试AK
前两题比较简单,第一题把题目读清楚就行,第二题也是理解题目就ok,第三题稍微考虑仔细点dfs即可
贴个第三题代码:
package rongyao;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test3 {
static int n;
static int[] cards;
static Map<Integer, Integer> map;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
n = in.nextInt();
cards = new int[n];
int ret = 0;
boolean J1 = false;
boolean J2 = false;
map = new HashMap<>();
for(int i=0 ; i<15 ; ++i){
map.put(i, 0);
}
if(n <= 1){
System.out.println(0);
continue;
}
for(int i=0 ; i<n ; ++i){
char[] cs = in.next().toCharArray();
if(cs[0] == 'J'){
if(cs[1] == '1'){
cards[i] = 0;
}
if(cs[1] == '2'){
cards[i] = 1;
}
}else if(Character.isDigit(cs[1])){
if(cs.length == 3){
cards[i] = 10;
}else{
cards[i] = cs[1] - '0';
}
}else{
if(cs[1] == 'J'){
cards[i] = 11;
}else if(cs[1] == 'Q'){
cards[i] = 12;
}else if(cs[1] == 'K'){
cards[i] = 13;
}else if(cs[1] == 'A'){
cards[i] = 14;
}
}
map.put(cards[i], map.get(cards[i]) + 1);
}
int x = Math.min(map.get(0), map.get(1));
map.put(0, map.get(0) - x);
map.put(1, map.get(1) - x);
x *= 5;
System.out.println(dfs(0) + x);
}
}
public static int dfs(int i){
System.out.println(i+ " " + map);
if(i == 15){
return 0;
}
int ret = 0;
if(map.get(i) >= 4){
map.put(i, map.get(i) - 4);
ret = Math.max(dfs(i)+5, ret);
map.put(i, map.get(i) + 4);
}
if(map.get(i) >= 3){
map.put(i, map.get(i) - 3);
ret = Math.max(dfs(i)+4, ret);
map.put(i, map.get(i) + 3);
}
if(map.get(i) >= 2){
map.put(i, map.get(i) - 2);
ret = Math.max(dfs(i)+2, ret);
map.put(i, map.get(i) + 2);
}
if(check(i)){
func1(i);
ret = Math.max(ret, dfs(i) + 3);
func2(i);
}
ret = Math.max(ret, dfs(i+1));
return ret;
}
public static boolean check(int i){
if(i > 10 || i <= 1){
return false;
}
for(int j=i ; j<i+5 ; ++j){
if(map.get(j) == 0){
return false;
}
}
return true;
}
public static void func1(int i){
for(int j=i ; j<i+5 ; ++j){
map.put(j, map.get(j) - 1);
}
}
public static void func2(int i){
for(int j=i ; j<i+5 ; ++j){
map.put(j, map.get(j) + 1);
}
}
}
查看5道真题和解析