School recruitment quarterly written test --- Amazon 2014 pen exam (1/4)
1. Preface:
Last year, I had to recruit and find a job. It was in a hurry. It has been a variety of lectures and written tests recently. The various types of abuse are terrible!
Share yesterday's Amazon online pen questions. The level is limited here, so keep it all here!
2. Check the question:
2.1 Question 1
My solutions:
1. Compare a set of sequences first process each element of the serial number, 2-a numeric int type implementation icompare interface can be directly compare the size, so the J-A to int type in turn assigned a value of 11-15.
2. Define a data machine structure. cardranking has two attributes: type (INT) and numberarray (list <int>). icomparable <cardranking>
Code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Amazon{ //定义一个数据结构:正确拿牌后每个人的手牌情况,实现ICompable 接口实现比较手牌方法 class CardRanking : IComparable<CardRanking> { //T1:6 //T2:5 //T3:4 //T4:3 //T5:2 //T6:1 public int Type { get; set; } public List<int> NumberArray { get; set; } //构成函数赋值 public CardRanking(List<int> array) { this.NumberArray = array; this.Type = GetType(array); } //获取手牌的类型 private int GetType(List<int> array) { Dictionary<int, int> tmp = new Dictionary<int, int>(); foreach (int card in array) { if (!tmp.ContainsKey(card)) { tmp.Add(card, 0); } else { tmp[card]++; } } if (tmp.Count == 1) { return 6; } if (tmp.Count == 4) { int w = tmp.Values.ToArray()[0]; if ((tmp.Values.ToArray()[0] - 3).CompareTo(tmp.Values.ToArray()[3]) == 0 && (tmp.Values.ToArray()[1] - 2).CompareTo(tmp.Values.ToArray()[3]) == 0 && (tmp.Values.ToArray()[2] - 1).CompareTo(tmp.Values.ToArray()[3]) == 0) { return 5; } else { return 1; } } if (tmp.Count == 2) { foreach (var item in tmp.Values) { if (item.CompareTo(2) == 0) return 4; else return 3; } } if (tmp.Count == 3) { return 2; } return -2; } //实现接口的比较方法 public int CompareTo(CardRanking other) { if (this.Type.CompareTo(other.Type) != 0) return this.Type.CompareTo(other.Type); switch (this.Type) { case 6: return this.NumberArray[0].CompareTo(other.NumberArray[0]); case 5: return this.NumberArray[0].CompareTo(other.NumberArray[0]); case 4: if (this.NumberArray[0].CompareTo(other.NumberArray[0]) == 0) return this.NumberArray[3].CompareTo(other.NumberArray[3]); else return this.NumberArray[0].CompareTo(other.NumberArray[0]); case 3: if (this.NumberArray[0].CompareTo(other.NumberArray[0]) == 0) return this.NumberArray[3].CompareTo(other.NumberArray[3]); else return this.NumberArray[0].CompareTo(other.NumberArray[0]); case 2: if (this.NumberArray[0].CompareTo(other.NumberArray[0]) != 0) return this.NumberArray[0].CompareTo(other.NumberArray[0]); else if (this.NumberArray[2].CompareTo(other.NumberArray[2]) != 0) return this.NumberArray[2].CompareTo(other.NumberArray[2]); else return this.NumberArray[3].CompareTo(other.NumberArray[3]); case 1: if (this.NumberArray[0].CompareTo(other.NumberArray[0]) != 0) return this.NumberArray[0].CompareTo(other.NumberArray[0]); else if (this.NumberArray[1].CompareTo(other.NumberArray[1]) != 0) return this.NumberArray[1].CompareTo(other.NumberArray[1]); else if (this.NumberArray[2].CompareTo(other.NumberArray[2]) != 0) return this.NumberArray[2].CompareTo(other.NumberArray[2]); else return this.NumberArray[3].CompareTo(other.NumberArray[3]); } return -2;//错误情况 } } class Program { //所有正确输入牌信息 public static Dictionary<string, int> allCards = null; static void Main(string[] args) { allCards = new Dictionary<string, int>(){ {"2",2}, {"3",3}, {"4",4}, {"5",5}, {"6",6}, {"7",7}, {"8",8}, {"9",9}, {"10",10}, {"J",11}, {"Q",12}, {"K",13}, {"A",14}, }; //你的手牌 List<string> yourCard = new List<string>() { "5", "2", "3", "4" }; //我的手牌 List<string> myCard = new List<string>() { "2", "3", "5", "4" }; //得到比较结果 int result = PokerHandRanking(myCard, yourCard); switch (result.ToString()) { case "-1": Console.WriteLine("You win!"); break; case "0": Console.WriteLine("We win!"); break; case "1": Console.WriteLine("I win!"); break; default: Console.WriteLine("Error!"); break; } } //比较方法 private static int PokerHandRanking(List<string> first, List<string> second) { List<int> firstArray; List<int> secondArray; if (CheckInput(first, out firstArray) || CheckInput(second, out secondArray)) return -2; CardRanking firstOne = new CardRanking(firstArray); CardRanking secondOne = new CardRanking(secondArray); return firstOne.CompareTo(secondOne); } //参数检查和数据格式化 private static bool CheckInput(List<string> cards, out List<int> array) { array = new List<int>(); if (cards == null || cards.Count != 4) { return true; } for (int i = 0; i < cards.Count; i++) { if (!allCards.ContainsKey(cards[i])) { return true; } else { array.Add(allCards[cards[i]]); } } array.Sort(); array.Reverse(); return false; } }}
2.2 Question 2
2.3 question 3
School recruitment quarterly written test --- Amazon 2014 pen exam (1/4)