programming-challenges A multiplication game (110505) 题解

思路是从结果向开始的情况逆向推算,利用极大值和极小值。代码如下:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

int main() {
	const string str1 = "Stan wins.";
	const string str2 = "Ollie wins.";

	long long n = 0;
	
	while (cin >> n) {
		int turn = 0;
		while (true) {
			if (n == 1) {
				if (turn % 2 == 1) {
					cout << str1 << endl;
				}
				else {
					cout << str2 << endl;
				}
				break;
			}
			else {
				if (turn % 2 == 0) {
					n = ceil((double)n / 9);
				}
				else {
					n = ceil((double)n / 2);
				}
				turn++;
			}
		}
	}

	return 0; 
}


你可能感兴趣的:(Algorithm,C++)