我想获得Objective C中的排列和组合。我发现两者都存在,但是它们在我的代码中存在一些问题,我不明白它在哪里出现。其实我想在文本框中打印这个值,但是它打印了我输入的最后一个值。我使用下面给出的代码。帮我请人..如何找到任何整数值的排列和组合?
typedef enum{Power,Exponent,nCr,nPr} CalcOperation;
NSString *storage;
CalcOperation operation;
-(IBAction)combination{
operation = nPr;
storage = label.text;
[email protected]"";
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; `
}
}
你试过调试?尝试设置断点并逐步执行代码。 – ColinE 2013-04-20 06:38:22
是的,很多时候它采用阶乘方法并找到阶乘,但无法合并结果并打印出我在文本框中输入的最后一个值。 – 2013-04-20 06:42:50
试着让'factorial:'方法返回一个'float'而不是'int'也许吧?此外,请尝试记录杜松子酒的每一个单独的值以及它们的组合,以确定您是否至少能够正确地获得这些值。 – msgambel 2013-04-20 07:01:10