经典实例

综合实例


 

水仙花数(Narcissistic number):

也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 水仙花数
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请按任意键继续:");
            Console.ReadLine();
            Console.WriteLine("100到1000之间的水仙花数为:");
            for (int i = 100; i < 1000;i++ )
            {
                int gw = i % 10;                    //求个位
                int sw = i / 10 % 10;               //求十位 
                int bw = i / 100 % 10;              //求百位
                if(i==gw*gw*gw+sw*sw*sw+bw*bw*bw)   //判断是否为水仙花数 
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadKey();
        }
    }
}
100-999中的水仙花数

桃子问题(经典问题):

条件桃子3元一个,3个桃核可以换一个桃子

金额(可以由用户自己输入):100元,求出最多吃多少个桃子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 买桃问题
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {

                //提示并储存输入的数字
                Console.WriteLine("请输入你的金钱(桃子3元一个):");
                int input = int.Parse(Console.ReadLine());

                //把输入给钱,单价定义为3,定义exchan:1次机会等于3个桃核
                int money = input, price = 3, exchange = 3;
                int eated, kernel;

                //eated:第一次换取得数量
                //kerner:第一次吃完后的桃核数量

                eated = money / price;
                kernel = eated;

                //当kerner大于机会的要求时,进入循环
                while (kernel >= exchange)
                {
                    int peach = kernel / exchange;          //peach:用桃核换取得桃子数量
                    int left = kernel % exchange;           //left:换桃子后的桃核余数(肯定小于3)
                    eated += peach;                         //累加吃的桃子数量
                    kernel = left + peach;                  //累加桃核数量
                }
                Console.WriteLine("最多可以吃到" + eated + "个桃子");  //输出
            }
        }
    }
}
View Code

哥德巴赫猜想(世界近代三大数学难题之一):

哥德巴赫1742年给欧拉的信中哥德巴赫提出了以下猜想:任一大于2的偶数都可写成两个质数之和。但是哥德巴赫自己无法证明它,于是就写信请教赫赫有名的大数学家欧拉帮忙证明,但是一直到死,欧拉也无法证明。[1]  因现今数学界已经不使用“1也是素数”这个约定,原初猜想的现代陈述为:任一大于5的整数都可写成三个质数之和。欧拉在回信中也提出另一等价版本,即任一大于2的偶数都可写成两个质数之和。今日常见的猜想陈述为欧拉的版本。把命题"任一充分大的偶数都可以表示成为一个素因子个数不超过a个的数与另一个素因子不超过b个的数之和"记作"a+b"。1966年陈景润证明了"1+2"成立,即"任一充分大的偶数都可以表示成二个素数的和,或是一个素数和一个半素数的和"。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 买桃问题
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {

                //提示并储存输入的数字
                Console.WriteLine("请输入你的金钱(桃子3元一个):");
                int input = int.Parse(Console.ReadLine());

                //把输入给钱,单价定义为3,定义exchan:1次机会等于3个桃核
                int money = input, price = 3, exchange = 3;
                int eated, kernel;

                //eated:第一次换取得数量
                //kerner:第一次吃完后的桃核数量

                eated = money / price;
                kernel = eated;

                //当kerner大于机会的要求时,进入循环
                while (kernel >= exchange)
                {
                    int peach = kernel / exchange;          //peach:用桃核换取得桃子数量
                    int left = kernel % exchange;           //left:换桃子后的桃核余数(肯定小于3)
                    eated += peach;                         //累加吃的桃子数量
                    kernel = left + peach;                  //累加桃核数量
                }
                Console.WriteLine("最多可以吃到" + eated + "个桃子");  //输出
            }
        }
    }
}
View Code

斐波那契数列(Fibonacci sequence):

又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=1,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从1963起出版了以《斐波纳契数列季刊》为名的一份数学杂志,用于专门刊载这方面的研究成果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 斐波拉数列
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取用户输入的长度
            Console.WriteLine("请输入斐波拉数列的长度:");
            int input = int.Parse(Console.ReadLine());

            //创建一个数组
            int[] arr = new int[input];
            for (int i = 0; i < arr.Length; i++)
            {

                //i=0或1,输出1
                if (i <= 1)
                {
                    arr[i] = 1;
                    Console.Write(arr[i] + "\t");
                }

                //数组索引为i的元素值=数组索引为i-1的值+数组索引为i-2的值
                else
                {
                    arr[i] = arr[i - 1] + arr[i - 2];
                    Console.Write(arr[i] + "\t");
                }
            }

            Console.ReadLine();

        }
    }
}
View Code

分解质因数:

