题解 | #扑克牌大小#
扑克牌大小
http://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
我是***
let line
let typeDef = (arr)=>{
let max = 0
let idot = ''
for(let i = 0;i<arr.length;i++){
let count = 0
for(let j = 0;j<arr.length;j++){
if(arr[i] == arr[j]){
count++
idot = arr[i]
}
}
max=Math.max(max,count)
}
return max
}
let getIdotByDef = (arr,count)=>{
let idot = ''
let map = {};
map = arr.reduce((maper,item)=>{
maper[item] ? maper[item]++ : maper[item] = 1
return maper
},{});
for(let i in map){
if(map[i] ==count){
idot = i
}
}
return idot
}
let typeJoker = (arr)=>{
let count = 0
// 3代表大小王。2代表大王 1 代表小王 0代表不包含大小王
for(let j = 0;j<arr.length;j++){
if(arr[j] == 'joker'){
count++
}
if(arr[j] == 'JOKER'){
count+=2
}
}
// 不包含
return count
}
// 顺子
let typeOrder = (arr)=>{
let isOrder = false
// 3代表大小王。2代表大王 1 代表小王 0代表不包含大小王
if(arr.length<5){
return {
isOrder:isOrder,
idot:''
}
}
let str = arr.join('');
let temp =''
for(let j = 0;j<arr.length;j++){
for(let i = arr.length - 1;i>=0;i--){
let str1 = str.substring(j,i);
let regular = '345678910JQKA2345678910JQKA2';
if(regular.includes(str1) && str1.length == 5){
isOrder = true
if(!temp){
temp = str1
}else{
if(str1.length >temp ){
temp = str1
}
}
}
}
}
return {
isOrder:isOrder,
idot: temp.length > 0 ? temp[temp.length-1]:''
}
}
const handleSingle = (arr)=>{
let order = '345678910JQKA2';
let index = 0;
for(let j = 0;j< arr.length;j++){
let zIndex = order.indexOf(arr[j]);
if(zIndex>index){
index = zIndex
}
}
return index
}
while(line = readline()){
let arr = line.split('-');
let first = arr[0].split(' ');
let second = arr[1].split(' ');
let jokerCountFirst = typeJoker(first);
let jokerCountSecond = typeJoker(second);
let order = '345678910JQKA2'
if(jokerCountFirst == 3){
console.log(first.join(' '))
}
if(jokerCountSecond == 3){
console.log(second.join(' '))
}
if(jokerCountFirst != 3 && jokerCountSecond!=3){
// 不包含大王和小王
// 判断是四个一样的炸弹还是三个还是两个还是一个
let defCountFirst = typeDef(first);
let defCountSecond = typeDef(second);
// 判断是不是顺子
if(defCountFirst == 4 && defCountSecond == 4){
// 炸弹 题目已经明确炸弹可以比较 其他的牌只能和同类型的进行比较
let idotFirst= getIdotByDef(first,defCountFirst);
let idotSecond= getIdotByDef(second,defCountSecond);
if(order.indexOf(idotFirst) > order.indexOf(idotSecond)){
console.log(first.join(' '))
}else{
console.log(second.join(' '))
}
}else if(defCountFirst == 4 || defCountSecond == 4){
if(defCountFirst == 4){
console.log(first.join(' '))
}
if(defCountSecond == 4){
console.log(second.join(' '))
}
}else if(defCountFirst == 3 && defCountSecond == 3){
// 三个
let idotFirst= getIdotByDef(first,defCountFirst);
let idotSecond= getIdotByDef(second,defCountSecond);
if(order.indexOf(idotFirst) > order.indexOf(idotSecond)){
console.log(first.join(' '))
}else{
console.log(second.join(' '))
}
}else if(defCountFirst == 2 && defCountSecond == 2){
// 对子
let idotFirst= getIdotByDef(first,defCountFirst);
let idotSecond= getIdotByDef(second,defCountSecond);
if(order.indexOf(idotFirst) > order.indexOf(idotSecond)){
console.log(first.join(' '))
}else{
console.log(second.join(' '))
}
}else if(defCountFirst == 1 && defCountSecond == 1){
// 对子
let idotFirst= typeOrder(first);
let idotSecond= typeOrder(second);
if(idotFirst.isOrder && idotSecond.isOrder){
if(order.indexOf(idotFirst.idot) > order.indexOf(idotSecond.idot)){
console.log(first.join(' '))
}else{
console.log(second.join(' '))
}
}else{
if(jokerCountFirst > jokerCountSecond){
console.log(first.join(' '))
}else if(jokerCountFirst < jokerCountSecond){
console.log(second.join(' '))
}else {
// 都没有大小王
if(handleSingle(first) > handleSingle(second)){
console.log(first.join(' '))
}else{
console.log(second.join(' '))
}
}
}
}else{
console.log('ERROR')
}
}
}