[Assorted-commits] SF.net SVN: assorted: [840] problems/topcoder
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-06-02 00:18:54
|
Revision: 840 http://assorted.svn.sourceforge.net/assorted/?rev=840&view=rev Author: yangzhang Date: 2008-06-01 17:18:59 -0700 (Sun, 01 Jun 2008) Log Message: ----------- added some new problems Added Paths: ----------- problems/topcoder/ImageDithering/ problems/topcoder/ImageDithering/ImageDithering.cc problems/topcoder/RectangularGrid/ problems/topcoder/RectangularGrid/RectangularGrid.cc problems/topcoder/Time/ problems/topcoder/Time/Time.cc Added: problems/topcoder/ImageDithering/ImageDithering.cc =================================================================== --- problems/topcoder/ImageDithering/ImageDithering.cc (rev 0) +++ problems/topcoder/ImageDithering/ImageDithering.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,332 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class ImageDithering { + public: + int count(string dithered, vector <string> screen) { + int res=0; + bounds(i,screen) { + bounds(j,screen[i]) { + if (dithered.find(screen[i][j]) != str::npos) { + res++; + } + } + } + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + string screenARRAY[] = {"AAAAAAAA", + "ABWBWBWA", + "AWBWBWBA", + "ABWBWBWA", + "AWBWBWBA", + "AAAAAAAA"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(0, theObject.count("BW", screen),24); + } + { + string screenARRAY[] = {"BBBBBBBB", + "BBWBWBWB", + "BWBWBWBB", + "BBWBWBWB", + "BWBWBWBB", + "BBBBBBBB"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(1, theObject.count("BW", screen),48); + } + { + string screenARRAY[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(2, theObject.count("ACEGIKMOQSUWY", screen),150); + } + { + string screenARRAY[] = {"BBBBBBB", + "BBBBBBB", + "BBBBBBB"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(3, theObject.count("CA", screen),0); + } + { + string screenARRAY[] = {"ACBD"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(4, theObject.count("DCBA", screen),4); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/RectangularGrid/RectangularGrid.cc =================================================================== --- problems/topcoder/RectangularGrid/RectangularGrid.cc (rev 0) +++ problems/topcoder/RectangularGrid/RectangularGrid.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,309 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << (x) << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class RectangularGrid { + public: + long long countRectangles(int width, int height) { + long long res = 0; + pp(width); + range(i,1,width + 1) { + range(j,1,height + 1) { + res += i==j ? 0 : (width - i + 1) * (height - j + 1); // (width - i) * (height - j); + //pp(i); + //pp(j); + //pp(i==j?0:i*j); + //cout<<endl; + } + } + //if (width==5) exit(0); + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + RectangularGrid theObject; + eq<long long>(0, theObject.countRectangles(3, 3),22L); + } + { + RectangularGrid theObject; + eq<long long>(1, theObject.countRectangles(5, 2),31L); + } + { + RectangularGrid theObject; + eq<long long>(2, theObject.countRectangles(10, 10),2640L); + } + { + RectangularGrid theObject; + eq<long long>(3, theObject.countRectangles(1, 1),0L); + } + { + RectangularGrid theObject; + eq<long long>(4, theObject.countRectangles(592, 964),81508708664LL); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/Time/Time.cc =================================================================== --- problems/topcoder/Time/Time.cc (rev 0) +++ problems/topcoder/Time/Time.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,299 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class Time { + public: + string whatTime(int seconds) { + int s = seconds % 60; + int minutes = seconds / 60; + int m = minutes % 60; + int h = minutes / 60; + stringstream ss; + ss << h << ":" << m << ":" << s; + return ss.str(); + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + Time theObject; + eq(0, theObject.whatTime(0),"0:0:0"); + } + { + Time theObject; + eq(1, theObject.whatTime(3661),"1:1:1"); + } + { + Time theObject; + eq(2, theObject.whatTime(5436),"1:30:36"); + } + { + Time theObject; + eq(3, theObject.whatTime(86399),"23:59:59"); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |