C#阶乘
来源:互联网 发布:企业级网络规划与设计 编辑:程序博客网 时间:2024/05/16 10:48
昨晚练手用了两种方案。因为Int64也只能算到20的阶乘,就尝试写了下。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace jiecheng{ class Program { //数组法,速度快,但是还是会溢出 PS,LZ觉得可以结合字符串方法,用两个int字符串 如此便不会溢出 static void Main(string[] args) { string str = ""; str = System.Console.ReadLine(); Int64 c = Int64.Parse(str); Int64[] x = new Int64[1000]; bool a = false; x[999] = 1; for (Int64 m = 1; m <= c; m++) { for (int i = 0; i < 1000; i++) { x[i] = (Int64)(x[i] * m); } for (int i = 999; i > 0; i--) { x[i - 1] += (Int64)(x[i] / 100000000L); x[i] = x[i] % 100000000L; } } for (int i = 0; i < 1000; i++) { if (x[i] == 0&&!a) { continue; } if (!a) { a = true; System.Console.Write(x[i].ToString()); continue; } System.Console.Write(x[i].ToString().PadLeft(8, '0')); } System.Console.WriteLine(); System.Console.WriteLine(); //jiecheng(); } //字符串法,此方法优势在于不会溢出,缺点太复杂,占资源。 static void jiecheng() { //思路:利用string存放数据防止内存溢出,相乘思路:竖式 // 9999 // * 1111 // ---------- // 0009999 // 0099990 // 0999900 // 9999000 // ---------- // Result //如此每次由下向上算前两层式子的和(逐位相加),从而解决大数相乘问题 string str = "100";//str的阶乘 //str = System.Console.ReadLine(); string str_bc = "";//被乘数 string str_c = "1";//乘数 string str_s = "0";//竖式上一行的结果 string str_x = "0";//竖式最下一行的结果 string temp_sum = "";//临时存放str_x与str_s的相加结果 string sum = "1";//总和 int jin = 0;//竖式的进位标志 //阶乘循环 for (int m = 2; m <= int.Parse(str); m++) { str_bc = m.ToString();//将比较短的数据赋予被乘数...怕溢出,做乘时都是乘数逐位与被乘数相乘,so乘数的位数不用考虑 str_c = sum; //补位,为字符串尾部补"0" str_x = bu(int.Parse(str_bc) * int.Parse(str_c.ToCharArray()[0].ToString()) + "", false, str_c.Length - 1); temp_sum = ""; //乘法循环 for (int j = 1; j < str_c.Length; j++) { //补位,为字符串尾部补"0" str_s = bu(int.Parse(str_bc) * int.Parse(str_c.ToCharArray()[j].ToString()) + "", false, str_c.Length - 1 - j); if (str_s.Length < str_x.Length) {//补位,将短的字符串前部补"0",使两个字符串长度相等,方便逐位计算 str_s = bu(str_s, true, str_x.Length - str_s.Length); } //加法循环,逐位求和 for (int i = str_x.Length - 1; i >= 0; i--) { temp_sum = (jin + int.Parse(str_x.ToCharArray()[i].ToString()) + int.Parse(str_s.ToCharArray()[i].ToString())) % 10 + temp_sum; jin = (int)(jin + int.Parse(str_x.ToCharArray()[i].ToString()) + int.Parse(str_s.ToCharArray()[i].ToString())) / 10; } if (jin == 1) { temp_sum = jin + temp_sum; } jin = 0; str_x = temp_sum; temp_sum = ""; } sum = str_x; //System.Console.WriteLine(str_x); } System.Console.WriteLine(sum); //System.Console.WriteLine(sum.Length); } //补位函数 static string bu(string str, bool qian, int times) { for (int i = 0; i < times; i++) { if (!qian) { str = str + "0"; } else { str = "0" + str; } } return str; } }}
- C#阶乘
- C#阶乘求和
- 标准阶乘算法-C#
- C#基础编程---阶乘
- C#大数阶乘算法
- C#阶乘递归算法
- C# 1000的阶乘
- c#计算100的阶乘
- C#程序设计(七)----阶乘
- C#大整数的阶乘
- C#编程实现阶乘算法
- C#阶乘类,可以精确计算大数的阶乘,10000的阶乘只要1.3秒左右
- C#计算阶乘的递归算法
- C#计算阶乘的递归算法
- C#用递归求阶乘 n!
- c#学习笔记之阶乘遇到问题
- c#实验7.2:阶乘的异常处理
- c# 递归 求阶乘 实现二分查找
- Linux下c开发 之 线程通信
- 深入分析Java ClassLoader原理
- 360 2013校园招聘笔试题(含参考答案)
- Linux多线程编程(不限Linux)
- sqlplus set用法
- C#阶乘
- getJSON 缓存
- 字节对齐
- Access restriction: The type XXXX is not accessible due to restriction on required library
- awk语法大全
- net-snmp-Using and loading MIBS
- MFC-CWinApp
- 定时任务crontab 详解
- GUID全局唯一标识符(GUID,Globally Unique Identifier)<转自百度百科>