Fork me on GitHub

1.随机输入一串字母,判断这一串字母是否是一个数字?(简单)

class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("输入一串字母:");
                string readStr = Console.ReadLine();

                if (IsNumber(readStr))
                {
                    Console.WriteLine("输入的数字是:{0}", readStr);
                }
                else
                {
                    Console.WriteLine("输入的不是数字");
                }
                Console.WriteLine();
            }
        }

        public static bool IsNumber(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }

            foreach (char c in str)
            {
                if (!char.IsDigit(c))//if(c<'0' || c>'9')
                    return false;
            }

            return true;
        }

结果显示按下面格式:

输入一串字母:
2
输入的数字是:2

输入一串字母:
q
输入的不是数字

输入一串字母:



2.随机输入一个数字,判断这个数字是否是素数?(中)

class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("输入数字:");
                int readStr =int.Parse(Console.ReadLine());

                if (IsPrimeNumber(readStr))
                {
                    Console.WriteLine("输入的素数是:{0}", readStr);
                }
                else
                {
                    Console.WriteLine("输入的不是素数");
                }
                Console.WriteLine();
            }
        }

        public static bool IsPrimeNumber(int n)
        {
            bool b = true;

            if (n == 1 || n == 2)
            {
                b = true;
            }
            else
            {
                int sqr = Convert.ToInt32(Math.Sqrt(n));

                for (int i = sqr; i > 2; i--)
                {
                    if (n % i == 0)
                    {
                        b = false;
                    }
                }
            }

            return b;
        }
}

输出结果如下:

输入数字:

23

输入的素数是:23

 

输入数字:

24

输入的不是素数

 

输入数字:

 

3.小红家养了一只松鼠,妈妈买了一袋松子,松鼠第一天一半多一个,第二天吃剩下的一半多一个,一次类推,吃了n天,最后全部吃完,问妈妈买了多少松子?

    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("松鼠要吃,妈妈需要卖多少松子:");
                int readStr = int.Parse(Console.ReadLine());
                Console.WriteLine(Apple(1, readStr));
                Console.WriteLine();
            }
        }

        /// <summary>
        /// 共吃了day天,
        /// </summary>
        /// <param name="n">计数器</param>
        /// <param name="day">吃了n天</param>
        /// <returns></returns>
        public static int Apple(int n, int day)
        {
            if (n == day)
            {
                return 2;
            }
            else
            {
                return 2 * (Apple(n + 1, day) + 1);
            }
        }
}

 4.在100-999中,一个数是4的倍数,可以开平方,还有两位相同,输出符合条件的在100-999中所有数。例如:100(难)

class Program
    {
        static void Main(string[] args)
        {
            IsCommon();
            Console.ReadLine();
        }

        public static void IsCommon()
        {
            int i, j;

            for (i = 100; i <= 999; i++)
            {
                int hundred = 1, ten = 2, data = 3;

                j = 10;

                while (j * j <= i)
                {
                    if (i == j * j)
                    {
                        hundred = i / 100;
                        ten = i % 100 / 10;
                        data = i % 100 % 10;
                    }

                    if (hundred == ten || hundred == data || ten == data)
                    {
                        if (i % 4 == 0)
                        {
                            Console.Write(i + "   ");
                        }
                    }

                    j++;
                }
            }
        }
}

 

posted on 2014-09-18 22:41  lingfeng95  阅读(355)  评论(0编辑  收藏  举报