//掷骰子题,掷骰子100次,输出每个号出现的次数
void one()
{
for (int i=1; i<=100; i++) {
int a = arc4random() % 6 +1;
NSLog(@"%d %d",i,a);
}
}
//输出一个字符串中每个字符出现的次数
void two()
{
NSString *str = @"hello apple";
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (int i = 0; i < str.length; i++) {
NSRange range = [str rangeOfComposedCharacterSequenceAtIndex:i];
NSString *s = [str substringWithRange:range];
if (dic[s]) {
int count = [dic[s] intValue]+1;
dic[s] = [NSNumber numberWithInt:count];
}else{
dic[s] = @1;
}
}
NSLog(@"%@",dic);
}
//现在有一个有序的数组,要求用户输入一个整数放到数组中还保证这个数组是有序的。例如原数组为@[@1,@3,@5,@7,@9]; 插入8之后为@[@1,@3,@5,@7,@8,@9]
void three()
{
NSArray *arr = @[@1, @3, @5, @7, @9, @8];
NSArray *result = [arr sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"%@",result);
}
//现有如下定义的字符串: NSString *str=@“itheimaAndroid”,能对该字符串进行修改请输出删除Android后的新字符串;
void four()
{
NSString *str1 = @"itheimaAndroid";
NSString *result1 = [str1 stringByReplacingOccurrencesOfString:@"Android" withString:@""];
NSLog(@"%@",result1);
}
//求出1.2.3.4四个数,随意组成一个里面没有相同数字的三位数,总共有多少种组合方式
void five()
{
NSMutableArray *arr = [NSMutableArray array];
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 4; k++) {
if(i != j && i != k && j != k){
[arr addObject:@(i*100 + j*10 +k)];
}
}
}
}
NSLog(@"%@\n%lu", arr, arr.count);
}
//求100以内3的倍数
void six()
{
int i = 1;
NSMutableArray *arr = [NSMutableArray array];
while (i <= 100) {
if (i % 3 == 0) {
[arr addObject:@(i)];
}
i++;
}
NSLog(@"%@\n%lu", arr, arr.count);
}
//深复制,浅复制的问题,随便一个NSArray,用代码创建一个可变和一个不可变数组,他们是深复制还是浅复制
void seven()
{
NSArray *arrary = [NSArray array];
NSArray *a1 = [NSArray arrayWithArray:arrary];
NSArray *a2 = [arrary copy];
NSArray *a3 = arrary;
NSMutableArray *a4 = [NSMutableArray arrayWithArray:arrary];
NSLog(@"\n%p\n%p\n%p\n%p\n%p", arrary, a1, a2, a3, a4);
}
//利用分类给NSString扩展3个方法:<1>字符串反转(比如@"123"调用方法后返回@"321");<2>计算英文字母的个数(比如@"5435abc54abc3AHJ5"调用方法后返回的是9);<3>去除字符串两端空格(比如@" 1235 45 "调用方法后返回@"1235 45")
- (NSString *)stringByReserve
{
NSMutableString *s = [NSMutableString string];
for (NSUInteger i = self.length; i > 0; i--)
{
[s appendString:[self substringWithRange:NSMakeRange(i-1, 1)]];
}
return s;
}
- (NSUInteger)chararcterCountOfString
{
NSUInteger count = 0;
for (NSUInteger i = 0; i < self.length; i ++)
{
unichar c = [self characterAtIndex:i];
if ((c<='z'&&c>='a')||(c<='Z'&&c>='A')) {
count ++;
}
}
return count;
}
- (NSString *)stringByRemovingBlank
{
return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}