close
- 1. 請設計一個程式,計算下面數列的和並印出完整的數列。(提示:先分析分子和分母變化的規則性。分母都是偶數,分子恰好是前一項的分子和分母相加。)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { double i = 1, sum = 0; int j = 2; for (j = 2; j <= 98; j += 2) { sum += i / j; Console.Write("({0}/{1})+", i, j); i = i + j; } Console.Write("({0}/{1})={2}", i, j, sum + (i / j)); Console.Read(); } } }
- 2. 在《孫子算經》裡有個著名的「孫子問題」:「今有物不知其數,三三數之剩二,五五數之剩三,七七數之剩一,問物幾何?」若把它翻譯成白話,便是:有一堆東西不知道有幾個;三個三個數它,剩餘二個;五個五個數它,剩餘三個;七個七個數它,剩餘一個;問這堆東西有幾個?
請設計一個程式:由鍵盤輸入一個界限值n,計算小於等於n的整數中,哪些數字是符合孫子算經,以每列印5個,並計算共有多少個。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int n; int count=0; n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { if (i % 3 == 2 && i % 5 == 3 && i % 7 == 1) { Console.Write("{0},",i); count++; if (count % 5 == 0) { Console.Write("\n"); } } } Console.Read(); } } }
- 3. 請設計一個程式:由鍵盤輸入一個界限值n,計算並印出小於等於n的質數,以每列印5個,並計算共有多少個。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { /* * 3. 請設計一個程式:由鍵盤輸入一個界限值n,計算並印出小於等於n的質數,以每列印5個,並計算共有多少個。 */ Console.Write("請輸入一數: "); int number; number = int.Parse(Console.ReadLine()); //判斷質數 int countp =0; for (int p = 1; p <= number; p++) { //int.Parse(Console.ReadLine()); int count = 0; for (int i = 1; i <= p; i++) { if (p % i == 0) { count++; //紀錄有幾個整除(也就是有幾個因式) } } if (count == 2) //假如因數個數剛好等於2則把質數印出 { countp += 1; Console.Write("{0} ", p); if (countp % 5 == 0) //每5個換行 { Console.Write("\n"); } } } Console.WriteLine("\n"+ countp + "個"); //1跟他自己 先判斷是不是質數 Console.Read(); } } }
全站熱搜