Volume 0 / 0030->0034

Problem 0030 : Sum of Integers(未)

"Wrong Answer" なんでだろ。

#include <iostream>

using namespace std;

int check( int s, int n, int sum, int start, int level ){
	int cnt = 0;

	for( int i=start; i<=9; i++ ){
		if( level == n && sum+i == s )
			return 1;
		if( level != n )
			cnt += check( s, n, sum+i, i+1, level+1 );
	}

	return cnt;
}

int main(void)
{
	int n, s;

	while( cin >> n >> s, n != 0 && s != 0 )
		cout << check( s, n, 0, 0, 1 ) << endl;

	return 0;
}

Problem 0031 : Weight

#include <iostream>
#include <deque>

using namespace std;


int main(void)
{
	int w_table[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
	int w, i;
	deque<int> w_use;

	while( cin >> w ){

		w_use.clear();

		for( i=9; i>=0 && w>0; i-- ){
			if( w-w_table[i] >= 0 ){
				w -= w_table[i];
				w_use.push_front(w_table[i]);
			}
		}

		cout << w_use[0];
		for( i=1; i<w_use.size(); i++ )
			cout << " " << w_use[i];
		cout << endl;

	}

	return 0;
}

Problem 0032 : Plastic Board

#include <iostream>

using namespace std;


int main(void)
{
	int a, b, c;
	int cnt1, cnt2;
	cnt1 = cnt2 = 0;
	while( scanf("%d,%d,%d", &a, &b, &c) != -1 ){
		if( a*a+b*b == c*c )
			cnt1++;
		else if( a == b )
			cnt2++;
	}

	cout << cnt1 << endl;
	cout << cnt2 << endl;

	return 0;
}

Problem 0033 : Ball

#include <iostream>
#include <stack>

using namespace std;


int main(void)
{
	int n, num, i;
	stack<int> b, c;

	cin >> n;
	while( n-- > 0 ){
		while( !b.empty() ) b.pop();
		while( !c.empty() ) c.pop();
		cin >> num; b.push(num);

		for( i=1; i<10; i++ ){
			cin >> num;
			if( b.top() > num ){
				if( !c.empty() &&  c.top() > num )
					continue;
				if( c.empty() || c.top() < num )
					c.push(num);
			} else {
				if( c.empty() ){
					b.push(num);
				} else {
					if( b.top() > c.top() )
						b.push(num);
					else
						c.push(num);
				}
			}
		}

		if( b.size() + c.size() == 10 )
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

Problem 0034 : Railway Lines

#include <iostream>
#include <stack>

using namespace std;


int main(void)
{
	int l[10], v1, v2, sum, i;
	double t;
	while( scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &l[0], &l[1], &l[2], &l[3], &l[4], &l[5], &l[6], &l[7], &l[8], &l[9], &v1, &v2) != -1 ){
		sum = 0;
		for( i=0; i<10; i++ )
			sum += l[i];
		t = (double)sum / ( v1 + v2 );

		double d;
		d = t*v1; i=0;
		while(d > 0){
			d -= l[i]; i++;
		}
		cout << i << endl;
	}
	return 0;
}