Moves in squared strings (I)
题目链接:https://www.codewars.com/kata/moves-in-squared-strings-i/train/objc
#import <Foundation/Foundation.h>
NSString *vertMirror(NSString *strng) {
NSMutableString *res = [[NSMutableString alloc] init];
NSArray *stringArray = [strng componentsSeparatedByString:@"\n"];
for (NSString *tmpStr in stringArray) {
NSMutableString * reverseString = [NSMutableString string];
for(int i = 0 ; i < tmpStr.length; i ++){
//倒序读取字符并且存到可变数组数组中
unichar c = [tmpStr characterAtIndex:tmpStr.length- i -1];
[reverseString appendFormat:@"%c",c];
}
if ([tmpStr isEqualToString:[stringArray lastObject]]) {
[res appendString:reverseString];
break;
}
[reverseString appendString:@"\n"];
[res appendString:reverseString];
}
return res;
}
NSString *horMirror(NSString *strng) {
NSMutableString *res = [[NSMutableString alloc] init];;
NSArray *stringAry = [strng componentsSeparatedByString:@"\n"];
for (int i = stringAry.count-1; i>=0; i--) {
[res appendString:stringAry[i]];
if (i==0) {
break;
}
[res appendString:@"\n"];
}
return res;
}
NSString *oper(NSString *(*f)(NSString *), NSString *s) {
return f(s);
}
Sum of many ints
题目链接:https://www.codewars.com/kata/moves-in-squared-strings-i/train/objc
注意long long
类型。
#import <Foundation/Foundation.h>
long long f(long long n, long long m) {
long long sum = 0;
long long q = n/m;
long long p = n%m;
sum+= (m*(m-1)/2)*q;
sum+= (p+1)*p/2;
return sum;
}
Copyright © 2017 Powered by LZH, Theme used GitHub CSS.