// main.m
// 有序数组添加元素
//
// Created by itJG on 15/11/23.
// Copyright © 2015年 luo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
//原始数组
NSMutableArray *arr = [NSMutableArray arrayWithArray:@[@"1", @"3", @"5", @"7", @"9"]];
int num;
scanf("%d",&num);
char c=num+'0';
NSMutableString *str=[NSMutableString stringWithFormat:@"%c",c];
for (int i=0; i<arr.count; i++)
{
if (([str compare: arr[i]])==-1)
{
[arr insertObject:str atIndex:i];
break;
}
if (i==arr.count)
{
[arr insertObject:[NSNumber numberWithChar:c] atIndex:arr.count];
}
}
NSLog(@"%@",arr);
return 0;
} |
|