alabama_country @Wiki

C-assign-2009-03-11

最終更新:

alabama_country

- view
管理者のみ編集可

問1

問2

#include <iostream>
#include <cstdio>
 
//100 人の利用者全員が10 段のエスカレーターを上り終えるまでの時間をカウントする。
//エスカレータは1 秒に1 段進む。
//エスカレータは1 段に二人まで乗れる。
//左側に立つ人は前の人とN 段あけて乗り込む。
//右側を歩いて上る人は、M 段ずつあけて乗り込む。
//エスカレータの右側を歩いて上る人は皆一秒にS 段の早さで登る。
//N,M,S はプログラム実行時に引数または設定ファイルによって指定できるものとする。
 
//時刻0で乗り込む。また、最後の段に最後の人が乗った瞬間で計測おしまい。
 
void increment( int *const arr, const int size)
{
	int *i;
	for(i=arr; i != arr+size; ++i)
		++*i;
}
 
class escalator
{
private:
	const int steps;
	const static int division = 10;
public:
	escalator(int s);
	double run(const int interval_left, const int interval_right, const int climb_steps, int person=100);
};
 
escalator::escalator(int s) : steps(s * division)
{
}
 
double escalator::run(int interval_left, int interval_right, int climb_steps, int person)
{
	int time;
	int last_step[] = {steps, steps};
 
	interval_left *= division;
	interval_right *= division;
 
	// 乗り込む
	for(time = 0; person; ++time)
	{
		// left
		if(last_step[0] >= interval_left)
		{
			--person;
			last_step[0] = 0;
		}
 
		// right
		if(person && last_step[1] >= interval_right)
		{
			--person;
			last_step[1] = 0;
		}
 
		increment(last_step, 2);
		last_step[1] += climb_steps;
	}
 
	// 最後の人たちが下りるまで。
	for(; last_step[0] < steps || last_step[1] < steps; ++time)
	{
		increment(last_step, 2);
		last_step[1] += climb_steps;
	}
	return (double)time / division;
}
 
 
int main(int argc, char *argv[])
{
	int N,M,S;
 
	if(argc < 4)
		return 0;
 
	N = atoi(argv[1]);
	M = atoi(argv[2]);
	S = atoi(argv[3]);
	escalator esc(10);
	std::cout << "Time:" << esc.run(N, M, S) << "sec." << std::endl;
	return 0;
}

タグ:

C言語 Cpp
記事メニュー
目安箱バナー