1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#import <Foundation/Foundation.h>
void PrintTree(int line);
int main(int argc, const char * argv[]) {
/*
1
1 1
1 2 1
1 3 3 1
*/
PrintTree(7);
return 0;
}
void PrintTree(int line) {
NSMutableArray *oldArr = [NSMutableArray array];
NSMutableArray *newArr = [NSMutableArray array];
for (int i = 3; i < line + 1; i++) {
[newArr removeAllObjects];
for (int J = 0; J < i; J++) {
if (J == 0 || J == i - 1) {
[newArr addObject:@"1"];
} else {
[newArr addObject:[NSString stringWithFormat:@"%ld",[oldArr[J] integerValue] + [oldArr[J - 1] integerValue]]];
}
}
for (int M = 0; M < line - i; M++) {
printf(" ");
}
for (int i = 0; i < newArr.count; i++) {
if (i == newArr.count - 1) {
printf("%d",[newArr[i] intValue]);
} else {
printf("%d ",[newArr[i] intValue]);
}
}
printf("\n");
oldArr = [NSMutableArray arrayWithArray:newArr];
}
}