您的位置:首页 > 编程语言 > Go语言

Print the numbers of form 2^i.5^j in increasing order -- Google

2013-07-04 11:30 399 查看
Problem

Print the numbers of form 2^i.5^j in increasing order. For eg: 
1, 2, 4, 5, 8, 10, 16, 20

Solution

using System;

using System.Collections.Generic;


namespace JamesChen

{

    class PrintNumIncreasingly

    {

        static void PrintIncreasingNums(int n)

        {

            if (n < 0) return;

            List<int> a = new List<int>();

            a.Add(1);


            int count2 = 0;  

            int count5 = 0;

            

            while (a.Count < n)

            {

                if (a[count2] * 2 < a[count5] * 5)

                {

                    a.Add(a[count2] * 2);

                    count2++;

                }

                else if (a[count2] * 2 > a[count5] * 5)

                {

                    a.Add(a[count5] * 5);

                    count5++;

                }

                else

                {

                    a.Add(a[count5] * 5);

                    count2++;

                    count5++;

                }

            }


            foreach (var i in a)

            {

                Console.Write("{0, 6}", i);

            }

            Console.WriteLine();

        }


        static void Main(string[] args)

        {

            PrintIncreasingNums(20);

        }

    }

}


Output

     1     2     4     5     8    10    16    20    25    32    40    50    64    80   100   125   128   160   200   250

Press any key to continue . . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  array