素数 约瑟夫环问题 斐波那契算法

using System;
using System.Collections.Generic;
 
using System.Text;
 
namespace ConsoleApplication2
{
    public class SuanFa
    {
        //求十进制数转换2进制后会有几个1
        public static int Number1(int n)
        {
            //n = Convert.ToInt32(Console.ReadLine());
            int x = 0;
            while (true)
            {
                while (n != 0)
                {
                    if ((n & 1) == 1)
                        x++;
                    n = n >> 1;

                }
                return x;

            }

        }

        //判断素数
        static bool Check(int val, int index)
        {
            bool ck = false;
            if (val == 2 || val == 3)
            {
                ck = true;
            }
            double squa = Math.Sqrt(val);
            for (int i = 2; i <= squa; i++)
            {
                if (val % i == 0)
                {
                    ck = false;
                    break;
                }
                else
                {
                    ck = true;
                }
            }
            return ck;
        }


        //第K大素数  筛选法
        private static List<long> Primes = new List<long> { 2, 3 };
        public static long Prime(int n)
        {

            for (var i = Primes[Primes.Count - 1] + 2; Primes.Count < n; i++, i++)     
            {
                bool isPrime = true;
                foreach (long j in Primes)
                {

                    if (j * j > i)
                        break;
                    if (i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                    Primes.Add(i);

            }

            return Primes[n - 1];
        }

        //汉诺塔
        static void HN(int n, char first, char middle, char third)
        {
            if (n <= 1)
            {
                Console.Write(first + "->" + third + "\n ");
            }
            else
            {
                HN(n - 1, first, third, middle);
                Console.Write(first + "->" + third + "\n ");
                HN(n - 1, middle, first, third);
            }
        }

        //最小汉诺塔步骤
        static int minHN(int n)
        {
            if (n == 1)
                return 1;
            else
            {
                return 2 * minHN(n - 1) + 1;
                // return Math.Pow(5, 2);
            }
        }

        //1 1 2 3 5..斐波那契函数
        public class Fib
        {

            private List<long> _fib;
            public Fib()
            {
                _fib = new List<long>();

                _fib.Add(1);

                _fib.Add(1);
            }
            public void update(int to)
            {
                int count = _fib.Count;
                while (count <= to)
                {
                    long temp;
                    temp = _fib[count - 1] + _fib[count - 2];
                    _fib.Add(temp);
                    count++;
                }
                //  Console.WriteLine(_fib[to]);

            }
            public long getNumber(int n)
            {
                // int n = Convert.ToInt32(Console.ReadLine());
                // if (_fib.Count < (n + 1))
                update(n);
                return _fib[n];
            }

        }


        //题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句

        //public static void Print_Sum()
        //{

        //    N = 0;
        //    temp = new Temp();
        //    Console.WriteLine(Sum);
        //}

        //public static Temp[] Temps = new Temp[Convert.ToInt32(Console.ReadLine())];
        //public static Temp temp;
        //public static int N { get; set; }
        //public static int Sum { get; set; }

        //public class Temp
        //{
        //    public Temp()
        //    {
        //        //++N 相当于每次执行都加1 
        //        ++N;
        //        //相当于1+2+...+N 
        //        Sum += N;
        //        //初始化Temp1类 
        //        Temp1 Temp1 = new Temp1();
        //    }
        //}

        //public class Temp1
        //{
        //    public Temp1()
        //    {
        //        try
        //        {
        //            Temps[N - 1] = temp;
        //            //初始化Temp类 
        //            temp = new Temp();

        //        }
        //        catch (Exception)
        //        {
        //            Sum -= N;

        //        }
        //    }
        //}


        //


        //约瑟夫环
        public static int[] Jose(int total, int start, int alter)
        {

            int j, k = 0;
            //count数组存储按出列顺序的数据,以当结果返回
            int[] count = new int[total];
            //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;
        }

        public static void StToInt(string s)
        {
            string b = "";

            for (int i = 0; i < s.Length; i++)
            {
                if (Char.IsNumber(s, i) == true)
                {
                    b += s[i];
                }
            }
            Console.WriteLine(Convert.ToInt32(b));
            
        }
    }

}

posted @ 2013-08-25 16:43  爱起早的小D  阅读(415)  评论(0编辑  收藏  举报