打印N字形

根据输入的内容打印
 //比如 [obj printString:@”12345″];
 
 1   1
 2  22
 3 3 3
 44  4
 5   5
 
 */

– (void)printString:(NSString *)string{
    NSMutableString *muStr = [[NSMutableString alloc]init];
    NSInteger length = string.length;
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < length; j++) {
            if (j == 0) {//第一列
                [muStr appendFormat:@”%c”,[string characterAtIndex:i]];
            }else if (j + i == length -1){//这是副对角线的表示方法
                [muStr appendFormat:@”%c”,[string characterAtIndex:i]];
            }else if (j == length – 1){//最后一列
                [muStr appendFormat:@”%c”,[string characterAtIndex: i]];
            }
            //这个是主对角戏
//            else if (i == j){
//            [muStr appendFormat:@”%c”,[string characterAtIndex: i]];
//            }
            else {
            [muStr appendFormat:@” “];//除了上面的位置其余都是空
            }
        }
    [muStr appendFormat:@”\n” ];
    }
    NSLog(@”第六题:\n%@”,muStr);
}

    原文作者:Z字形编排问题
    原文地址: https://blog.csdn.net/godblessmyparents/article/details/47340841
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