07 链表

    class Program
    {
        static void Main(string[] args)
        {
            //var a1 = Jose(10, 3, 4);
            //foreach (var item in a1)
            //{
            //    Console.WriteLine(item);
            //}

            var strs = new char[] { 'a', 'b', 'c', 'd', };
            var index = Find(strs, 'b');

            Console.ReadKey();
        }
        static int Find2(char[] a, char key)
        {
            // 我举2个例子,你可以拿例子走一下代码
            // a = {4, 2, 3, 5, 9, 6} n=6 key = 7
            // a = {4, 2, 3, 5, 9, 6} n=6 key = 6
            if (a == null || a.Length <= 0)
            {
                return -1;
            }
            int n = a.Length;
            // 这里因为要将a[n-1]的值替换成key,所以要特殊处理这个值
            if (a[n - 1] == key)
            {
                return n - 1;
            }
            // 把a[n-1]的值临时保存在变量tmp中,以便之后恢复。tmp=6。
            // 之所以这样做的目的是:希望find()代码不要改变a数组中的内容
            char tmp = a[n - 1];
            // 把key的值放到a[n-1]中,此时a = {4, 2, 3, 5, 9, 7}
            a[n - 1] = key;
            int i = 0;
            // while 循环比起代码一,少了i<n这个比较操作
            while (a[i] != key)
            {
                ++i;
            }
            // 恢复a[n-1]原来的值,此时a= {4, 2, 3, 5, 9, 6}
            a[n - 1] = tmp;
            if (i == n - 1)
            {
                // 如果i == n-1说明,在0...n-2之间都没有key,所以返回-1
                return -1;
            }
            else
            {
                // 否则,返回i,就是等于key值的元素的下标
                return i;
            }
        }
        // 在数组a中,查找key,返回key所在的位置
        // 其中,n表示数组a的长度
        private static int Find(char[] a, char key)
        {
            // 边界条件处理,如果a为空,或者n<=0,说明数组中没有数据,就不用while循环比较了
            if (a == null || a.Length <= 0)
            {
                return -1;
            }
            int i = 0;
            // 这里有两个比较操作:i<n和a[i]==key.
            while (i < a.Length)
            {
                if (a[i] == key)
                {
                    return i;
                }
                ++i;
            }
            return -1;
        }

        //从第start人开始计数,以alter为单位循环记数出列,总人数为total 
        public static int[] Jose(int total, int start, int alter)
        {
            int j, k = 0;
            //count数组存储按出列顺序的数据,以当结果返回 
            int[] count = new int[total + 1];
            //s数组存储初始数据 
            int[] s = new int[total + 1];
            //对数组s赋初值,第一个人序号为0,第二人为1,依此下去
            for (int i = 0; i < total; i++)
            {
                s[i] = i;
            }
            //按出列次序依次存于数组count中 
            for (int i = total; i >= 2; i--)
            {
                start = (start + alter - 1) % i;
                if (start == 0)
                    start = i;
                count[k] = s[start];
                k++;
                for (j = start + 1; j <= i; j++)
                    s[j - 1] = s[j];
            }
            count[k] = s[1];
            //结果返回 
            return count;
        }
    }

猜你喜欢

转载自www.cnblogs.com/kikyoqiang/p/10505193.html
07