每日一题

59 篇文章 0 订阅
49 篇文章 1 订阅

NowCoder最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。

输入描述:
输入有多组数据,每组数据为一张20×20的棋盘。

其中黑子用“*”表示,白子用“+”表示,空白位置用“”表示。

输出描述:
如果棋盘上存在五子连珠(无论哪种颜色的棋子),输入“是”,否则输出“否”。
示例1
输入

.................... 
.................... 
.......... .......... 
.................... 
...... * ............. 
....... * ............ 
........ * ............ 
++++。* .......... 
.......... * ......... 
.................... 
.................... 
.................... 
.......... .......... 
.................... 
.................... 
.................... 
.................... 
.......... .......... 
.................... 
.................... 
.................... 
.................... 
.......... .......... 
....... * ............ 
...... + * +++ ......... 
...... * ............
....... * ............ 
.................... 
.......... .......... 
.................... 
.................... 
.................... 
.................... 
.......... .......... 
.................... 
.................... 
.................... 
.................... 
.......... .......... 
....................

输出

// write your code here cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int count(string table[], char ch, int x, int y)
{
    int maxc = 0;
    int dir[4][2][2] = { {{ -1,0 },{ 1,0 }},{{ 0,-1 },{ 0,1 }},{{ -1,-1 },{ 1,1 }},{{ -1,1 },{ 1,-1 }} };
    for (int i = 0; i < 4; ++i) // 四种方向
    {
        int c = 0;
        for (int j = 0; j < 2; ++j) // 两个小方向
        {
            int nx = x, ny = y;
            while (nx >= 0 && nx < 20 && ny >= 0 && ny < 20 && table[nx][ny] == ch)
            {
                nx += dir[i][j][0];
                ny += dir[i][j][1];
                ++c;
            }
        }
        maxc = (maxc > c ? maxc : c);
    }
    return maxc - 1;
}
bool solve(string table[])
{
    for(int i = 0; i < 20; ++i)
    {
        for(int j = 0; j < 20; ++j)
        {
            if(table[i][j] == '*' || table[i][j] == '+')
            {
                if(count(table,table[i][j],i,j) >= 5)
                    return true;
            }
        }
    }
    return false;
}
int main()
{
    string table[20];
    while(cin >> table[0])
    {
        for(int i = 1; i < 20; ++i)
            cin >> table[i];
        cout << (solve(table) ? "Yes" : "No") << endl;
    }
    return 0;
}

请你实现一个后缀表达式的计算器。

输入描述:
输入包含多组数据。

每组数据包括两行:第一行是一个正整数n(3≤n≤50);紧接着第二行包含n个由数值和运算符组成的列表。

“+ - * /”分别为加减乘除四则运算,其中除法为整除,即‘5/3 = 1’。

输出描述:
对应每一组数据,输出它们的运算结果。
示例1
输入
3
2 3 +
5
2 2 + 3 *
5
2 2 3 + *
输出
5
12
10

#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        vector<string> vec;
        vec.resize(n);
        for(int i = 0; i < n; ++i)
            cin >> vec[i];
        stack<int> s;
        string str;
        for(int i = 0; i < n; ++i)
        {
            str = vec[i];
            if(!(str == "+" || str == "-" || str == "*" || str == "/"))
                s.push(atoi(str.c_str()));
        else
        {
            int right = s.top();
            s.pop();
            int left = s.top();
            s.pop();
            if(str == "+")
                s.push(left + right);
            else if(str == "-")
                s.push(left - right);
            else if(str == "*")
                s.push(left * right);
            else if(str == "/")
            {
                if(right != 0)
                    s.push(left / right);
                else
                    return 0;
            }
        }
    }
    cout << s.top() << endl;
}
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值