[关闭]
@fiy-fish 2015-05-18T16:09:16.000000Z 字数 903 阅读 1315

搬运货物练习

Objective-c


  1. // Person.h
  2. // setter和getter
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. /*
  9. 4.有一个人,最大承重100KG,现在有2T货物,问最少要搬多少次?
  10. */
  11. @interface Person : NSObject
  12. {
  13. int _maxWeight;
  14. }
  15. -(void)setMaxWeight:(int)weight;
  16. -(int)timesForCarry:(int)weight;
  17. @end
  1. // Person.m
  2. // setter和getter
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Person.h"
  8. @implementation Person
  9. -(void)setMaxWeight:(int)weight
  10. {
  11. if(_maxWeight != weight)
  12. {
  13. _maxWeight = weight;
  14. }
  15. }
  16. -(int)timesForCarry:(int)weight
  17. {
  18. if(weight%_maxWeight == 0)
  19. {
  20. return weight/_maxWeight;
  21. }
  22. else
  23. {
  24. return weight/_maxWeight+1;
  25. }
  26. }
  27. @end
  1. // main.m
  2. // 搬货物练习
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Person.h"
  9. int main(int argc, const char * argv[])
  10. {
  11. @autoreleasepool {
  12. Person *p = [Person alloc];
  13. [p setMaxWeight:100];
  14. NSLog(@"需要%d次",
  15. [p timesForCarry:2001]);
  16. }
  17. return 0;
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注