@fiy-fish
2015-07-15T20:16:33.000000Z
字数 1116
阅读 1440
Objective-c
// day05-03-数组乱序
//
// Created by Aaron on 15/7/7.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
//0 1 2 3 4
//1 2 3 4
#if 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = @[@1,@2,@3,@4,@5,@6,@7,@8];
NSMutableArray *array1 = [NSMutableArray arrayWithArray:array];
NSMutableArray *array2 = [NSMutableArray array];
//控制随机的次数
for(int i = 0; i < [array count]; i++)
{
//根据array1来随下标
NSInteger index = arc4random_uniform((int)[array1 count]);
//随出来的对象添加到array2里
[array2 addObject:array1[index]];
[array1 removeObjectAtIndex:index];
}
NSLog(@"%@",array2);
}
return 0;
}
#endif
//1 2 3 4 5
#if 0
int main()
{
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@5]];
//控制随机的对象和哪一个元素交换
for(int i = 0; i < [array count]; i++)
{
NSInteger index = arc4random_uniform((int)[array count]-i)+i;
[array exchangeObjectAtIndex:i withObjectAtIndex:index];
}
NSLog(@"%@",array);
}
#endif
#if 0
int main()
{
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@5]];
for(int i = 0; i < [array count]-1; i++)
{
for(int j = i+1; j < [array count]; j++)
{
if(arc4random_uniform(2))
{
[array exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"%@",array);
}
#endif