每个合数都可以写成几个质数相乘的形式,其中每个质数都是这个合数的因数,叫做这个合数的分解质因数。 分解质因数只针对合数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 分解质因数
{
    class Program
    {
        static void Main(string[] args)
        {
       
               Console.WriteLine("请输入非质数后回车!");
               int inNumber = int.Parse(Console.ReadLine());//将输入转换为int类型
       
               string texRet = inNumber.ToString() + " = ";//将inNumber转换为字符串+等号
            
               //i为被除数,一直增加,直到找到能被除的数
               for (int i = 2; i <= inNumber; i++)
               {
                   if (inNumber % i == 0)
                   {
                       texRet += i.ToString() + "*";//texRet=texRet+i转换成字符串+*号
                       inNumber = inNumber / i;//将inNumber重新取值
                       i--;
                   }
               }
               texRet = texRet.Substring(0, texRet.Length - 1);
               Console.WriteLine(texRet);
               Console.ReadKey();



            /*
            while (true)
            {
                Console.WriteLine("请输入一个非质数:");
                Console.ReadLine();
                int num = Convert.ToInt32(Console.ReadLine());
                for (int i = 2; i < num; i++)//循环  1没有质因数
                {
                    while (num != i)//num==i的话跳出循环当然num==i你也进不去
                    {
                        if (num % i == 0)//num%i为0就继续执行
                        {
                            Console.Write(num+"="+i + "*");//输出质数i的值
                            num /= i;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                Console.WriteLine(num);//这个是跳出循环的值,没得求了,不跳出来不行啊,当然也是质数
            }
*/

        }
    }
}
View Code

反序输出:

用户输入字符串后,字母反序输出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 反序输出
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输出一个字符串:");
            string input = Console.ReadLine();


        //定义一个空字符串reverse
            string reverse = "";

        //从大到小循环
            for (int i = input.Length - 1; i >= 0; i--)
            {
                //索引从大到小存入新字符串内
                reverse = reverse + input[i];
            }


            Console.WriteLine(reverse + ":串字符个一出输请");
            Console.ReadLine();
        }
    }
}
View Code

日历控制台:

C#基本的知识实现日历的输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 控制台日历
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {

                int year, month;

                //死循环只有全部输对才能跳出进行下一步
                while (true)
                {
                    Console.WriteLine("请输入年份(1900-2100):");
                    year = int.Parse(Console.ReadLine());
                    if (year < 1900 || year > 2100)
                    {
                        Console.WriteLine("年份输入错误,请重新输入");
                        Console.ReadLine();
                        Console.Clear();
                    }
                    else
                    {
                        Console.WriteLine("请输入月份(1-12):");
                        month = int.Parse(Console.ReadLine());
                        if (month < 1 || month > 12)
                        {
                            Console.WriteLine("月份输入错误,请重新输入:");
                            Console.ReadLine();
                            Console.Clear();
                        }
                        else
                        {
                            break;//跳出循环

                        }
                    }
                }




                
                //定义一个集合
                List<string> dates = new List<string>();



                //计算从1900到输入的年份一共有多少年
                int crossDayOfYear = 0;
                for (int i = 1900; i < year; i++)
                {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                    {
                        crossDayOfYear += 366;
                    }
                    else
                    {
                        crossDayOfYear += 365;//并且累加天数
                    }
                }




                //计算从1月到输入的月份一共有多少个月
                int crossDayMonth = 0;
                for (int i = 1; i < month; i++)
                {

                    //2月里面有闰年和平年两种情况
                    if (i == 2)
                    {
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        {
                            crossDayMonth += 29;
                        }
                        else
                        {
                            crossDayMonth += 28;
                        }
                    }

                    //大月的情况
                    else if (i <= 7 && i % 2 != 0 || i >= 7 && i % 2 == 0) { crossDayMonth += 31; }

                    //小月的情况
                    else { crossDayMonth += 30; }
                }




                int crossDays = crossDayMonth + crossDayOfYear;//crossDays:月份加年份一共有多少天
                int dayOfweek = crossDays % 7 + 1;//计算是星期几
                int space = dayOfweek - 1;//日历前面的空白数量



                for (int i = 0; i < space; i++)
                {
                    dates.Add("");//循环space次,并添加space次空白
                }




                //年和月计算了,现在计算用户输入的几号的天数
                int days;
                if (month == 2)
                {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { days = 29; }
                    else { days = 28; }
                }
                else if (month <= 7 && month % 2 != 0 || month >= 7 && month % 2 == 0) { days = 31; }
                else
                {
                    days = 30;
                }

                //添加几号进集合
                for (int i = 1; i < days; i++)
                {
                    dates.Add(i.ToString());

                }



                //输出
                Console.WriteLine("*********************************************************");
                Console.Write("一\t二\t三\t四\t五\t六\t日");
                for (int i = 0; i < dates.Count; i++)
                {
                    //到7位是换行,日历一排七位
                    if (i % 7 == 0) { Console.WriteLine("\t"); }
                    Console.Write(dates[i] + "\t");
                }
                Console.WriteLine();
                Console.WriteLine("*********************************************************");


                Console.WriteLine("按回车键继续");
                Console.ReadLine();
                Console.Clear();
            }
        }
    }
}
View Code

 

posted @ 2017-08-19 17:13  I丶Promise  阅读(193)  评论(0编辑  收藏  举报