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


    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 + " ");
                }
                else
                {
                    HN(n - 1, first, third, middle);
                    Console.Write(first + "->" + third + " ");
                    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));
                
            }
        }

    }

  • 相关阅读:

    第一次发贴,也是在看了首页吵架贴后有感,也给博客园提个提议
    【把玩信息图之标签云】看看我一年的邮件在扯什么
    VS发布Web时自动调用YUICompressor批量压缩JS、CSS
    用JavaScript实现自己的DOM选择器
    一键发布应用了seajs的asp.net项目
    在JavaScript中应用单例模式
    Qt 资料大全
    C#的不足(1):匿名类型的不足之处
    LINQ解析:LINQ to Objects简介
  • 原文地址:https://www.cnblogs.com/dasydong/p/3280952.html
Copyright © 2020-2023  润新知