夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

タイムアタック〜5問クリア

今日は宿題Dayですが、集中できないのでSRMのタイムアタックやっちゃいました。

ではでは、今日の分♪

173

プログレスバーの問題。

AOJかなんかで前もやった気がする。
解答だけ

class ProgressBar {
   public:
   string showProgress(vector <int> taskTimes, int tasksCompleted)
  {
	int aTime = 0, cTime = 0;
	for (int i=0; i<taskTimes.size(); i++) aTime += taskTimes[i];
	for (int i=0; i<tasksCompleted; i++) cTime += taskTimes[i];
	double per = 1.0*cTime/aTime;
	int done = (int)20*per;
	string ans = "";
	for (int i=0; i<done; i++) ans += '#';
	for (int i=0; ans.length()<20; i++) ans += '.';
	return ans;
  }
};

174

クロスワードと数字が与えられて、横並びの数字と同じサイズの空欄の数を数える問題。
要約すると微妙だけどまぁコードみてどういう事か理解してください。

class CrossWord {
   public:
   int countWords(vector <string> board, int size)
  {
	int tSize = 0, ans = 0;
	for (int i=0; i<board.size(); i++) {
		string tmp = board[i];
		cout << tmp << endl;
		for (int j=0; j<tmp.length(); j++) {
			if (tmp[j] == '.') {
				tSize++;
			} else if (tmp[j] == 'X') {
				if (tSize == size) ans++;
				tSize = 0;
			}
		}
		if (tSize == size) ans++;
		tSize = 0;
	}
	return ans;
  }
};

175

コイン投げて時計を進んだり戻ったり。
n回目におもてが出ればn時間進んで、裏がでたらn時間もどる。

やっぱりDiv.2 Easyは書くだけが多い。

class ClockWalk {
   public:
   int finalPosition(string flips)
  {
	int time = 12;
	for (int i=0; i<flips.length(); i++) {
		if (flips[i] == 'h') {
			time += i+1;
			while (time > 12) {
				time -= 12;
			}
		} else if (flips[i] == 't') {
			time -= (i+1);
			while (time < 1) {
				time += 12;
			}
		}
	}
	return time;
  }
};

176

RGBインデックスで色がわたされるからその補色(?)みたいなのを求める。
ただし条件付きで、R,G,Bのいずれかの補色と原色の数値の差がすべて32以下だったらそれは美しくないから、128で操作しましょう。

はい。コード見てください。

class RGBColor {
   public:
   vector <int> getComplement(vector <int> rgb)
  {
	vector<int> ans(3);
	int r, g, b;
	if (abs(rgb[0]-(255-rgb[0])) <= 32 && abs(rgb[1]-(255-rgb[1])) <= 32 && abs(rgb[2]-(255-rgb[2])) <= 32) {
		if (rgb[0]+128 > 255) {
			r = rgb[0]-128;
		} else {
			r = rgb[0]+128;
		}
		if (rgb[1]+128 > 255) {
			g = rgb[1]-128;
		} else {
			g = rgb[1]+128;
		}if (rgb[2]+128 > 255) {
			b = rgb[2]-128;
		} else {
			b = rgb[2]+128;
		}
	} else {
		r = 255-rgb[0]; g = 255-rgb[1]; b = 255-rgb[2];
	}
	ans[0] = r; ans[1] = g; ans[2] = b;
	return ans;
  }
};

177

ちょっとだけ手こずった・・・かも?
階段みたいな模様をつくるんだけど制約の中で何パターン作れるか調べること。

0で割ちゃったり、あとループ掛ける対称まちがえるとTLEするからそこさえ気をつければよし。

class Stairs {
   public:
   int designs(int maxHeight, int minWidth, int totalHeight, int totalWidth)
  {
  	int div, count=0;
	for (int i=maxHeight; i>0; i--) {
		if (totalHeight%i!=0) continue;
		div = totalHeight/i-1;
		if (div==0||totalWidth%div!=0) continue;
		if (totalWidth/div>=minWidth) {
			count++;
		} else {
			break;
		}
	}
	return count;
  }
};

総評

だんだんDiv.2 Easyが作業ゲーに思えてきた。なれるのも大切だけど、もうちょっと難しいものを解いてアルゴリズムに慣れていかなければいけない気がしてきた。