class TextStatistics { public: double averageLength(string text) { for(int i=0; i<text.size(); ++i){ if( isalpha( text[i] ) ); else text[i] = ' '; } istringstream iss(text); int sum = 0, cnt = 0; for(string s; iss >> s; ){ sum += s.size(); ++cnt; } if( cnt )return (double)sum / (double)cnt; else return 0; } };500
class CreatePairs { public: int maximalSum(vector <int> v) { vector<int> neg; vector<int> pos; int zero = 0; int one = 0; for(int i=0; i<v.size(); ++i){ if(v[i] == 0)++zero; else if(v[i] < 0)neg.push_back( v[i] ); else if(1 < v[i])pos.push_back( v[i] ); else ++one; } sort( neg.begin(), neg.end() ); sort( pos.begin(), pos.end() ); reverse( neg.begin(), neg.end() ); int sum = 0; while( 1 < neg.size() ){ int a = neg.back(); neg.pop_back(); int b = neg.back(); neg.pop_back(); sum += a * b; } if( neg.size() && zero == 0 )sum += neg[0]; while( 1 < pos.size() ){ int a = pos.back(); pos.pop_back(); int b = pos.back(); pos.pop_back(); sum += a * b; } if( pos.size() )sum += pos[0]; return sum + one; } };1000
正方形の傾きが問題文中の2つかと思った。
class Squares { public: int countSquares(vector <string> f) { const int h = f.size(); const int w = f[0].size(); int cnt = 0; for(int i=0; i<h; ++i){ for(int j=0; j<w; ++j){ for(int di=0; i+di<h; ++di){ for(int dj=1; j+dj<w; ++dj){ if( i+dj < h ); else continue; if( 0 <= j-di ); else continue; if( i+di+dj < h); else continue; if( j-di+dj ); else continue; if( f[i][j] != f[i+di][j+dj] )continue; if( f[i][j] != f[i+dj][j-di] )continue; if( f[i][j] != f[i+di+dj][j-di+dj] )continue; ++cnt; } } } } return cnt; } };
0 件のコメント :
コメントを投稿