# 一道亞馬遜的在線筆試題

來園子快一年了，從來都是隻看看，不說話。一來覺得自己技術太弱，說出來誤導別人；二來自己文筆太差，寫出來不通就貽笑大方了。

之前看到園子有人發了一道面試題，覺得蠻有意思的，鼓起勇氣寫一篇，讓大神們扶正一下，也好促進自己進步！

3,3,7,4 < 3,3,7,5

看到這個規則第一印象就是我們四川的鬥地主嘛（每年過年，全家必玩的遊戲，嘿嘿），不過是四張牌和沒有花色而已！

首先是Card類：

```/// <summary>
/// 牌
/// </summary>
public class Card : IComparable
{
private string num;
private int count;
/// <summary>
/// 數字
/// </summary>
public int Num
{
get
{
return ConvertStringToNum(num);
}
set
{
num = value.ToString();
}
}
/// <summary>
/// 出現次數
/// </summary>
public int Count
{
get { return count; }
set { count = value; }
}
public Card(string num)
{
this.num = num.Trim();
this.Count = 1;
}
public Card()
{ }
/// <summary>
/// 實現排序
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
int result;
try
{
Card card = obj as Card;
result = this.Count.CompareTo(card.Count);
if (result == 0)
{
result = this.Num.CompareTo(card.Num);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return result;
}
/// <summary>
/// 將JQKA轉爲數字，方便比較
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
private int ConvertStringToNum(string inputStr)
{
int result = 0;
if (!Int32.TryParse(inputStr, out result))
{
switch (inputStr)
{
case "J":
result = 11;
break;
case "Q":
result = 12;
break;
case "K":
result = 13;
break;
case "A":
result = 14;
break;

}
}
return result;

}
}```
View Code

這個類主要實現了IComparable中的CompareTo方法，方便之後的排序比較。

然後是四張牌的類型（原諒我取了一個很sao二聲，四川人應該懂得起。金花：GlodFlower）：

``` public enum GlodFlowerType
{
/// <summary>
/// 散牌
/// </summary>
Other,
/// <summary>
/// 順子
/// </summary>
Progression,
/// <summary>
/// 全相同
/// </summary>
AllSame,

}```
View Code

再後是主要的“金花”類（- -！）:

```  public class GoldFlower
{
public static  List<string> cardNumList =new List<string>  { "A","2","3","4","5","6","7",
"8","9","10","J","Q","K"};

private List<int> numList = new List<int>();
public List<Card> cardList = new List<Card>();
public GlodFlowerType Type
{
get
{
numList.Sort();
if (numList[0] == numList[numList.Count - 1])
return GlodFlowerType.AllSame;
else if (IsProgression(numList))
return GlodFlowerType.Progression;
else
return GlodFlowerType.Other;

}
}
/// <summary>
/// 判斷是否是連牌
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private bool IsProgression(List<int> list)
{
list.Sort();
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i + 1] != list[i] + 1)
return false;
}
return true;
}
public GoldFlower(List<string> list)
{
Card card;
Card cardA = null;
List<int> tempList = new List<int>();
foreach (string s in list)
{
card = new Card(s);
if (card.Num == 14)
cardA = card;
if (!tempList.Contains(card.Num))
{
}
else
{
int index = numList.IndexOf(card.Num);
cardList[index].Count += 1;
}
}
if (cardA != null)
{
int index = cardList.IndexOf(cardA);
numList.Remove(14);
if (IsProgression(numList) && numList.Contains(2))
{
cardList[index].Num = 1;
}
else
}
}

public int CompareTo(GoldFlower glodFlower)
{
int result = 0;
this.cardList.Sort();
glodFlower.cardList.Sort();
result = this.Type.CompareTo(glodFlower.Type);
if (result == 0)
result = ComparenTwoArray(this.cardList, glodFlower.cardList, this.cardList.Count, glodFlower.cardList.Count);

return result;
}

/// <summary>
/// 字符串比較思想比較兩個數組的大小
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="aLen"></param>
/// <param name="bLen"></param>
/// <returns></returns>
private int ComparenTwoArray(List<Card> a, List<Card> b, int aLen, int bLen)
{
int i = aLen - 1, j = bLen - 1;
while (i > 0 && j > 0)
{
if (a[i].Count == b[j].Count && a[i].Num == b[j].Num)
{
i--;
j--;
}
else if (a[i].Count != b[j].Count)
return a[i].Count.CompareTo(b[j].Count);
else if (a[i].Num != b[j].Num)
return a[i].Num.CompareTo(b[j].Num);
}
if (i == 0 && j == 0)
{
return 0;
}
else
{
return i == 0 ? -1 : 1;
}
}
}```
View Code

比較兩個類的時候，先比較類型，類型相同後再比較牌的數字大小。
測了幾組數據，還沒有發現誤判，但是感覺好慢。不知道是我的機子太爛還是算法不好。希望各位大神幫幫小女子！

最後，請教一下大家，我看他們的博客都好漂亮，爲什麼我的不行？排版這些怎麼弄的？真的第一次發這種東西，小白一枚！