iOS - 编程

作者: 搬砖的crystal | 来源:发表于2023-02-23 09:46 被阅读0次
    1.开启3个线程顺序打印1-100
    - (void)print0_100 {
        __block int i = 0;
        NSCondition *condition = [[NSCondition alloc] init];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            while (1) {
                [condition lock];
                while (i%3 != 0) {
                    [condition wait];
                }
                if (i > 100) {
                    [condition unlock];
                    return;
                }
                NSLog(@"A ==== i = %d",i);
                i++;
                [condition broadcast];
                [condition unlock];
            }
        });
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            while (1) {
                [condition lock];
                while (i%3 != 1) {
                    [condition wait];
                }
                if (i > 100) {
                    [condition unlock];
                    return;
                }
                NSLog(@"B ==== i = %d",i);
                i++;
                [condition broadcast];
                [condition unlock];
            }
        });
    
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            while (1) {
                [condition lock];
                while (i%3 != 2) {
                    [condition wait];
                }
                if (i > 100) {
                    [condition unlock];
                    return;
                }
                NSLog(@"C ==== i = %d",i);
                i++;
                [condition broadcast];
                [condition unlock];
            }
        });
    
    }
    
    2.格式化字符串

    给定⼀个只包含英⽂字⺟,半⻆标点(仅包括逗号,句号)和空格的字符串。请按如下规则将字符串 规范化: 1. 单词之间的空格只保留⼀个(需要删除多余的空格) 2. 标点之前的空格需要删除 3. 标点之后仅保留⼀个空格,若没有需要添加,若有两个或以上空格需要删除多余空格 4. 句⼦(以句号分割的字符串)⾸字⺟需要修正为⼤写,其他字⺟需要修正为⼩写 5. 多余的标点需要删除(⽐如连续重复逗号/句号仅保留⼀个)

    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
    
        [self upDateString:@"when a father gives to his son,,, Both laugh, When a son gives to his father, , , Both cry...shakespeare.."];
    }
    
    -(void)upDateString:(NSString *)string{
        if(string.length == 0) return;
        //去除多余的符号和空格
        string = [self deleteRepeat:string withRepeatStr:@" "];
        string = [self deleteRepeat:string withRepeatStr:@","];
        string = [self deleteRepeat:string withRepeatStr:@"."];
        NSLog(@"%@", string);
        
        //全部转换为小写
        string = [string lowercaseString];
    
        //先去掉符号前后空格后续添加
        string = [string stringByReplacingOccurrencesOfString:@". " withString:@"."];
        string = [string stringByReplacingOccurrencesOfString:@", " withString:@","];
        string = [string stringByReplacingOccurrencesOfString:@" ." withString:@"."];
        string = [string stringByReplacingOccurrencesOfString:@" ," withString:@","];
        
        NSArray * arrayK = [string componentsSeparatedByString:@"."];
        NSMutableArray *resultArr = [NSMutableArray array];
        for (NSString *arrayString in arrayK) {
            //获取每个单词
            NSMutableArray * arrayM = [[arrayString componentsSeparatedByString:@" "] mutableCopy];
            //首个单词首字母大写
            if(arrayM.count > 0){
                NSString * first = arrayM[0];
                NSString *resultStr;
                resultStr = [first capitalizedString];
                arrayM[0] = resultStr;
            }
            //拼成字符串
            NSString *capStr = [arrayM componentsJoinedByString:@" "];
            [resultArr addObject:capStr];
        }
        string = [resultArr componentsJoinedByString:@"."];
        string = [string stringByReplacingOccurrencesOfString:@"." withString:@". "];
        string = [string stringByReplacingOccurrencesOfString:@"," withString:@", "];
        NSLog(@"%@", string);
        
    }
    
    //去掉多余字符串
    -(NSString *)deleteRepeat:(NSString *)string withRepeatStr:(NSString *)repeatStr{
        NSArray *stringArr = [string componentsSeparatedByString:repeatStr];
        NSMutableArray *strArrM = [NSMutableArray array];
        for (int i = 0 ; i< stringArr.count ; i++) {
            NSString *str = stringArr[i];
            if (![str isEqualToString:@""]){
                [strArrM addObject:str];
            }else{
                //最后的符号不去掉
                if(i == stringArr.count - 1){
                    [strArrM addObject:str];
                }
            }
        }
        string = [strArrM componentsJoinedByString:repeatStr];
        return string;
    }
    @end
    
    3.三联得分

    给定⼀个 N×N 的棋盘,上⾯有两个玩家玩棋盘游戏的局⾯。其中玩家 A 的棋⼦⽤ O 表⽰,玩家 B 的棋 ⼦⽤ X 表⽰。请计算这个局⾯下两个玩家的得分。 得分计算规则如下: 1. 如果有三个相同棋⼦在连续相邻的格⼦上,称为三连,计⼀分。 2. 相邻是包括横竖斜⽅向上的相邻。 3. 两组三连只要不完全重复,可以分别计⼀分。

    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        
        NSArray *arr = @[@[@".",@".",@".",@".",@".",@".",@".",@".",@"x",@"x",@"."],
                         @[@"x",@".",@".",@".",@".",@"x",@".",@".",@".",@".",@"."],
                         @[@".",@".",@".",@".",@".",@".",@".",@"x",@".",@".",@"."],
                         @[@".",@".",@".",@"o",@"o",@"o",@".",@".",@".",@".",@"."],
                         @[@".",@".",@".",@"o",@"o",@"o",@"o",@"x",@".",@".",@"."],
                         @[@".",@".",@".",@"o",@"o",@"o",@".",@".",@".",@".",@"."],
                         @[@".",@".",@".",@".",@"o",@".",@".",@".",@".",@".",@"."],
                         @[@".",@"x",@".",@".",@".",@".",@".",@".",@".",@".",@"."],
                         @[@".",@".",@".",@".",@".",@".",@"x",@".",@".",@".",@"."],
                         @[@".",@".",@".",@".",@".",@".",@".",@".",@".",@"x",@"."],
                         @[@".",@".",@".",@".",@".",@".",@".",@".",@".",@"x",@"."]
                         
        ];
        [self calculateScore:arr];
    }
    
    -(void)calculateScore:(NSArray *)arr {
        NSString * x = @"x";
        NSString * o = @"o";
        int xPoint = 0;
        int oPoint = 0;
     
        for (int i = 0; i < arr.count; i++) {
            for (int j = 0; j < arr.count; j++) {
           
                NSString *start = arr[i][j];
                //1.判断横轴是否连续三个能得分
                if (i < arr.count - 2 && start == arr[i + 1][j] && start == arr[i + 2][j]) {
                    if (start == x) {
                        xPoint += 1;
                    } else if (start == o) {
                        oPoint += 1;
                    }
                }
                //2.判断斜着是否连续三个能得分
                //右下侧斜
                if (i < arr.count - 2 && j < arr.count - 2 && start == arr[i + 1][j + 1] && start == arr[i + 2][j + 2]) {
                    if (start == x) {
                        xPoint += 1;
                    } else if (start == o) {
                        oPoint += 1;
                    }
                }
                //右上侧斜
                if (i < arr.count - 2 && j > 1 && start == arr[i + 1][j - 1] && start == arr[i + 2][j - 2]) {
                    if (start == x) {
                        xPoint += 1;
                    } else if (start == o) {
                        oPoint += 1;
                    }
                }
                //3.判断竖着三个是否能得分
                if (j < arr.count - 2 && start == arr[i][j + 1] && start == arr[i][j + 2]) {
                    if (start == x) {
                        xPoint += 1;
                    } else if (start == o) {
                        oPoint += 1;
                    }
                }
            }
        }
        //打印得分
        NSLog(@"%d",oPoint);
        NSLog(@"%d",xPoint);
        
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:iOS - 编程

        本文链接:https://www.haomeiwen.com/subject/hychkdtx.html