(C#)计算字符串排列组合数 如"abcd"组合数为24 "aabb"组合数为6
2010-12-10 12:25
501 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { public static long GetA(int m, int n) { long num = -1; if (m >= n && n >= 0 && m > 0) { num = 1; for (int i = 0; i < n; i++) { num *= m - i; } return num; } return num; } public static long GetCombinationCount(string input) { long count; if (input == null) count = -1; else if (input == string.Empty) count = 0; else { int length = input.Length; if (length > 25) count = -1; else { Dictionary<char, int> dic = new Dictionary<char, int>(); foreach (char item in input) { if (!dic.ContainsKey(item)) dic.Add(item, 1); else dic[item]++; } count = GetA(length, length); if (dic.Count != length) { Dictionary<char, int>.Enumerator enu = dic.GetEnumerator(); while (enu.MoveNext()) { int tmp = enu.Current.Value; if (tmp != 1) count /= GetA(tmp, tmp); } } } } return count; } static void Main(string[] args) { Console.WriteLine(GetA(50, 20)); Console.WriteLine(GetA(50, 20) * 10); Console.WriteLine(GetCombinationCount("abcdefghijklmnopqrstuvwxy")); Console.WriteLine(GetCombinationCount("abcdefghijklmnopqrstuvwxyz")); Console.WriteLine(GetCombinationCount("1234567890")); Console.WriteLine(GetCombinationCount("1122333456")); Console.WriteLine(GetCombinationCount("1234567890") / GetA(2, 2) / GetA(2, 2) / GetA(3, 3)); } } }
相关文章推荐
- 在C#中解决动态计算表达式的问题(如字符串"Sin(1)+Cos(2)",执行并得出结果)
- 从排列与组合的python实现到"生日问题"的解释
- iOS 字符串长度计算boundingRectWithSize遇到 " \r\n"的时候,后面的字符串长度返回0 在swift中使用pod导入第三方库时,注意事项
- 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 原始字符串是"abc",打印得到下列所有组合情况
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 针对如"123456"之类的任意字符序列,输出它们所有的排列组合
- 找出字符串的最长子串,要求子串的所有字符相同,如:"abcdeeefgh"结果是"eee"
- C 反转字符串 "abcd" --> "dcba"
- C# "125" 转换位“00000125” 左边使用固定字符补充为固定长度的字符串
- asp.net(c#) datelist DataGrid 中截取字符串加"..." 和 鼠标放上去字符全部
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java) (转载)
- 笔试算法题(16):二叉树深度计算 & 字符串全排列
- 用于执行(计算) "字符串表达式" 的 T-SQL 存储过程
- C#中怎么做到这样的stylevar["charset"]下标为字符串的数组?
- C# 计算排列组合数,及列出所有组合形式的算法
- C#字符串拼接 "+"连接与Append()方法 速度比较
- 一个中文汉字算两位,一个英文字母算一位,计算中文和英文混合的字符串长度。字符串:String str = "我不好java";
- C#中如何去掉字符串结束符"/0"
- asp.net(c#) datelist DataGrid 中截取字符串加"..." 和 鼠标放上去字符全部显示
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)