第一回 オフラインリアルタイムどう書く の回答
14491 ワード
http://nabetani.sakura.ne.jp/hena/1/ の回答
コンパイル方法と実行方法
$ gcc main.m -framework Foundation
$ ./a.out < input.txt
ソースコードとインプットファイル
main.m
#import <Foundation/Foundation.h>
NSString* ox(int i) {
return (i == 0)? @"o" : @"x";
}
NSString* playgame(NSString *str) {
int b[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
const char* cstr = [str UTF8String];
for(int i = 0; i < 9; i++) {
int p = i % 2;
int s = cstr[i] - '0' - 1;
if(b[s] != 0) return [[NSString alloc] initWithFormat:@"Foul : %@ won.", ox((i + 1) % 2)];
else b[s] = p + 1;
if((b[0] & b[1] & b[2]) ||
(b[3] & b[4] & b[5]) ||
(b[6] & b[7] & b[8]) ||
(b[0] & b[3] & b[6]) ||
(b[1] & b[4] & b[7]) ||
(b[2] & b[5] & b[8]) ||
(b[0] & b[4] & b[8]) ||
(b[2] & b[4] & b[6])) return [[NSString alloc] initWithFormat:@"%@ won.", ox(p)];
}
return @"Draw game.";
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str = [[NSString alloc]
initWithData:[NSData
dataWithData:[[NSFileHandle fileHandleWithStandardInput]
readDataToEndOfFile]]
encoding:NSUTF8StringEncoding];
NSArray *lines = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSError *error = nil;
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]+) +(.*)" options:0 error: &error];
if(error == nil) {
for(NSString* line in lines) {
NSTextCheckingResult * match = [regex firstMatchInString:line options:0 range:NSMakeRange(0, line.length)];
if([match numberOfRanges] == 3) {
NSString *result = playgame([line substringWithRange:[match rangeAtIndex: 1]]);
if([result compare:[line substringWithRange:[match rangeAtIndex: 2]]] != 0) {
NSLog(@"input:%@", [line substringWithRange:[match rangeAtIndex: 1]]);
NSLog(@"actual :'%@'", result);
NSLog(@"expected:'%@'", [line substringWithRange:[match rangeAtIndex: 2]]);
}
}
}
}
}
return 0;
}
input.txt
79538246 x won.
35497162193 x won.
61978543 x won.
254961323121 x won.
6134278187 x won.
4319581 Foul : x won.
9625663381 Foul : x won.
7975662 Foul : x won.
2368799597 Foul : x won.
18652368566 Foul : x won.
965715 o won.
38745796 o won.
371929 o won.
758698769 o won.
42683953 o won.
618843927 Foul : o won.
36535224 Foul : o won.
882973 Foul : o won.
653675681 Foul : o won.
9729934662 Foul : o won.
972651483927 Draw game.
5439126787 Draw game.
142583697 Draw game.
42198637563 Draw game.
657391482 Draw game.
Author And Source
この問題について(第一回 オフラインリアルタイムどう書く の回答), 我々は、より多くの情報をここで見つけました https://qiita.com/nnabeyang/items/5171c0171fbe82e68795著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .