//得到一个字符串@“absdfasdfsdfdsafsd....”所有的排列 length!/((相同项)!*(相同项)!) -(int )numberOfstring:(NSString*)str // NSString *str [email protected]"adfsadfasdfasdfsadfsdaxfgghfgjtyy"; int sumlength = str.length;//所有字符的个数 NSString *mystr = @"abcdefghijklmnopqrstuvwxyz"; NSMutableArray *sameCharNumberArr = [NSMutableArray array]; for (int i=0; i<mystr.length; i++) { NSString *charstr = [mystr substringFromIndex:i]; int number = [[str componentsSeparatedByString:charstr]count]-1; if (number>1) { [sameCharNumberArr addObject:[NSNumber numberWithInt:number]]; } } int textsum=1; for (NSNumber *nub in sameCharNumberArr) { textsum *= fac([nub intValue]); } int numberOfstr = fac(sumlength)/textsum; NSLog(@"所有排列的个数为 %d",numberOfstr); return numberOfstr; } int fac(int n) { int rs=1; while(n>1) rs*=n--; return rs; }
时间: 2024-11-05 11:27:16