2021年4月27日 星期二

2.簡單小程式 - 瘋狂程式

A007:最大公因數之公因數最大法

題目

最大公因數之公因數最大法


輸入兩個整數 x y,輸出 x , y 的最大公因數。

                Console.WriteLine("輸入兩個數");
                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());
                a = (a > 0) ? a : -a;
                b = (b > 0) ? b : -b;
                if (b>a)
                {
                    int temp = a;
                    a = b;
                    b = temp;
                }
                while (b > 0)
                {
                    int r = a % b;
                    a = b;
                    b = r;
                }
                Console.WriteLine("最大公因數為{0}\n", a);

 

M90H017:最小公倍數(2)

題目

最小公倍數(2)


輸入2整數,輸出其最小正公倍數。

                Console.WriteLine("輸入兩個數");
                int a = Math.Abs(Convert.ToInt32(Console.ReadLine())), max = 0;
                int b = Math.Abs(Convert.ToInt32(Console.ReadLine())), min = 0;
                if (b > a)
                {
                    int temp = a;
                    max = a = b;
                    min = b = temp;
                }
                while (min > 0)
                {
                    int r = max % min;
                    max = min;
                    min = r;
                }
                Console.WriteLine("最小公倍數為{0}\n", a*b/max);

 

A009:質數判別

題目

質數判別


輸入一個正整數,如果是質數,則輸出 Yes,如果不是,則輸出 No

                double n = Convert.ToDouble(Console.ReadLine());
                double Sqrt = Math.Ceiling(Math.Sqrt(n));
                bool Prime = true;
                for (int i = 2; i <= (int)Sqrt; i++)
                {
                    if (n % i == 0)
                    {
                        Prime = false;
                        break;
                    }
                }
                if (Prime) Console.WriteLine("yes 是質數");
                else Console.WriteLine("no 不是質數");

 

A020:韓信點兵

題目

韓信點兵


韓信點兵 7個一數剩3, 9個一數剩4, 10個一數剩2, 11個一數剩1個 請問韓信兵團至少有多少人?

                for (int i = 0; ;i++ )
                {
                    if (i % 7 == 3 && i % 9 == 4 && i % 10 == 2 && i % 11 == 1)
                    {
                        Console.WriteLine(i);
                        break;
                    }
                }

 

A024:級數

題目

級數


輸入一個正整數N 。輸出1*3+3*5+5*7+...+(2N-1)(2N+1)之值。

                int sum = 0, n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                    sum += (2 * i - 1) * (2 * i + 1);
                Console.WriteLine(sum);

 

A045:大樂透包牌

(所有子集合)

題目

大樂透包牌


大樂透從149號中選6個號碼,開獎時共開出6個號碼及1個特別號,6個號碼全中者得頭獎。 阿平每期計算明牌,挑出8個號碼,想要將所有由這8個號碼所組的6個號碼全部簽。 請你設計程式供阿平輸入此8個號碼,然後印出所有的簽牌可能。

                List<int> Num = new List<int>();
                for (int i = 0; i < 8; i++)
                    Num.Add(Convert.ToInt32(Console.ReadLine()));
                Num.Sort();
                for (int i = 0; i < 8; i++)
                {
                    for (int j = i + 1; j < 8; j++)
                    {
                        for (int k = 0; k < 8; k++)
                            if (k != i && k != j)
                                Console.Write("-{0}", Num[k]);
                        Console.WriteLine();
                    }
                }

 

F024:23的倍數

題目

23的倍數


輸入一整數n,輸出比n小的正數且是(2的倍數或3的倍數)

                 int n = Convert.ToInt32(Console.ReadLine()), line = 0;
                for (int i = 1; i <= n; i++)
                {
                    if (i % 2 == 0 || i % 3 == 0)
                    {
                        Console.Write("{0} ",i);
                        line++;
                    }
                    if (line % 10 == 0) Console.WriteLine();
                }

 

 


沒有留言:

張貼留言

WinFormTb02

https://drive.google.com/drive/u/0/folders/1UwS9FZ3ELCOK6SAwirHrkxq3z_RSbxJt https://www.youtube.com/watch?v=k7IkIeww_U0&list=PLumjEWemD...