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;
}