首页 > ACM题库 > HDU-杭电 > HDU 2954-Marble Madness[解题报告]HOJ
2014
02-24

HDU 2954-Marble Madness[解题报告]HOJ

Marble Madness

问题描述 :

oyvind likes to test his future employees, especially with funny games.

This time he has invented a game where you start out with B black marbles and W white ones in a bag.


You also have endless supplies of both kinds of marbles outside the bag. The game proceeds in rounds. In each round, you take two marbles at random out of the bag, and put one marble back in (possibly another color than any of the two you took out), obeying the following rules:

1. When you take out two white marbles, you put a black one back in.

2. When you take out one black and one white, you put a white marble back in.

3. When you take out two black marbles, you put a black one back in.

At the end of this game, there will only be one marble in the bag. This marble may be white with a probability and black with a probability given the number of marbles of each colour you start with. Your job is to find these probabilities.

输入:

The input will start with a line giving the number of test cases, T. Each test case will be presented at one line with two integers, B and W, separated by a single space, representing the number of black and white marbles respectively.

输出:

The input will start with a line giving the number of test cases, T. Each test case will be presented at one line with two integers, B and W, separated by a single space, representing the number of black and white marbles respectively.

样例输入:

2
1 1
3 0

样例输出:

0.00 1.00
1.00 0.00

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

#define PB push_back
#define MP make_pair
#define clr(a,b) (memset(a,b,sizeof(a)))
#define rep(i,a) for(int i=0; i<(int)a.size(); i++)

const int INF = 0x3f3f3f3f;
const double eps = 1E-8;

int T,b,w;

int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&b,&w);
		if(w%2 == 0)	puts("1.00 0.00");
		else	puts("0.00 1.00");
	}
	return 0;
}

  1. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。