這其實也是昨天的實訓,是我太懶了,唉!
第1關:九九乘法口訣

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch601 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); if (n>=1 && n<=9) { for (int j = 1; j <= n; j++) { for (int i = 1; i <= j; i++) { Console.Write("{0}*{1}={2}\t", j, i ,i * j); } Console.WriteLine(); } } else { Console.WriteLine("input error!"); } Console.ReadKey(); /*******end********/ } } }
第2關:多重循環-求階乘之和

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch602 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); int x = 1, sum = 0; for (int i = 1; i <= n; i++) { x *= i; sum += x; } Console.WriteLine(sum); Console.ReadKey(); /*******end********/ } } }
第3關:多重循環-求n以內所有素數的和

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch603 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); int sum = 0; for (int i = 2; i <= n; i++) { bool b = true; for (int j = 2; j < i; j++) //判斷當前判斷的數字是不是質數 { if (i % j == 0) //說明不是質數 { b = false; break; } } if (b == true) { sum += i; } } Console.WriteLine($"素數的和:{sum}"); Console.ReadKey(); /*******end********/ } } }
第4關:多重循環-尋找完數

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch604 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); int r, m, j = 1; do { m = 0; for (int i = 1; i < j; i++) { r = j % i; if ((r == 0) == true) { m = m + i; } } if ((m == j) == true) { Console.WriteLine(j); } j++; } while (j < n); /*******end********/ } } }
時間匆忙,不過應該不會錯的,我還要刷劇呢!