代码改变世界

正能量

2013-02-01 14:39 by leo.wl2020, ... 阅读, ... 评论, 收藏, 编辑

     * 1.01的365次方=37.78343433289 >>>1;
     * 1的365次方=1;
     * 0.99的365次方= 0.02551796445229 <<<1;
     * 1.01=1+0.01,也就是每天进步一点,1.01的365次方也就是说你每天进步一点,一年以后,你将进步很大,远远大于“1”;
     * 1是指原地踏步,一年以后你还是原地踏步,还是那个“1”;
     * 0.99=1-0.01,也就是说你每天退步一点点,你将在一年以后,远远小于“1”,远远被人抛在后面,将会是“1”事无成。

   power

  参考:http://www.cnblogs.com/wzyl/archive/2013/01/31/2883890.html  算法,具体算法思路,还没细看。

  用C#实现试试看看怎么样.

  代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPower
{
    /*
     * 1.01的365次方=37.78343433289 &gt;&gt;&gt;1;
     * 1的365次方=1;
     * 0.99的365次方= 0.02551796445229 &lt;&lt;&lt;1;
     * 1.01=1+0.01,也就是每天进步一点,1.01的365次方也就是说你每天进步一点,一年以后,你将进步很大,远远大于“1”;
     * 1是指原地踏步,一年以后你还是原地踏步,还是那个“1”;
     * 0.99=1-0.01,也就是说你每天退步一点点,你将在一年以后,远远小于“1”,远远被人抛在后面,将会是“1”事无成。
     */
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var v1 = Math.Pow(1.01,365);
                Console.WriteLine(string.Format(&quot;1.01的365次方={0}&quot;, v1));
                var v2 = Math.Pow(0.99,365);
                Console.WriteLine(string.Format(&quot;0.99的365次方={0}&quot;, v2));
                Console.WriteLine(&quot;*******************************&quot;);
                var v3 = GetFinalResult(&quot;365&quot;,&quot;1.01&quot;);                
                Console.WriteLine(string.Format(&quot;1.01的365次方={0}&quot;, v3));
                var v4 = GetFinalResult(&quot;365&quot;,&quot;0.99&quot;);
                Console.WriteLine(string.Format(&quot;0.99的365次方={0}&quot;, v4));
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(&quot;Ex:&quot; + ex.Message + &quot; &quot; + ex.StackTrace);
            }
        }

        static string GetFinalResult(string num, string d)
        {
            int number = int.Parse(num);
            if (number &lt;= 1) return d;
            else if (number == 2) return GetMultiplicativeResult(d, d);
            else
            {
                string s = (number - 1).ToString();
                return GetMultiplicativeResult(GetFinalResult(s,d), d);
            }
        }

        static string GetMultiplicativeResult(string double1, string double2)
        {
           //参数合法性检查略
            //去除正负号和小数点
            string first = double1.TrimStart('-').Replace(&quot;.&quot;, &quot;&quot;);
            string second = double2.TrimStart('-').Replace(&quot;.&quot;, &quot;&quot;);
            //计算
            int len = first.Length + second.Length;
            int[] res = new int[len];//存储结果
            int t1; int t2; int t3; int SureChar; int JinWeiChar;
            for (int i = second.Length - 1; i &gt;= 0; i--)
            {
                JinWeiChar = 0;
                for (int j = first.Length - 1; j &gt;= 0; j--)
                {
                    t1 = Int32.Parse(second[i].ToString());
                    t2 = Int32.Parse(first[j].ToString());
                    t3 = t1 * t2 + JinWeiChar + res[i + j + 1];
                    SureChar = t3 % 10;
                    JinWeiChar = t3 / 10;
                    res[j + i + 1] = SureChar;
                    if (j == 0)
                    {
                        res[j + i] = JinWeiChar;
                    }
                }

            }
            //整型数组变字符串方便后面操作
            StringBuilder sb = new StringBuilder();
            foreach (int i in res)
            {
                sb.Append(i.ToString());
            }
            //算小数点
            int dot = 0;
            if (double1.Contains('.'))
                dot += double1.Length - 1 - double1.IndexOf('.');
            if (double2.Contains('.'))
                dot += double2.Length - 1 - double2.IndexOf('.');
            sb.Insert(len - dot, &quot;.&quot;);
            //去多余的0
            string result = sb.ToString();
            string[] arr = result.Split('.');
            arr[0] = arr[0].TrimStart('0');
            arr[1] = arr[1].TrimEnd('0');
            if (string.IsNullOrEmpty(arr[0]))
                arr[0] = &quot;0&quot;;
            result = arr[0].ToString() + &quot;.&quot; + arr[1].ToString();
            //加正负号
            if (double1.StartsWith(&quot;-&quot;) ^ double2.StartsWith(&quot;-&quot;))
                result = &quot;-&quot; + result;
            return result.Trim('.');  
        }
    }
}

结果:

QQ截图20130201143113