Created
September 6, 2015 12:45
-
-
Save mzsima/18eff7269c20d042167f to your computer and use it in GitHub Desktop.
Minimum Combination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.mm | |
// MinimumCombination | |
// | |
// Created by Mizushima Yusuke on 9/6/15. | |
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved. | |
// | |
#import "ViewController.h" | |
#include <vector> | |
#include <memory> | |
using namespace std; | |
class MinInner { | |
public: | |
void solve(vector<int> &v1, vector<int> &v2) { | |
sort(v1.begin(), v1.end()); | |
sort(v2.begin(), v2.end(), [](const int r, const int l) { | |
return r > l; | |
}); | |
} | |
}; | |
@interface ViewController () | |
@property (nonatomic) NSMutableArray *right; | |
@property (nonatomic) NSMutableArray *left; | |
@property (nonatomic, weak) UIView *rightView; | |
@property (nonatomic, weak) UIView *leftView; | |
@property (nonatomic, weak) UIView *targetParent; | |
@property (nonatomic, weak) UIView *targetView; | |
@property (nonatomic, weak) UILabel *guide; | |
@property (nonatomic, weak) UILabel *total; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSMutableArray *numbers = [NSMutableArray array]; | |
for (int i=1; i<100; i++) { | |
[numbers addObject:@(i)]; | |
} | |
// setup riglt | |
self.right = [NSMutableArray array]; | |
for (int i=0; i<10; i++) { | |
int rand = arc4random() % numbers.count; | |
NSNumber *n = numbers[rand]; | |
[self.right addObject:n]; | |
[numbers removeObject:n]; | |
} | |
self.left = [NSMutableArray array]; | |
for (int i=0; i<10; i++) { | |
int rand = arc4random() % numbers.count; | |
NSNumber *n = numbers[rand]; | |
[self.left addObject:n]; | |
[numbers removeObject:n]; | |
} | |
UIView *rv = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.bounds), 0, CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds))]; | |
rv.backgroundColor = [UIColor greenColor]; | |
[self.view addSubview:rv]; | |
self.rightView = rv; | |
[self updateNumberLabel:self.rightView numbers:self.right]; | |
UIView *lv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds))]; | |
lv.backgroundColor = [UIColor orangeColor]; | |
[self.view addSubview:lv]; | |
self.leftView = lv; | |
[self updateNumberLabel:self.leftView numbers:self.left]; | |
// x x x ... x | |
for (int i=0; i<10; i++) { | |
UILabel *xxx = [[UILabel alloc] init]; | |
xxx.text = @"x"; | |
xxx.font = [UIFont systemFontOfSize:40]; | |
xxx.textColor = [UIColor darkGrayColor]; | |
[xxx sizeToFit]; | |
xxx.center = CGPointMake(CGRectGetMidX(self.view.bounds), i * 50 + 80); | |
[self.view addSubview:xxx]; | |
} | |
UILabel *t = [[UILabel alloc] init]; | |
t.font = [UIFont boldSystemFontOfSize:80]; | |
t.textColor = [UIColor whiteColor]; | |
[self.view addSubview:t]; | |
self.total = t; | |
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; | |
[btn setTitle:@"Min" forState:UIControlStateNormal]; | |
btn.titleLabel.font = [UIFont boldSystemFontOfSize:40]; | |
[btn sizeToFit]; | |
btn.center = CGPointMake(CGRectGetMaxX(self.view.bounds) - 40, CGRectGetMaxY(self.view.bounds) - 20); | |
[self.view addSubview:btn]; | |
[btn addTarget:self action:@selector(getMinimum) forControlEvents:UIControlEventTouchUpInside]; | |
} | |
- (void)updateNumberLabel:(UIView*)parent numbers:(NSArray*)numbers { | |
[parent.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
[obj removeFromSuperview]; | |
}]; | |
for (int i=0; i<10; i++) { | |
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)]; | |
l.tag = [numbers[i] intValue]; | |
l.center = CGPointMake(CGRectGetMidX(self.view.bounds) * 0.5, i * 50 + 80); | |
l.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6]; | |
l.text = [numbers[i] stringValue]; | |
l.font = [UIFont boldSystemFontOfSize:40]; | |
l.textColor = [UIColor purpleColor]; | |
l.textAlignment = NSTextAlignmentCenter; | |
l.userInteractionEnabled = YES; | |
l.layer.zPosition = 10; | |
[parent addSubview:l]; | |
} | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
CGPoint p = [[touches anyObject] locationInView:self.view]; | |
self.targetParent = p.x > CGRectGetMidX(self.view.bounds) ? self.rightView : self.leftView; | |
p = [[touches anyObject] locationInView:self.targetParent]; | |
UIView *v = [self.targetParent hitTest:p withEvent:nil]; | |
if (v.tag > 0) { | |
if (!self.guide) { | |
[self.guide removeFromSuperview]; | |
UILabel *l = [[UILabel alloc] initWithFrame:v.frame]; | |
l.font = ((UILabel*)v).font; | |
l.textAlignment = NSTextAlignmentCenter; | |
l.textColor = [UIColor whiteColor]; | |
l.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.8]; | |
[self.targetParent addSubview:l]; | |
self.guide = l; | |
} | |
v.userInteractionEnabled = NO; | |
self.targetView = v; | |
} | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { | |
CGPoint p = [[touches anyObject] locationInView:self.targetParent]; | |
self.targetView.center = p; | |
UIView *v = [self.targetParent hitTest:p withEvent:nil]; | |
if (v.tag == 0) { | |
self.guide.text = @""; | |
} | |
[self.targetParent.subviews enumerateObjectsUsingBlock:^(UILabel *sv, NSUInteger idx, BOOL *stop) { | |
if (sv != v) { | |
sv.alpha = 1; | |
} else { | |
sv.alpha = 0.1; | |
self.guide.text = sv.text; | |
} | |
}]; | |
} | |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | |
if (self.guide.text.length > 0) { | |
NSMutableArray *numbers = (self.targetParent.center.x < CGRectGetMidX(self.view.bounds)) ? self.left : self.right; | |
[numbers exchangeObjectAtIndex:[numbers indexOfObject:@(self.targetView.tag)] withObjectAtIndex:[numbers indexOfObject:@([self.guide.text intValue])]]; | |
[self updateNumberLabel:self.targetParent numbers:numbers]; | |
[self calTotal]; | |
} | |
[self.guide removeFromSuperview]; | |
self.targetView.userInteractionEnabled = YES; | |
self.targetView = nil; | |
self.targetParent = nil; | |
} | |
- (void)calTotal { | |
int ans = 0; | |
for (int i=0; i<10; i++) { | |
ans += [self.right[i] intValue] * [self.left[i] intValue]; | |
} | |
self.total.text = [NSString stringWithFormat:@"%d", ans]; | |
[self.total sizeToFit]; | |
self.total.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 60); | |
} | |
- (void)getMinimum { | |
vector<int> r, l; | |
for(int i=0; i<10; i++) { | |
r.emplace_back([self.right[i] intValue]); | |
l.emplace_back([self.left[i] intValue]); | |
} | |
shared_ptr<MinInner> cppClass(new MinInner()); | |
cppClass->solve(r, l); | |
for(int i=0; i<10; i++) { | |
self.left[i] = @(l[i]); | |
self.right[i] = @(r[i]); | |
} | |
[self updateNumberLabel:self.rightView numbers:self.right]; | |
[self updateNumberLabel:self.leftView numbers:self.left]; | |
[self calTotal]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
blog http://lepetit-prince.net/ios/?p=4324