[关闭]
@fiy-fish 2015-07-14T19:41:54.000000Z 字数 1302 阅读 1483

day02-03-2练习有一个人,最大承重100KG,现在有2T货物,问最少要搬多少次?

Objective-c


  1. // 搬货物
  2. //
  3. // Created by Aaron on 15/7/2.
  4. // Copyright (c) 2015年 Aaron. All rights reserved.
  5. //
  6. #import <Foundation/Foundation.h>
  7. #import "Person.h"
  8. #import "HuoWu.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. Person *p = [[Person alloc] init];
  12. NSLog(@"%ld",[p timesForCarry:2001]);
  13. HuoWu *h = [[HuoWu alloc] init];
  14. [h setWeight:2001];
  15. //对象用来做参数
  16. NSLog(@"%ld",[p timesForCarryHuoWu:h]);
  17. }
  18. return 0;
  19. }

  1. import <Foundation/Foundation.h>
  2. #import "HuoWu.h"
  3. //有一个人,最大承重100KG,现在有2T货物,问最少要搬多少次?
  4. @interface Person : NSObject
  5. {
  6. NSInteger _maxWeight;
  7. }
  8. -(NSInteger)timesForCarry:(NSInteger)weight;
  9. -(NSInteger)timesForCarryHuoWu:(HuoWu *)huoWu;
  10. @end

  1. #import "Person.h"
  2. @implementation Person
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _maxWeight = 100;
  8. }
  9. return self;
  10. }
  11. -(NSInteger)timesForCarry:(NSInteger)weight
  12. {
  13. if(weight%_maxWeight == 0)
  14. {
  15. return weight/_maxWeight;
  16. }
  17. else
  18. {
  19. return weight/_maxWeight+1;
  20. }
  21. }
  22. -(NSInteger)timesForCarryHuoWu:(HuoWu *)huoWu
  23. {
  24. if([huoWu weight]%_maxWeight == 0)
  25. {
  26. return [huoWu weight]/_maxWeight;
  27. }
  28. else
  29. {
  30. return [huoWu weight]/_maxWeight+1;
  31. }
  32. }
  33. @end

  1. #import <Foundation/Foundation.h>
  2. @interface HuoWu : NSObject
  3. {
  4. NSInteger _weight;
  5. }
  6. -(void)setWeight:(NSInteger)weight;
  7. -(NSInteger)weight;
  8. @end

  1. #import "HuoWu.h"
  2. @implementation HuoWu
  3. -(void)setWeight:(NSInteger)weight
  4. {
  5. if(_weight != weight)
  6. {
  7. _weight = weight;
  8. }
  9. }
  10. -(NSInteger)weight
  11. {
  12. return _weight;
  13. }
  14. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注