Iphone 如何找到任何整数值的排列和组合?
我想在Objective C中获得排列和组合。我已经找到了这两种排列和组合,但它们在我的代码中存在一些问题,我不知道它们发生在哪里。实际上,我想打印文本字段中的值,但它会打印我输入的最后一个值。我使用了下面给出的代码。请帮助我Iphone 如何找到任何整数值的排列和组合?,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我想在Objective C中获得排列和组合。我已经找到了这两种排列和组合,但它们在我的代码中存在一些问题,我不知道它们发生在哪里。实际上,我想打印文本字段中的值,但它会打印我输入的最后一个值。我使用了下面给出的代码。请帮助我 typedef enum{Power,Exponent,nCr,nPr} CalcOperation; NSString *storage; CalcOperation operation; -(IBAction)combination{
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; `
}
}
typedef枚举{幂,指数,nCr,nPr}CalcOperation;
NSString*存储;
计算操作;
-(iAction)组合{
操作=nPr;
存储=label.text;
textField.text=@;
label.text=@;
}
-(int)阶乘:(int)n{
int d=n;
内部温度=1;
用于(int b=2;b//计算nCr,其中n=tempo,r=b
int tempo = [storage intValue];
int b = [label.text intValue];
float tempCb = (float)[self factorial:tempo] / ([self factorial:b] * [self factorial:tempo-b])];
break;
你试过调试吗?试着设置断点并单步执行你的代码。是的,很多时候它会校准阶乘方法并找到阶乘,但无法组合结果并打印我在文本字段中输入的最后一个值。试着让你阶乘:
方法返回一个浮点值,而不是int
?此外,请尝试记录每个值及其组合,以查看您是否至少获得了正确的值。textField.text=[[NSString alloc]initWithFormat:@“%d”,[self factorial:tempo]/([self factorial:[val floatValue]]*[self factorial:tempo-[val floatValue]])];//我们的方法也是正确的,但我已经纠正了,只要在我的代码中将%f改为%d,你就可以使用float进行阶乘存储。但是facorial是用于数字的,结果应该是一个数字。