經(jīng)典實例
綜合實例
水仙花數(shù)(Narcissistic number):
也被稱為超完全數(shù)字不變數(shù)(pluperfect digital invariant, PPDI)、自戀數(shù)、自冪數(shù)、阿姆斯壯數(shù)或阿姆斯特朗數(shù)(Armstrong number),水仙花數(shù)是指一個 n 位數(shù)(n≥3 ),它的每個位上的數(shù)字的 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 水仙花數(shù) { class Program { static void Main(string[] args) { Console.WriteLine("請按任意鍵繼續(xù):"); Console.ReadLine(); Console.WriteLine("100到1000之間的水仙花數(shù)為:"); 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) //判斷是否為水仙花數(shù) { Console.WriteLine(i); } } Console.ReadKey(); } } }
桃子問題(經(jīng)典問題):
條件桃子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) { //提示并儲存輸入的數(shù)字 Console.WriteLine("請輸入你的金錢(桃子3元一個):"); int input = int.Parse(Console.ReadLine()); //把輸入給錢,單價定義為3,定義exchan:1次機(jī)會等于3個桃核 int money = input, price = 3, exchange = 3; int eated, kernel; //eated:第一次換取得數(shù)量 //kerner:第一次吃完后的桃核數(shù)量 eated = money / price; kernel = eated; //當(dāng)kerner大于機(jī)會的要求時,進(jìn)入循環(huán) while (kernel >= exchange) { int peach = kernel / exchange; //peach:用桃核換取得桃子數(shù)量 int left = kernel % exchange; //left:換桃子后的桃核余數(shù)(肯定小于3) eated += peach; //累加吃的桃子數(shù)量 kernel = left + peach; //累加桃核數(shù)量 } Console.WriteLine("最多可以吃到" + eated + "個桃子"); //輸出 } } } }
哥德巴赫猜想(世界近代三大數(shù)學(xué)難題之一):
哥德巴赫1742年給歐拉的信中哥德巴赫提出了以下猜想:任一大于2的偶數(shù)都可寫成兩個質(zhì)數(shù)之和。但是哥德巴赫自己無法證明它,于是就寫信請教赫赫有名的大數(shù)學(xué)家歐拉幫忙證明,但是一直到死,歐拉也無法證明。[1] 因現(xiàn)今數(shù)學(xué)界已經(jīng)不使用“1也是素數(shù)”這個約定,原初猜想的現(xiàn)代陳述為:任一大于5的整數(shù)都可寫成三個質(zhì)數(shù)之和。歐拉在回信中也提出另一等價版本,即任一大于2的偶數(shù)都可寫成兩個質(zhì)數(shù)之和。今日常見的猜想陳述為歐拉的版本。把命題"任一充分大的偶數(shù)都可以表示成為一個素因子個數(shù)不超過a個的數(shù)與另一個素因子不超過b個的數(shù)之和"記作"a+b"。1966年陳景潤證明了"1+2"成立,即"任一充分大的偶數(shù)都可以表示成二個素數(shù)的和,或是一個素數(shù)和一個半素數(shù)的和"。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 買桃問題 { class Program { static void Main(string[] args) { while (true) { //提示并儲存輸入的數(shù)字 Console.WriteLine("請輸入你的金錢(桃子3元一個):"); int input = int.Parse(Console.ReadLine()); //把輸入給錢,單價定義為3,定義exchan:1次機(jī)會等于3個桃核 int money = input, price = 3, exchange = 3; int eated, kernel; //eated:第一次換取得數(shù)量 //kerner:第一次吃完后的桃核數(shù)量 eated = money / price; kernel = eated; //當(dāng)kerner大于機(jī)會的要求時,進(jìn)入循環(huán) while (kernel >= exchange) { int peach = kernel / exchange; //peach:用桃核換取得桃子數(shù)量 int left = kernel % exchange; //left:換桃子后的桃核余數(shù)(肯定小于3) eated += peach; //累加吃的桃子數(shù)量 kernel = left + peach; //累加桃核數(shù)量 } Console.WriteLine("最多可以吃到" + eated + "個桃子"); //輸出 } } } }
斐波那契數(shù)列(Fibonacci sequence):
又稱黃金分割數(shù)列、因數(shù)學(xué)家列昂納多·斐波那契(Leonardoda Fibonacci)以兔子繁殖為例子而引入,故又稱為“兔子數(shù)列”,指的是這樣一個數(shù)列:1、1、2、3、5、8、13、21、34、……在數(shù)學(xué)上,斐波納契數(shù)列以如下被以遞歸的方法定義:F(0)=1,F(xiàn)(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)在現(xiàn)代物理、準(zhǔn)晶體結(jié)構(gòu)、化學(xué)等領(lǐng)域,斐波納契數(shù)列都有直接的應(yīng)用,為此,美國數(shù)學(xué)會從1963起出版了以《斐波納契數(shù)列季刊》為名的一份數(shù)學(xué)雜志,用于專門刊載這方面的研究成果。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 斐波拉數(shù)列 { class Program { static void Main(string[] args) { //獲取用戶輸入的長度 Console.WriteLine("請輸入斐波拉數(shù)列的長度:"); int input = int.Parse(Console.ReadLine()); //創(chuàng)建一個數(shù)組 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"); } //數(shù)組索引為i的元素值=數(shù)組索引為i-1的值+數(shù)組索引為i-2的值 else { arr[i] = arr[i - 1] + arr[i - 2]; Console.Write(arr[i] + "\t"); } } Console.ReadLine(); } } }
分解質(zhì)因數(shù):
每個合數(shù)都可以寫成幾個質(zhì)數(shù)相乘的形式,其中每個質(zhì)數(shù)都是這個合數(shù)的因數(shù),叫做這個合數(shù)的分解質(zhì)因數(shù)。 分解質(zhì)因數(shù)只針對合數(shù)。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 分解質(zhì)因數(shù) { class Program { static void Main(string[] args) { Console.WriteLine("請輸入非質(zhì)數(shù)后回車!"); int inNumber = int.Parse(Console.ReadLine());//將輸入轉(zhuǎn)換為int類型 string texRet = inNumber.ToString() + " = ";//將inNumber轉(zhuǎn)換為字符串+等號 //i為被除數(shù),一直增加,直到找到能被除的數(shù) for (int i = 2; i <= inNumber; i++) { if (inNumber % i == 0) { texRet += i.ToString() + "*";//texRet=texRet+i轉(zhuǎn)換成字符串+*號 inNumber = inNumber / i;//將inNumber重新取值 i--; } } texRet = texRet.Substring(0, texRet.Length - 1); Console.WriteLine(texRet); Console.ReadKey(); /* while (true) { Console.WriteLine("請輸入一個非質(zhì)數(shù):"); Console.ReadLine(); int num = Convert.ToInt32(Console.ReadLine()); for (int i = 2; i < num; i++)//循環(huán) 1沒有質(zhì)因數(shù) { while (num != i)//num==i的話跳出循環(huán)當(dāng)然num==i你也進(jìn)不去 { if (num % i == 0)//num%i為0就繼續(xù)執(zhí)行 { Console.Write(num+"="+i + "*");//輸出質(zhì)數(shù)i的值 num /= i; } else { break; } } } Console.WriteLine(num);//這個是跳出循環(huán)的值,沒得求了,不跳出來不行啊,當(dāng)然也是質(zhì)數(shù) } */ } } }
反序輸出:
用戶輸入字符串后,字母反序輸出。
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 = ""; //從大到小循環(huán) for (int i = input.Length - 1; i >= 0; i--) { //索引從大到小存入新字符串內(nèi) reverse = reverse + input[i]; } Console.WriteLine(reverse + ":串字符個一出輸請"); Console.ReadLine(); } } }
日歷控制臺:
C#基本的知識實現(xiàn)日歷的輸出
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; //死循環(huán)只有全部輸對才能跳出進(jìn)行下一步 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;//跳出循環(huán) } } } //定義一個集合 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;//并且累加天數(shù) } } //計算從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;//日歷前面的空白數(shù)量 for (int i = 0; i < space; i++) { dates.Add("");//循環(huán)space次,并添加space次空白 } //年和月計算了,現(xiàn)在計算用戶輸入的幾號的天數(shù) 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; } //添加幾號進(jìn)集合 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("按回車鍵繼續(xù)"); Console.ReadLine(); Console.Clear(); } } } }
浙公網(wǎng)安備 33010602011771號