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

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

1.前言:

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

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

 

2.且看题目:

2.1 第一题

  

我的解题思路:

 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 第二题

2.3 第三题

转载于:https://www.cnblogs.com/HaifengCai/p/4015724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值