码迷,mamicode.com
首页 > 其他好文 > 详细

校招季笔试---亚马逊2014笔试题(1/4)

时间:2014-10-10 16:24:33      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   for   strong   数据   

校招季笔试---亚马逊2014笔试题(1/4)

1.前言:

去年入学今年就得校招找工作,时间匆匆。最近一直都是各种宣讲笔试。被虐的各种惨不忍睹啊!

分享昨天亚马逊在线笔试题题。水平有限放在这里全当留着纪念吧!

 

2.且看题目:

2.1 第一题

  bubuko.com,布布扣

我的解题思路:

 1. 比较一组序列首先处理号序列每个元素,2-A 数字int类型实现icompare接口是可以直接比较大小,所以把J-A转换成int类型依次赋值11-15。

 2. 定义一个数据机结构CardRanking 有两个属性Type(int)和NumberArray(List<int>),主要通过CardRanking实现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 第二题

bubuko.com,布布扣

2.3 第三题

bubuko.com,布布扣

校招季笔试---亚马逊2014笔试题(1/4)

标签:style   blog   http   io   os   ar   for   strong   数据   

原文地址:http://www.cnblogs.com/HaifengCai/p/4015724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!