关闭

我解2006百度之星程序设计大赛之蝈蝈计分

2115人阅读 评论(2) 收藏 举报

#include <string>
#include <vector>
#include <fstream>
#include <queue>
#include <iostream>
#include <stdio.h>
#include "math.h"

using namespace std;

struct score
{
    char left;
    char right;
};

vector< vector<char> > record;
queue< vector<char> > testRecord;
vector< vector<char> > finalTestRecord;

bool ReachScore(const char& left, const char& right)
{
    if(left >= 21)
    {
        if(right <= 19)
        {
            return true;
        }
    }
    if(right >= 21)
    {
        if(left <= 19)
        {
            return true;
        }
    }

    if((left >= 22) && (right == left -2))
    {
        return true;
    }
    if((right >= 22) && (left == right - 2))
    {
        return true;
    }

    return false;
}

vector<score> GetScore(vector<char> vec)
{
    char left = 0;
    char right = 0;
    vector<score> scoreVec;
    score mk;
    for(size_t i = 0; i < vec.size(); ++i)
    {
        if(i % 2 == 0)
        {
            left += vec.at(i);
        }
        else
        {
            right += vec.at(i);
        }
        if(ReachScore(left, right) == true)
        {
            mk.left = left;
            mk.right = right;
            scoreVec.push_back(mk);
            left = 0;
            right = 0;
        }
        else if (i == vec.size() - 1)
        {
            scoreVec.clear();
            return scoreVec;
        }

    }
    int nLeftScore = 0;
    int nRightScore = 0;
    for(vector<score>::iterator iter = scoreVec.begin();
        iter != scoreVec.end();
        ++iter)
    {
        if((*iter).left > (*iter).right)
        {
            ++nLeftScore;
        }
        else
        {
            ++nRightScore;
        }
    }
    if((nLeftScore == 3 && nRightScore < 3) ||
        (nRightScore == 3 && nLeftScore < 3))
    {
        return scoreVec;
    }
    else
    {
        scoreVec.clear();
        return scoreVec;
    }
}

void GetScore()
{
    vector<score> temp;
    vector<char> chvec;

    for(size_t i = 0; i < testRecord.size(); ++i)
    {
        chvec = testRecord.front();
        testRecord.pop();
        temp = GetScore(chvec);
        if(temp.size() == 0)
        {
            cout << "/nUNKNOWN/n/n";
            return;
        }
        else
        {
            for(vector<score>::iterator iter = temp.begin(); iter != temp.end(); ++iter)
            {
                cout << (int)(*iter).left << ":" << (int)(*iter).right << endl;
            }
            return;
        }
    }
}

int main(int argc, char* argv[])
{
    ifstream is(argv[1], ios::in | ios::binary);
    int nMatch = 0;
    is >> nMatch;
    for(int nMatchIndex = 0; nMatchIndex < nMatch; ++nMatchIndex)
    {
        int nChar = 0;
        is >> nChar;
        vector<char> recordPerMatch;
        unsigned char ch = 0;
        for(int nCharIndex = 0; nCharIndex < nChar; ++nCharIndex)
        {
            is >> ch;
            if(ch != 'X')
            {
                recordPerMatch.push_back(ch - 48);
            }
            else
            {
                recordPerMatch.push_back('X');
            }
        }
        record.push_back(recordPerMatch);
        recordPerMatch.clear(); 
    }

    for(int nMatchIndex = 0; nMatchIndex < nMatch; ++nMatchIndex)
    {
        testRecord.push(record[nMatchIndex]);
        for( ; ; )
        {
            vector<char> temp = testRecord.front();
            vector<char> temp1(temp);
            vector<char> temp2(temp);
            bool tag = false;
            for(vector<char>::iterator iter = temp1.begin();
                iter != temp1.end();
                ++iter)
            {
                if((*iter) == 'X')
                {
                    testRecord.pop();
                    (*iter) = 10;
                    tag = true;
                    break;
                }       
            }

            if(tag == false)
            {
                finalTestRecord.push_back(temp1);
                break;
            }
            else
            {
                testRecord.push(temp1);

                for(vector<char>::iterator iter = temp2.begin();
                    iter != temp2.end();
                    ++iter)
                {
                    if((*iter) == 'X' && (iter+1) != temp2.end())
                    {
                        (*iter) = 10 + (*(iter + 1));
                        temp2.erase(iter + 1);
                        break;
                    }
                }
                testRecord.push(temp2);
            }
        }
        GetScore();
        while(testRecord.size())
        {
            testRecord.pop();
        }
    }

    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95142次
    • 积分:1237
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:3篇
    • 译文:0篇
    • 评论:59条
    文章分类
    最新评论
    交通查询