Loading

AcWing.3370 牛年

题目链接

https://www.acwing.com/problem/content/3373/

题目思路

用哈希表存储生肖对应年, 读入字符串并过滤掉多余内容, 通过判断 previous和 next 求与Bessie的相对年龄差
需要用 (j % 12 + 12) % 12 来处理负数情况.因为最后答案是相对差,所以求绝对值.

题目代码

#include <iostream>
#include <algorithm>
#include <map>

using namespace std;
string res[13] = {"Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig", "Rat"};
map<string, int> year;
int n;

int main()
{
    year["Bessie"] = 0;
    cin >> n;
    string a, b, c, d;
    int j = 0;
    for(int i = 1; i <= n; i ++ )
    {
        cin >> a >> b >> b >> b >> c >> d >> d >> d;
        if(b == "previous") 
        {
            for(j = year[d] - 1; ; j -- ) if(res[(j % 12 + 12) % 12] == c) break;
            year[a] = j;
        }
        else 
        {
            for(j = year[d] + 1; ; j ++ ) if(res[(j % 12 + 12) % 12] == c) break;
            year[a] = j;
        }
    }
    
    cout << abs(year["Elsie"]) << endl;
    return 0;
}
posted @ 2022-03-16 21:02  vacilie  阅读(13)  评论(0编辑  收藏  举报