@fiy-fish
2015-07-14T11:41:54.000000Z
字数 1302
阅读 1739
Objective-c
// 搬货物//// Created by Aaron on 15/7/2.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"#import "HuoWu.h"int main(int argc, const char * argv[]) {@autoreleasepool {Person *p = [[Person alloc] init];NSLog(@"%ld",[p timesForCarry:2001]);HuoWu *h = [[HuoWu alloc] init];[h setWeight:2001];//对象用来做参数NSLog(@"%ld",[p timesForCarryHuoWu:h]);}return 0;}
import <Foundation/Foundation.h>#import "HuoWu.h"//有一个人,最大承重100KG,现在有2T货物,问最少要搬多少次?@interface Person : NSObject{NSInteger _maxWeight;}-(NSInteger)timesForCarry:(NSInteger)weight;-(NSInteger)timesForCarryHuoWu:(HuoWu *)huoWu;@end
#import "Person.h"@implementation Person-(instancetype)init{if(self = [super init]){_maxWeight = 100;}return self;}-(NSInteger)timesForCarry:(NSInteger)weight{if(weight%_maxWeight == 0){return weight/_maxWeight;}else{return weight/_maxWeight+1;}}-(NSInteger)timesForCarryHuoWu:(HuoWu *)huoWu{if([huoWu weight]%_maxWeight == 0){return [huoWu weight]/_maxWeight;}else{return [huoWu weight]/_maxWeight+1;}}@end
#import <Foundation/Foundation.h>@interface HuoWu : NSObject{NSInteger _weight;}-(void)setWeight:(NSInteger)weight;-(NSInteger)weight;@end
#import "HuoWu.h"@implementation HuoWu-(void)setWeight:(NSInteger)weight{if(_weight != weight){_weight = weight;}}-(NSInteger)weight{return _weight;}@end
