1. 掷骰子题,掷骰子100次,输出每个号出现的次数
typedef struct
{
int diceNum;
int count;
} myDiceDate;
void test1()
{
myDiceDate dicedate[6] = {{1,0},{2,0},{3,0},{4,0},{5,0},{6,0}};
for(int i = 0; i < 100; i++)
{
int current = (arc4random()%6)+1; // 表示值的范围[1,6]
for(int j = 0; j < 6; j++)
{
if(current == dicedate[j].diceNum)
{
dicedate[j].count+=1;
continue;
}
}
}
for(int i = 0; i < 6; i++)
{
NSLog(@"%i出现的次数%i",dicedate[i].diceNum,dicedate[i].count);
}
}
2. 输出一个字符串中每个字符出现的次数
void test2()
{
NSString *str1 = @"abcegsdfjkflajfkjhjfajkfakjfdsafhjfkdsahfa";
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //创建字典集合
for(int i = 0; i < [str1 length]; i++)
{
char c = [str1 characterAtIndex:i]; //获取字符串对应的字符
id value = [dictionary objectForKey:@(c)]; //获取字典表中对应的value
int num = [value intValue] + 1; //获取当前字符已出现的次数
[dictionary setObject:@(num) forKey:@(c)];
}
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop)
{
NSLog(@"%c-----%@",[key charValue],value);
}];
}
3. 现在有一个有序的数组,要求用户输入一个整数放到数组中还保证这个数组是有序的。例如原数组为@[@1,@3,@5,@7,@9];插入8之后为@[@1,@3,@5,@7,@8,@9];
void test3()
{
NSArray *array = @[@1, @3, @5, @7, @9];
int num = 8;
int location = arrayOrder(array, num);
NSMutableArray *mutableArray = [NSMutableArray array];
[mutableArray addObjectsFromArray:array];
[mutableArray insertObject:@(num) atIndex:@(location)];
NSLog(@"%@",mutableArray);
}
#pragma mark 返回要插入数的位置
int arrayOrder(NSArray *array,int num)
{
int start = 0; //循环开始位置
int end = array.count-1; //循环结束位置
int medium ; //数组中间值
while(start <= end)
{
medium = (start + end) / 2;
if(num > [array[medium] intValue])
{
start = medium+1;
}
else
{
end = medium-1;
}
}
return start;//返回要插入的位置
}
4. 现有如下定义的字符串: NSString *str=@“itheimaAndroid”,能对该字符串进行修改请输出删除Android后的新字符串
void test4()
{
NSString *str = @"itheimaAndroid";
NSRange range = [str rangeOfString:@"Android"];
NSString *newStr = [str substringToIndex:range.location];
NSLog(@"%@",newStr);
}
5. 求出1.2.3.4四个数,随意组成一个里面没有相同数字的三位数,总共有多少种组合方式
void test5()
{
int count = 0; //存放组合方式
for(int i = 1; i < 5; i++)
{
for(int j = 1; j < 5; j++)
{
for(int k = 1; k < 5;k++)
{
if(i != j && i != k && j != k)
{
NSLog(@"%d%d%d",i,j,k);
count++;
}
}
}
}
NSLog(@"总共有%d种组合方式",count);
}
6. 求100以内3的倍数
void test6()
{
for(int i = 3; i < 101; i+=3)
{
if(i % 3 == 0)
{
NSLog(@"%d",i);
}
}
}
7. 深复制,浅复制的问题,随便一个NSArray,用代码创建一个可变和一个不可变数组,他们是深复制还是浅复制
void test7()
{
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhaoliu",@"tianqi"];
//创建不可变数组
NSArray *array2 = [array1 copy];
NSLog(@"array2 = %@",array2);
NSLog(@"拷贝的array2数组是不可变数组,这属于浅复制,源对象和副本对象指向的是同一个对象");
//创建可变数组
NSMutableArray *array3 = [array1 mutableCopy];
//给array3添加内容
[array3 addObject:@"wangba"];
//打印array1内容
NSLog(@"array1 = %@",array1);
//打印array3内容
NSLog(@"array3 = %@",array3);
NSLog(@"拷贝的array3数组是可变数组,这属于深复制,源对象和副本对象指向的是两个不同的对象,修改新对象的内容不会影响原来的对象");
}
8. 遍历NSArray、NSDictionary(知道几种写几种)
void test8()
{
//NSArray
NSArray *array1 = @[@"张三",@"李四",@"王五",@"赵六",@"钱七",@"王八"];
//直接输出NSArray
NSLog(@"array = %@",array1);
//普通遍历
for(int i = 0; i < array1.count; i++)
{
NSLog(@"%d-----%@", i, [array1 objectAtIndex:i]);
}
//快速遍历
for(NSString *item in array1)
{
NSLog(@"%@",item);
}
//利用block输出
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zd---------%@",idx,obj);
}];
//NSDictionary
NSDictionary *dict1 = @{@"name":@"张三",
@"age":@18,
@"QQ":@"12324355245",
@"tel":@"154535545235"};
//直接输出
NSLog(@"dict1 = %@",dict1);
//利用普通for循环输出
NSArray *allKeys = [dict1 allKeys];
for(int i = 0; i < allKeys.count; i++)
{
NSLog(@"%@ = %@", allKeys[i], dict1[allKeys[i]]);
}
//快速遍历
for(NSString *item in dict1.allKeys)
{
NSLog(@"%@ = %@", item, dict1[item]);
}
//使用block
[dict1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ = %@",key,obj);
}];
}
9. 利用分类给NSString扩展3个方法:<1>字符串反转(比如@"123"调用方法后返回@"321");<2>计算英文字母的个数(比如@"5435abc54abc3AHJ5"调用方法后返回的是9);<3>去除字符串两端空格(比如@" 1235 45 "调用方法后返回@"1235 45");
@interface NSString(LPY)
//字符串反转
+ (NSString *) stringWithReverse:(NSString *)rString;
- (NSString *) initWithReverse;
//计算英文字母的个数
+ (NSUInteger) stringWithCharacterLength:(NSString *)lString;
- (NSUInteger) initWithCharacterLength;
//去除字符串两端空格
+ (NSString *) stringWithTrim:(NSString *)tString;
- (NSString *) initWithTrim;
@end
@implementation NSString(LPY)
//字符串反转
+ (NSString *) stringWithReverse:(NSString *)rString
{
NSMutableString *str = [[NSMutableString alloc] initWithString:rString];
NSUInteger rLength = str.length;
for(int i = 0; i < rLength / 2; i++)
{
//交换 i 与 str.length - i 两个变量的值
char tmp = [str characterAtIndex:i];
NSUInteger rindex = rLength - 1 - i;
char rtmp = [str characterAtIndex:rindex];
[str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",rtmp]];
[str replaceCharactersInRange:NSMakeRange(rindex, 1) withString:[NSString stringWithFormat:@"%c",tmp]];
}
return str;
}
- (NSString *) initWithReverse
{
return [NSString stringWithReverse:self];
}
//计算英文字母个数
+ (NSUInteger) stringWithCharacterLength:(NSString *)lString
{
NSUInteger count = 0;
for (int i = 0; i < lString.length; i++)
{
char tmp = [lString characterAtIndex:i];
if((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z'))
{
count++;
}
}
return count;
}
- (NSUInteger) initWithCharacterLength
{
return [NSString stringWithCharacterLength:self];
}
//去除字符串两端空格
+ (NSString *) stringWithTrim:(NSString *)tString
{
NSMutableString *str = [[NSMutableString alloc] initWithString:tString];
NSUInteger index = 0;
//去除左边空格
while ([[NSString stringWithFormat:@"%c",[str characterAtIndex:index]] isEqualToString:@" "])
{
[str deleteCharactersInRange:NSMakeRange(index, 1)];
}
//去除右边空格
while([[NSString stringWithFormat:@"%c",[str characterAtIndex:str.length - 1]] isEqualToString:@" "])
{
[str deleteCharactersInRange:NSMakeRange(str.length - 1, 1)];
}
return str;
}
- (NSString *) initWithTrim
{
return [NSString stringWithTrim:self];
}
@end
void test9()
{
//字符串反转
NSString *str = @"abcdefg";
//类方法
NSString *rStr1 = [NSString stringWithReverse:str];
NSLog(@"%@反转后:%@",str,rStr1);
//对象方法
NSString *rStr2 = [str initWithReverse];
NSLog(@"%@反转后:%@",str,rStr2);
//求字符串中字母的个数
NSString *str2 = @"5435abc54abc3AHJ5";
//类方法
NSUInteger count1 = [NSString stringWithCharacterLength:str2];
NSLog(@"%@的字母个数为:%ld",str2,count1);
//对象方法
NSUInteger count2 = [str2 initWithCharacterLength];
NSLog(@"%@的字母个数为:%ld",str2,count2);
//去除字符串两端空格
NSString *str3 = @" 1235 45 ";
//对象方法
NSString *tStr1 = [NSString stringWithTrim:str3];
NSLog(@"%@去除两端空格:%@",str3,tStr1);
//类方法
NSString *tStr2 = [str3 initWithTrim];
NSLog(@"%@去除两端空格:%@",str3,tStr2);
}