Solution of 847 - A Multiplication Game

Problem Description
source:https://uva.onlinejudge.org/external/8/847.html

Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p ≥ n. 

Input and Output

 Each line of input contains one integer number n. For each line of input output one line either 


Stan wins. 

or 

Ollie wins. 

assuming that both of them play perfectly. 

Sample input

162
17
340
12226

Sample Output 

Stan wins.
Ollie wins.
Stan wins.

Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    long int i,p,n;
    while(scanf("%ld",&n)==1)
    {
        p=1;
        i=1;
        while(1)
        {
            if(i%2==1)
                p*=9;
            else
                p*=2;

            if(p>=n)
                break;
            i+=1;
        }
        if(i%2==1)
            printf("Stan wins.\n");
        else
            printf("Ollie wins.\n");
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience