指定した整数は何種類の足し算に分割できるか計算するiPhoneアプリ(C++)のサンプルコードを描いてみます。
#import “ViewController.h”
#include <memory>
using namespace std;
class IntegerPartition {
public:
int solve(int, int);
int dp[100][100];
};
int IntegerPartition::solve(int m, int n) {
dp[0][0] = 1;
for (int i=1; i<=m; i++)
for (int j=0; j<=n; j++)
dp[i][j] = dp[i – 1][j] + (j >= i ? dp[i][j-i]: 0);
return dp[m][n];
};
@interface ViewController ()
@property (nonatomic, weak) UITextField *textM;
@property (nonatomic, weak) UITextField *textN;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
UILabel *title = [[UILabel alloc] init];
title.text = @”Integer Partition”;
title.font = [UIFont systemFontOfSize:30];
[title sizeToFit];
title.center = CGPointMake(CGRectGetMidX(self.view.bounds), 50);
[self.view addSubview:title];
UILabel *example = [[UILabel alloc] init];
example.numberOfLines = 0;
example.text = @”ex) n = 4 \n 4 \n 3 + 1 \n 2 + 2 \n 1 + 1 + 2 \n 1 + 1 + 1 + 1″;
[example sizeToFit];
example.center = CGPointMake(CGRectGetMidX(self.view.bounds), 150);
example.backgroundColor = [UIColor colorWithHue:0.5 saturation:0.1 brightness:1 alpha:1];
[self.view addSubview:example];
UITextField *tm = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
tm.center = CGPointMake(CGRectGetMidX(self.view.bounds) – 50, 250);
tm.borderStyle = UITextBorderStyleLine;
tm.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tm];
UITextField *tn = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
tn.center = CGPointMake(CGRectGetMidX(self.view.bounds) + 50, 250);
tn.borderStyle = UITextBorderStyleLine;
tn.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tn];
UILabel *m = [[UILabel alloc] init];
m.text = @”m =”;
[m sizeToFit];
m.center = CGPointMake(tm.center.x – 50, tm.center.y);
[self.view addSubview:m];
UILabel *n = [[UILabel alloc] init];
n.text = @”n =”;
[n sizeToFit];
n.center = CGPointMake(tn.center.x – 50, tn.center.y);
[self.view addSubview:n];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@”Solve” forState:UIControlStateNormal];
[btn sizeToFit];
btn.center = CGPointMake(CGRectGetMidX(self.view.bounds), 300);
[self.view addSubview:btn];
self.textM = tm;
self.textN = tn;
[btn addTarget:self action:@selector(calc) forControlEvents:UIControlEventTouchUpInside];
}
– (void)calc {
shared_ptr<IntegerPartition> cppClass(new IntegerPartition());
int res = cppClass->solve([self.textM.text intValue], [self.textN.text intValue]);
UILabel *resultLabel = [[UILabel alloc] init];
resultLabel.text = [NSString stringWithFormat:@”%d”, res];
resultLabel.font = [UIFont boldSystemFontOfSize:40];
[resultLabel sizeToFit];
resultLabel.center = CGPointMake(3.0 * CGRectGetMidX(self.view.bounds), 350);
resultLabel.backgroundColor = [UIColor colorWithHue:0.25 saturation:0.8 brightness:0.7 alpha:1];
[self.view addSubview:resultLabel];
[UIView animateWithDuration:0.5 animations:^{
resultLabel.center = CGPointMake(CGRectGetMidX(self.view.bounds), 350);
}];
}
@end