`
zxlyecf2
  • 浏览: 129612 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个有意思的面试题

阅读更多

给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数    要求下排每个数都是先前上排那十个数在下排出现的次数。    上排的十个数如下:    

【0,1,2,3,4,5,6,7,8,9】  

初看此题,貌似很难,10分钟过去了,可能有的人,题目都还没看懂。     

举一个例子,    

数值: 0,1,2,3,4,5,6,7,8,9    分配: 6,2,1,0,0,0,1,0,0,0    

0在下排出现了6次,1在下排出现了2次,    2在下排出现了1次,3在下排出现了0次....    以此类推..

private static int length = 9;
        int[] top = new int[length];
        int[] bottom = new int[length];
        bool success;

        protected void Page_Load(object sender, EventArgs e)
        {
            int i;
            InitNum();
            SetBottomNum();
            for (i = 0; i < length; i++)
                Response.Write(bottom[i]);
        }

        /// <summary>
        /// 初始化上下排数组
        /// </summary>
        private void InitNum()
        {
            int i;
            success = false;
            for (i = 0; i < length; i++)
            {
                top[i] = i;
                bottom[i] = i;
            }
        }

        /// <summary>
        /// 获取频率
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private int GetFrequecy(int num)
        {
            int i;
            int count = 0;
            for (i = 0; i < length; i++)
            {
                if (bottom[i] == num)
                    count++;
            }
            return count;
        }

        /// <summary>
        /// 设置下排数组
        /// </summary>
        private void SetBottomNum()
        {
            int i;
            int frequecy;
            while (!success)
            {
                bool temp = true; for (i = 0; i < length; i++)
                {
                    frequecy = GetFrequecy(i);
                    if (bottom[i] != frequecy)
                    {
                        bottom[i] = frequecy;
                        temp = false;
                    }
                }
                success = temp;
            }
        }
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics