首页 \ 问答 \ 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])

在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])

可能的重复:
统计:Python中的组合
有效地计数组合和排列
python中的项目欧拉问题(问题53)

我正在看看是否内置了python中的数学库是nCr(n Choose r)函数:

在此输入图像描述

我知道这可以编程,但是我以为我会检查它是否已经建立在我之前。


Possible Duplicates:
Statistics: combinations in Python
counting combinations and permutations efficiently
Project euler problem in python (problem 53)

I'm looking to see if built in with the math library in python is the nCr (n Choose r) function:

enter image description here

I understand that this can be programmed but I thought that I'd check to see if it's already built in before I do.


原文:https://stackoverflow.com/questions/4941753
更新时间:2024-04-25 15:04

最满意答案

首先,不要使用char*char[N] 。 使用std::string ,然后一切都变得很容易!

例子,

std::string s = "Hello";
std::string greet = s + " World"; //concatenation easy!

容易,不是吗?

现在如果你需要char const *的某些原因,比如当你想传递一些功能,那么你可以这样做:

some_c_api(s.c_str(), s.size()); 

假设此函数声明为:

some_c_api(char const *input, size_t length);

从这里开始探索std::string自己:

  • std :: string的文档

希望有帮助。


First of all, don't use char* or char[N]. Use std::string, then everything else becomes so easy!

Examples,

std::string s = "Hello";
std::string greet = s + " World"; //concatenation easy!

Easy, isn't it?

Now if you need char const * for some reason, such as when you want to pass to some function, then you can do this:

some_c_api(s.c_str(), s.size()); 

assuming this function is declared as:

some_c_api(char const *input, size_t length);

Explore std::string yourself starting from here:

Hope that helps.

相关问答

更多
  • const string message = "Hello" + ",world" + exclam; +运算符具有从左到右的关联性,因此等效括号表达式为: const string message = (("Hello" + ",world") + exclam); 如你所见,两个字符串文字"Hello"和",world"被“添加”,因此出现错误。 串联的前两个字符串之一必须是std::string对象: const string message = string("Hello") + ",world ...
  • 首先,不要使用char*或char[N] 。 使用std::string ,然后一切都变得很容易! 例子, std::string s = "Hello"; std::string greet = s + " World"; //concatenation easy! 容易,不是吗? 现在如果你需要char const *的某些原因,比如当你想传递一些功能,那么你可以这样做: some_c_api(s.c_str(), s.size()); 假设此函数声明为: some_c_api(char cons ...
  • 您可以使用+运算符连接Strings: System.out.println("Your number is " + theNumber + "!"); theNumber被隐式转换为字符串"42" 。 You can concatenate Strings using the + operator: System.out.println("Your number is " + theNumber + "!"); theNumber is implicitly converted to the Stri ...
  • C不支持一些其他语言的字符串。 C中的一个字符串只是一个指向由第一个空字符终止的char数组的指针。 C中没有字符串连接运算符 使用strcat连接两个字符串。 您可以使用以下功能: #include #include char* concat(const char *s1, const char *s2) { char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-termina ...
  • PostScript没有内置的字符串连接运算符。 你需要为此编写一些代码。 例如,请参阅http://en.wikibooks.org/wiki/PostScript_FAQ#How_to_concatenate_strings.3F 。 (由mh更新:在这里复制) /concatstrings % (a) (b) -> (ab) { exch dup length 2 index length add string dup dup 4 2 roll copy ...
  • 您可以merge声明为朋友: public: friend ArrayBag merge(ArrayBag a, ArrayBag b); ArrayBag(); int getCurrentSize() const; bool isEmpty() const; ... 然后定义合并如: ArrayBag merge(ArrayList a, ArrayList b) { int newsz = a.g ...
  • 我想你的意思是PHP代码应该在到达客户端之前进行评估,因此你的代码语法是正确的,但是看看下面看起来更干净,而且不会污染全局JavaScript范围(这就是var ...的用途) : var serviceClass = ''; var serviceName = serviceClass+'.php'; var serviceId = '#'+serviceClass; 但是,因为你的代码语法是正确的,所以你应该在你执行它时确认你实际上有一个带有ser ...
  • 其他答案当然是正确的。 为了说清楚; 这在C#规范的2.4.4.5节中有所介绍: 2.4.4.5字符串文字 C#支持两种形式的字符串文字:常规字符串文字和逐字字符串文字。 常规字符串文字由用双引号括起来的零个或多个字符组成,如“hello”中所示,并且可以包括简单转义序列(例如用于制表符的\ t)和十六进制和Unicode转义序列。 逐字字符串文字由@字符后跟双引号字符,零个或多个字符以及结束双引号字符组成。 一个简单的例子就是@“你好”。 在逐字字符串文字中,分隔符之间的字符是逐字解释的,唯一的例外是qu ...
  • 您必须确保str1指向足够大的内存位置以接收整个结果: char *concat(char const*str1, char const*str2) { size_t const l1 = strlen(str1) ; size_t const l2 = strlen(str2) ; char* result = malloc(l1 + l2 + 1); if(!result) return result; memcpy(result, str1, l1) ; ...
  • 如果你绝对必须,它可能是这样的: jobjectArray ab = env->NewObjectArray(alen+blen, env->FindClass("java/lang/String"), 0); jsize i; for(i=0;iSetObjectArrayElement(ab, i, env->GetObjectArrayElement(a, i)); for(i=0;iSetObjectArrayEleme ...

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)