Created
October 10, 2013 02:14
-
-
Save jlstr/6911965 to your computer and use it in GitHub Desktop.
MyCodeSchool Problem #38 Solution (http://www.mycodeschool.com/Problems/Show?problemId=38)
This file contains 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
#include <iostream> | |
#include <string> | |
using namespace std; | |
class RowOrColSum { | |
public: | |
long long sumRowOrColumn(long, long, long, char); | |
}; | |
long long RowOrColSum::sumRowOrColumn(long n, long w, long p, char dir) { | |
long long sum = 0; | |
if(dir == 'R') { | |
long start = p*w - w + 1; | |
for(long i = start; i < start + w; ++i) { | |
if(i <= n) { | |
sum += i; | |
} else { | |
break; | |
} | |
} | |
} else if(dir == 'C') { | |
for(long i = p; true; i += w) { | |
if(i <= n) { | |
sum += i; | |
} else { | |
break; | |
} | |
} | |
} | |
return sum; | |
} | |
int main() { | |
RowOrColSum obj; | |
int cases; | |
cin >> cases; | |
for(int i = 0; i < cases; ++i) { | |
long n, w; | |
cin >> n >> w; | |
long p; | |
char dir; | |
cin >> p >> dir; | |
cout << obj.sumRowOrColumn(n, w, p, dir) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment