C#华为OD笔试题*3

C#华为OD笔试题*3

背景

参加了一下华为OD笔试,共三道题特此记录一下前两道都对了,第三道还在研究之后更新。

代码

题目1

   轮转寿司店办活动,假设5盘寿司价格分别为3 14 15 6 5,买其中一盘寿司则赠送下一盘价格小于他的寿司,最低价格不赠送,比如买14价格的寿司 赠送6元的 买5元的赠送3元的,请输出实际商品价值。比如3 14 15 6 5,最终输出为3 20 18 9 8

        public static void Main(string[] args)
        {
            string line;
            while ((line = System.Console.ReadLine()) != null)
            { // 注意 while 处理多个 case
                string[] tokens = line.Split(' ');
                if (tokens.Length != 0)
                {
                    if (tokens.Length == 1)
                        Console.WriteLine(tokens[0]);
                    else
                    {
                        int[] inputArr = tokens.AsEnumerable().Select(o => Convert.ToInt32(o)).ToArray<int>();
                        int[] resArr = GetTotalPrice(inputArr);
                        Console.WriteLine(string.Join(" ", resArr));
                    }
                }
            }
            Console.ReadKey();
        }

        static int[] GetTotalPrice(int[] arr)
        {
            int[] newArr = new int[arr.Length];
            //3 14 15 6 5
            //3 20 18 9 8
            for (int i = 0; i < arr.Length; i++)
            {
                int tmp = 0;
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[i])
                    {
                        tmp = arr[j];
                        break;
                    }
                }
                if (tmp == 0 && i > 0)
                {
                    for (int j = 0; j < i - 1; j++)
                    {
                        if (arr[j] < arr[i])
                        {
                            tmp = arr[j];
                            break;
                        }
                    }
                }
                newArr[i] = tmp + arr[i];
            }
            return newArr;
        }

题目2

        输入
        5 4
        1
        1
        2
        3
        5
        1 2 3
        1 4
        3 4 5
        2 3 4
        5测试用例权重 4组测试用例
        相同权重按顺序 按序号排序
        1 2 3 权重4
        1 4 权重 4
        3 4 5 权重 10
        2 3 4 权重 6
        输出 3 4 1 2

        static int numM = 0;
        static int numN = 0;
        static int[] arrM;
        static int[] arrN;
        static int rowCount = 0;
        public static void Main()
        {
            //m n m times spe n times data
            List<int[]> list = new List<int[]>();
            string line;
            while (!string.IsNullOrEmpty(line = System.Console.ReadLine()))
            { // 注意 while 处理多个 case
                string[] tokens = line.Split(' ');
                if (list.Count == 0 && tokens.Length == 2)
                {
                    numM = Convert.ToInt32(tokens[0].Trim());
                    numN = Convert.ToInt32(tokens[1].Trim());
                    rowCount = numM + numN + 1;
                    arrM = new int[numM];
                    arrN = new int[numN];
                    list.Add(new int[] { numM, numN });
                }
                else if (list.Count <= rowCount)
                {
                    if (list.Count >= 1 && list.Count < numM + 1)
                    {
                        list.Add(new int[] { Convert.ToInt32(tokens[0]) });
                        arrM[list.Count - 1 - 1] = Convert.ToInt32(tokens[0]);
                    }
                    else
                    if (list.Count >= numM + 1 && list.Count <= rowCount - 1)
                    {
                        list.Add(tokens.AsEnumerable().Select(o => Convert.ToInt32(o)).ToArray());
                        int valueN = tokens.AsEnumerable().Select(o => arrM[Convert.ToInt32(o) - 1]).Sum();
                        arrN[list.Count - numM - 1 - 1] = valueN;
                        if (list.Count == rowCount)
                        {
                            int[] newArr = arrN.AsEnumerable().OrderByDescending(o => o).ToArray();
                            List<int> arrIndex = new List<int>();
                            for (int i = 0; i < numN; i++)
                            {
                                for (int j = 0; j < numN; j++)
                                {
                                    if (!arrIndex.Contains(j) && arrN[j] == newArr[i])
                                        arrIndex.Add(j);
                                    else
                                        continue;
                                }
                            }
                            foreach(var item in arrIndex)
                                Console.WriteLine(item+1);
                            list.Clear();
                            rowCount = 0;
                            arrM = null;
                            arrN = null;
                            numM = 0;
                            numN = 0;
                        }
                    }
                }
            }
            Console.ReadKey();
        }

题目3

        /**
         * 返回通过指定路口之间的最短时间
         * @param lights int整型二维数组 n*m 个街口每个交通灯的周期,值范围[0,120],n和m的范围为[1,9]
         * @param timePerRoad int整型 相邻两个街口之间街道的通过时间,范围为[0,600]
         * @param rowStart int整型 起点的行号
         * @param colStart int整型 起点的列号
         * @param rowEnd int整型 终点的行号
         * @param colEnd int整型 终点的列号
         * @return int整型
         */

实现算法,这个应该是最小路径。菜,没答上来。

研究明白再回来更。

        public int calcTime(int[][] lights, int timePerRoad, int rowStart, int colStart, int rowEnd, int colEnd)
        {
            // write code here
            int m = lights[0].Length;
            int n = lights.Count();
            // 0 0 2 2
            int num1 = rowEnd - rowStart;
            int num2 = colEnd - colStart;
            //youzhuan 0 zuozhuan 5
            int rowTurn = 0;
            if (num1 >= 0 && num2 >= 0)
                rowTurn = 5 * 1 + 0 * 2;
            if (num1 <= 0 && num2 <= 0)
                rowTurn = 2 * 5 + 0;
            if (num1 <= 0 && num2 >= 0)
                rowTurn = 2 * 5 + 0;
            int res = (num1 + num2) * timePerRoad + rowTurn;
            return res;
        }

百战不灭。。。总能拿到offer的