2017年6月6日 星期二

(4.5.6)瘋狂程設->CPE顆星廣場

04.The 3n + 1 problem
考慮以下的演算法:

1.        
輸入 n
2.        
印出 n
3.        
如果 n = 1 結束
4.        
如果 n 是奇數 那麼 n=3*n+1
5.        
否則 n=n/2
6.         GOTO 2

例如輸入 22, 得到的數列: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

據推測此演算法對任何整數而言會終止 (當列印出 1 的時候)。雖然此演算法很簡單,但以上的推測是否真實卻無法知道。然而對所有的n ( 0 < n < 1,000,000 )來說,以上的推測已經被驗證是正確的。

給一個輸入 n ,透過以上的演算法我們可以得到一個數列(1作為結尾)。此數列的長度稱為ncycle-length。上面提到的例子, 22 cycle length 16.

問題來了:對任2個整數ij我們想要知道介於ij(包含ij)之間的數所產生的數列中最大的 cycle length 是多少。
Input
輸入可能包含了好幾列測試資料,每一列有一對整數資料 ij  
0< ij < 1,000,000
Output
對每一對輸入 i , j 你應該要輸出  i, j 和介於 i, j 之間的數所產生的數列中最大的 cycle length
Sample Input
1 10
10 1
100 200
201 210
900 1000
Sample Output
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174

static void Main(string[] args)
        {
            long Ni = Convert.ToInt64(Console.ReadLine());
            long Nj = Convert.ToInt64(Console.ReadLine());
            int N_length = 0;
            if (Nj > Ni)
            {
                long i = Nj;
                Nj = Ni;
                Ni = i;
            }
            while (Ni != Nj)
            {
                int cycle = 0;
                long x = Ni;
                while (x != 1)
                {
                    if (x % 2 == 0) x /= 2;
                    else x = (x * 3) + 1;
                    cycle++;
                }
                if (N_length < cycle) N_length = cycle;
                Ni--;
            }
            //N_length+1是因為當值等於1會跳出
            Console.WriteLine(N_length + 1);
        }

05. You can say 11
Introduction to the problem
Your job is, given a positive number N, determine if it is a multiple of eleven.
Description of the input
The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits.
  Description of the output
The output of the program shall indicate, for each input number, if it is a multiple of eleven or not.

  Sample input:
112233
30800
2937
323455693
5038297
112234
0

 Sample output
112233  is a multiple of 11.
30800  is a multiple of 11.
2937  is a multiple of 11.
323455693  is a multiple of 11.
5038297  is a multiple of 11.
112234  is not a multiple of 11.

static void Main(string[] args)
        {
            while (true)
            {
                char[] Chr = new char[20];
                int[] math = new int[2];
                string Input = Console.ReadLine();
                if (Input == "0") return;
                Chr = Input.ToCharArray();
                for (int i = 0; i < Input.ToCharArray().Length; i++)
                {
                    if (i % 2 == 0) math[0] = math[0] + Convert.ToInt32(Chr[i].ToString());
                    else math[1] = math[1] + Convert.ToInt32(Chr[i].ToString());
                }
                if (Math.Abs(math[1] - math[0]) % 11 == 0)
                    Console.WriteLine(Input + " is a multiple of 11.");
                else
                    Console.WriteLine(Input + " is not a multiple of 11.");
            }
        }
06.Bangla Numbers

問題
Bangla數通常會用'kuti' (10000000), 'lakh' (100000), 'hajar' (1000), 'shata' (100)這幾個字來把一個數值轉換成文字。你的任務就是寫一個程式來作這件事。


Input
輸入檔包含多筆測試資料,每筆測試資料都是一個不超過999999999999999的數字。

Output
對每一筆測試資料輸出一列轉換後的結果,每一列的開頭必須是一個佔四個字元的case number


Sample Input

23764
45897458973958

Sample Output

   1. 23 hajar 7 shata 64
   2. 45 lakh 89 hajar 7 shata 45 kuti 89 lakh 73 hajar 9 shata 58

測試用I/O
Input
0
1012
1000000000
100101001

Output

   1. 0
   2. 1 hajar 12
   3. 1 shata kuti
   4. 10 kuti 1 lakh 1 hajar 1

static void Main(string[] args)
        {
            while (true)
            {
                Input = Convert.ToInt64(Console.ReadLine());
                Calculation(Input);
                Console.WriteLine();
            }
        }

        static long Input = 0;
        static long kuti = -1, lakh = -1, hajar = -1, shata = -1;

        public static void Calculation(long input)
        {
            if (input >= 10000000)
            {
                kuti = input / 10000000;
                input = input % 10000000;
                if (kuti >= 100)
                {
                    Calculation(kuti);
                    Console.Write(" kuti ");
                }
                else Console.Write(kuti + " kuti ");
            }
            if (input >= 100000)
            {
                lakh = input / 100000;
                input = input % 100000;
                Console.Write(lakh + " lakh ");
            }
            if (input >= 1000)
            {
                hajar = input / 1000;
                input = input % 1000;
                Console.Write(hajar + " hajar ");
            }
            if (input >= 100)
            {
                shata = input / 100;
                input = input % 100;
                Console.Write(shata + " shata ");
            }
            if (input != 0) Console.Write(input);
        }

沒有留言:

張貼留言

WinFormTb02

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