{"id":295745,"date":"2024-05-20T15:43:08","date_gmt":"2024-05-20T07:43:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/295745.html"},"modified":"2024-05-20T15:43:31","modified_gmt":"2024-05-20T07:43:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8c%e6%88%96c%e5%ae%9e%e7%8e%b0%e5%a4%9a%e4%bd%8d%e6%95%b0%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295745.html","title":{"rendered":"\u5982\u4f55\u7528c#\u6216c\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27142959\/eb051bd3-1bc2-4523-b1db-9b41a4215aee.webp\" alt=\"\u5982\u4f55\u7528c#\u6216c\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97\" \/><\/p>\n<p><p>\u8981\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97\uff08\u7279\u522b\u662f\u8d85\u8fc7\u7f16\u7a0b\u8bed\u8a00\u76f4\u63a5\u63d0\u4f9b\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u80fd\u591f\u5b58\u50a8\u7684\u8303\u56f4\uff09, \u5728C#\u6216C\u8bed\u8a00\u4e2d, \u4e3b\u8981\u65b9\u5f0f\u5305\u62ec<strong>\u4f7f\u7528\u5927\u6570\u5b57\u5e93\u3001\u624b\u52a8\u5b9e\u73b0\u5927\u6570\u7b97\u6cd5\u3001\u5229\u7528\u73b0\u6709\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u6765\u6a21\u62df\u5927\u6570\u7684\u5b58\u50a8\u548c\u8ba1\u7b97<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u9700\u8981\u7075\u6d3b\u8fd0\u7528\u7f16\u7a0b\u6280\u5de7\u548c\u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u6765\u89e3\u51b3\u5927\u6570\u7684\u52a0\u51cf\u4e58\u9664\u7b49\u57fa\u672c\u8fd0\u7b97\u95ee\u9898\u3002\u7279\u522b\u5730\uff0c<strong>\u5229\u7528\u73b0\u6709\u7684\u6570\u636e\u7ed3\u6784\u6765\u6a21\u62df\u5927\u6570\u7684\u5b58\u50a8\u548c\u8ba1\u7b97<\/strong>\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u5982\u4f55\u5904\u7406\u5927\u6570\u8fd0\u7b97\u7684\u7ec6\u8282\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u589e\u5f3a\u5bf9\u6570\u636e\u7ed3\u6784\u5982\u6570\u7ec4\u548c\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5927\u6570\u5b57\u5e93<\/h3>\n<\/p>\n<p><p>\u5728C#\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528.NET\u6846\u67b6\u63d0\u4f9b\u7684<code>System.Numerics.BigInteger<\/code>\u7c7b\u6765\u8fdb\u884c\u5927\u6570\u8fd0\u7b97\u3002\u8fd9\u4e2a\u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\u6765\u652f\u6301\u5927\u6574\u6570\u7684\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u4ee5\u53ca\u6a21\u8fd0\u7b97\u7b49\u64cd\u4f5c\u3002\u4f7f\u7528<code>BigInteger<\/code>\u975e\u5e38\u7b80\u5355\uff0c\u9996\u5148\u9700\u8981\u5f15\u5165<code>System.Numerics<\/code>\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u7136\u540e\u5c31\u53ef\u4ee5\u521b\u5efa<code>BigInteger<\/code>\u5b9e\u4f8b\uff0c\u5e76\u6267\u884c\u5404\u79cd\u8fd0\u7b97\u4e86\u3002\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">using System;<\/p>\n<p>using System.Numerics;<\/p>\n<p>class Program<\/p>\n<p>{<\/p>\n<p>    static void M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()<\/p>\n<p>    {<\/p>\n<p>        BigInteger bigNum1 = BigInteger.Parse(&quot;12345678901234567890&quot;);<\/p>\n<p>        BigInteger bigNum2 = BigInteger.Parse(&quot;98765432109876543210&quot;);<\/p>\n<p>        BigInteger sum = bigNum1 + bigNum2;<\/p>\n<p>        Console.WriteLine($&quot;Sum: {sum}&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eC\u8bed\u8a00\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684\u5927\u6570\u5904\u7406\u5e93\uff0c\u5982GMP\uff08GNU Multiple Precision Arithmetic Library\uff09\u3002GMP\u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5927\u6570\u8fd0\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u5904\u7406\u5927\u6570\u8fd0\u7b97\u3002\u5728\u4f7f\u7528GMP\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u4e0b\u8f7d\u5e76\u5b89\u88c5GMP\u5e93\u5230\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u624b\u52a8\u5b9e\u73b0\u5927\u6570\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60f3\u6df1\u5165\u4e86\u89e3\u5927\u6570\u8fd0\u7b97\u7684\u5185\u90e8\u673a\u5236\uff0c\u6216\u8005\u5728\u4e00\u4e9b\u7279\u5b9a\u73af\u5883\u4e0b\u9700\u8981\u81ea\u63a7\u5236\u5927\u6570\u8fd0\u7b97\u7684\u7ec6\u8282\uff0c\u90a3\u4e48\u624b\u52a8\u5b9e\u73b0\u5927\u6570\u7b97\u6cd5\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5728C#\u6216C\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u6765\u8868\u793a\u5927\u6570\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u62df\u4eba\u7c7b\u8fdb\u884c\u8ba1\u7b97\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u7b49\u57fa\u672c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u52a0\u6cd5\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u52a0\u6cd5\u662f\u6700\u57fa\u672c\u7684\u8fd0\u7b97\u4e4b\u4e00\u3002\u5b9e\u73b0\u5927\u6570\u52a0\u6cd5\u7684\u5173\u952e\u662f\u5904\u7406\u597d\u8fdb\u4f4d\u95ee\u9898\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u4ece\u4e24\u4e2a\u5927\u6570\u7684\u6700\u4f4e\u4f4d\uff08\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6700\u53f3\u7aef\u6216\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u5f00\u59cb\uff0c\u9010\u4f4d\u76f8\u52a0\uff0c\u5e76\u5c06\u8fdb\u4f4d\u4f20\u9012\u5230\u4e0b\u4e00\u4f4d\u7684\u8ba1\u7b97\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public string Add(string num1, string num2)<\/p>\n<p>{<\/p>\n<p>    StringBuilder result = new StringBuilder();<\/p>\n<p>    int carry = 0; \/\/ \u8fdb\u4f4d<\/p>\n<p>    int i = num1.Length - 1;<\/p>\n<p>    int j = num2.Length - 1;<\/p>\n<p>    while (i &gt;= 0 || j &gt;= 0 || carry &gt; 0)<\/p>\n<p>    {<\/p>\n<p>        int sum = carry;<\/p>\n<p>        if (i &gt;= 0) sum += num1[i--] - &#039;0&#039;;<\/p>\n<p>        if (j &gt;= 0) sum += num2[j--] - &#039;0&#039;;<\/p>\n<p>        carry = sum \/ 10;<\/p>\n<p>        result.Insert(0, sum % 10);<\/p>\n<p>    }<\/p>\n<p>    return result.ToString();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728C\u4e2d\u5b9e\u73b0\u65f6\uff0c\u539f\u7406\u76f8\u540c\uff0c\u53ea\u662f\u5b57\u7b26\u4e32\u548c\u6570\u7ec4\u7684\u64cd\u4f5c\u4f1a\u6d89\u53ca\u5230\u66f4\u591a\u7684\u6307\u9488\u64cd\u4f5c\u548c\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u73b0\u6709\u6570\u636e\u7ed3\u6784\u6a21\u62df\u5927\u6570\u5b58\u50a8\u548c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u5728C#\u8fd8\u662fC\u4e2d\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u6765\u5b58\u50a8\u5927\u6570\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u64cd\u4f5c\uff0c\u6765\u6a21\u62df\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u7b49\u8fd0\u7b97\u3002\u8fd9\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5927\u6570\u8fd0\u7b97\u7684\u539f\u7406\uff0c\u8fd8\u80fd\u589e\u5f3a\u6211\u4eec\u5bf9\u5b57\u7b26\u4e32\u548c\u6570\u7ec4\u7684\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>\u4e58\u6cd5\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u4e58\u6cd5\u53ef\u4ee5\u770b\u4f5c\u591a\u6b21\u52a0\u6cd5\u3002\u53ef\u4ee5\u91c7\u7528\u201c\u957f\u4e58\u6cd5\u201d\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5927\u6570\u7684\u4e58\u6cd5\u8fd0\u7b97\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c31\u662f\u5bf9\u4e58\u6570\u7684\u6bcf\u4e00\u4f4d\u4e0e\u88ab\u4e58\u6570\u76f8\u4e58\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u6309\u6b63\u786e\u7684\u4f4d\u7f6e\u76f8\u52a0\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u6570\u5b57\u7684\u4f4d\u7f6e\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public string Multiply(string num1, string num2)<\/p>\n<p>{<\/p>\n<p>    int[] pos = new int[num1.Length + num2.Length];<\/p>\n<p>    for (int i = num1.Length - 1; i &gt;= 0; i--)<\/p>\n<p>    {<\/p>\n<p>        for (int j = num2.Length - 1; j &gt;= 0; j--)<\/p>\n<p>        {<\/p>\n<p>            int mul = (num1[i] - &#039;0&#039;) * (num2[j] - &#039;0&#039;);<\/p>\n<p>            int sum = mul + pos[i + j + 1];<\/p>\n<p>            pos[i + j] += sum \/ 10;<\/p>\n<p>            pos[i + j + 1] = sum % 10;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    StringBuilder result = new StringBuilder();<\/p>\n<p>    foreach (int p in pos)<\/p>\n<p>    {<\/p>\n<p>        if (!(result.Length == 0 &amp;&amp; p == 0))<\/p>\n<p>        {<\/p>\n<p>            result.Append(p);<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    return result.Length == 0 ? &quot;0&quot; : result.ToString();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0\u601d\u8def\u5b8c\u5168\u4e00\u6837\uff0c\u53ea\u662f\u5728\u64cd\u4f5c\u5b57\u7b26\u4e32\u548c\u6570\u7ec4\u65f6\uff0c\u8981\u6ce8\u610f\u7ec6\u8282\u548c\u5185\u5b58\u7ba1\u7406\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u65b9\u6cd5\u4e2d\uff0c\u6bcf\u79cd\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5927\u6570\u5b57\u5e93\u8fd8\u662f\u624b\u52a8\u5b9e\u73b0\u5927\u6570\u7b97\u6cd5\uff0c\u5173\u952e\u90fd\u5728\u4e8e\u7406\u89e3\u5927\u6570\u8fd0\u7b97\u7684\u539f\u7406\u548c\u5bf9\u6570\u636e\u7ed3\u6784\u7684\u7075\u6d3b\u8fd0\u7528\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728C#\u6216C\u8bed\u8a00\u4e2d\u6709\u6548\u5730\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u591a\u4f4d\u6570\u8fd0\u7b97\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u591a\u4f4d\u6570\u8fd0\u7b97\u662f\u6307\u5bf9\u8d85\u8fc7\u4e00\u4f4d\u6570\u5b57\u7684\u6570\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u7b49\u3002\u5728C#\u6216C\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u548c\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97\u3002<\/p>\n<p><strong>2. \u5728C#\u6216C\u4e2d\uff0c\u5982\u4f55\u8868\u793a\u548c\u5904\u7406\u591a\u4f4d\u6570\uff1f<\/strong><\/p>\n<p>\u5728C#\u6216C\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u6765\u8868\u793a\u591a\u4f4d\u6570\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u8fd0\u7b97\u3002\u5bf9\u4e8e\u52a0\u6cd5\u548c\u51cf\u6cd5\u8fd0\u7b97\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e24\u4e2a\u591a\u4f4d\u6570\u5b57\u7b26\u4e32\u6309\u4f4d\u8fdb\u884c\u8fd0\u7b97\uff0c\u9010\u4f4d\u76f8\u52a0\u6216\u76f8\u51cf\uff1b\u5bf9\u4e8e\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u8fdb\u4f4d\/\u501f\u4f4d\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u8fd0\u7b97\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u6216\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u591a\u4f4d\u6570\u8fd0\u7b97\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cC#\u6216C\u4e2d\u6709\u4e00\u4e9b\u73b0\u6210\u7684\u5e93\u6216\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u591a\u4f4d\u6570\u8fd0\u7b97\uff0c\u5982BigInteger\u7c7b\uff08C#\uff09\u6216GMP\u5e93\uff08C\uff09\u3002\u8fd9\u4e9b\u5e93\u6216\u5de5\u5177\u5df2\u7ecf\u5b9e\u73b0\u4e86\u9ad8\u7cbe\u5ea6\u7684\u591a\u4f4d\u6570\u8fd0\u7b97\u7b97\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u3002\u4f60\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u6587\u6863\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6216\u5de5\u5177\u6765\u8fdb\u884c\u591a\u4f4d\u6570\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b9e\u73b0\u591a\u4f4d\u6570\u8fd0\u7b97\uff08\u7279\u522b\u662f\u8d85\u8fc7\u7f16\u7a0b\u8bed\u8a00\u76f4\u63a5\u63d0\u4f9b\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u80fd\u591f\u5b58\u50a8\u7684\u8303\u56f4\uff09, \u5728C#\u6216C\u8bed\u8a00\u4e2d, \u4e3b\u8981\u65b9\u5f0f\u5305\u62ec [&hellip;]","protected":false},"author":3,"featured_media":295775,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295745"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=295745"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295775"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}