sicily--1052. Candy Sharing Game

  1. 自我感觉不水,但是看到网上大牛的做法后又觉得自己想得好复杂了

// Problem#: 1052
// Submission#: 1167841
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<vector>
using namespace std;

int StudentNum;
int CandySum;

void move(vector<double>& v)
{
    vector<double> temp = v;
    vector<double>::iterator Viter;
    vector<double>::iterator Titer = temp.begin();
    for(Viter = v.begin(); Viter != v.end(); Viter++)
    {
        if(Titer + 1 == temp.end())
        {
            Viter = v.begin();
            (*Titer) = (*Viter)*(0.5) + (*Titer)*(0.5);
            break;
        }
        else
        {
        double temp1  = (*Viter)*(0.5);
        double temp2 = (*(Viter + 1))*(0.5);
        (*Titer) =  temp1 + temp2;
        
        Titer++;
        }
    }

    v = temp;
    for(Viter = v.begin(); Viter != v.end(); Viter++)
    {
        if((int)(*Viter) % 2 == 1)
        {
            (*Viter)++;
            CandySum++;
        }
    }
}

int main()
{
    while(cin >> StudentNum && StudentNum != 0)
    {
        int i;
        CandySum = 0;
        int MoveCounter = 0;
        bool flag = true;
        vector<double> Candy;
        int candyNum;
        for(i = 0; i < StudentNum; i++)
        {
            cin >> candyNum ;
            CandySum = CandySum + candyNum;
            Candy.push_back(candyNum);
        }

        while(1)
        {
            
            double average = CandySum / StudentNum;
            vector<double>::iterator iter = Candy.begin();
            for(iter = Candy.begin(); iter != Candy.end(); iter++)
            {
                if((*iter) != average)
                {
                    move(Candy);
                    MoveCounter++;
                    flag = false;
                    break;
                }
                flag = true;
            }

            if(flag)
                break;
        }

        cout << MoveCounter << " " << CandySum / StudentNum << endl;
    }
    return 0;
}                                 


你可能感兴趣的:(iterator,sun)