首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何找到任何整数值的排列和组合?

如何找到任何整数值的排列和组合?
EN

Stack Overflow用户
提问于 2013-04-20 14:33:31
回答 1查看 775关注 0票数 1

我想要得到Objective C中的排列和组合。我已经发现了这两种情况,但我的代码中有一些问题,我不知道它发生在哪里。实际上,我想打印文本字段中的值,但它打印的是我输入的最后一个值。我使用的代码如下所示。请帮帮我..

代码语言:javascript
复制
    typedef enum{Power,Exponent,nCr,nPr} CalcOperation;
    NSString *storage;
    CalcOperation operation;
   -(IBAction)combination{ 
    operation = nPr; 
    storage = label.text;
    textField.text=@"";
    label.text = @"";
    }

   -(int) factorial:(int)n{
   int d = n;
   int temp = 1;
   for(int b = 2; b <= d; b++){
    temp = temp*b;
   }
  //NSLog(@"the factorials is = %i",temp);

  return temp;
 }

 -(IBAction)equalButton{
  float tempo = [storage floatValue];
  float b = [label.text floatValue];
  NSString *val = [[NSString alloc]initWithFormat:@"%f" , b];  

    switch(operation) {
     case nCr:

     textField.text = [[NSString alloc]initWithFormat:@"%f",[self factorial:tempo] / ([self factorial:[val floatValue]] * [self factorial:tempo-[val floatValue]])]; 
            //textField.text = [[NSString alloc]initWithFormat:@"%f",[val floatValue]+tempo+10];
          NSLog(@"combination %d",[self factorial:tempo] / ([self factorial:[val floatValue]] * [self factorial:tempo-[val floatValue]]));
            break;
    case nPr:

            textField.text = [[NSString alloc]initWithFormat:@"%f",[self factorial:tempo] / [self factorial:tempo-[val floatValue]]]; 
           NSLog(@"permutations %d",[self factorial:tempo] / ([self factorial:tempo-[val floatValue]]));
            break;  `
                  }
                }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-20 15:12:50

//计算nCr,其中n=节拍,r=b

代码语言:javascript
复制
int tempo = [storage intValue];
int b = [label.text intValue];

float tempCb =  (float)[self factorial:tempo] / ([self factorial:b] * [self factorial:tempo-b])]; 

            break;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16117473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档