当前位置:网站首页>7-2 symbol pairing | PTA
7-2 symbol pairing | PTA
2022-04-22 04:27:00 【The so-called drunk alone】
7-2 Symbol pairing
Please write a program to check C Whether the following symbols are paired in the language source program :/* And */、( And )、[ And ]、{
And }.
Input format :
Input as a C Language source . When you read a line with only one period . And a carriage return , It marks the end of the input . In the program, you need to check that the paired symbols do not exceed 100 individual .
Output format :
First , If all the symbols match correctly , Then output... In the first line YES, Otherwise output NO. Then point out in the second line The first one is not paired The symbol of : If the left symbol is missing , The output ?- Right sign ; If the right symbol is missing , The output Left sign -?.
sample input 1:
void test()
{
int i, A[10];
for (i=0; i<10; i++) { /*/
A[i] = i;
}
.
sample output 1:
NO
/*-?
Answer key
There is an important point in the output format of this question , If there is a mismatch , Pointed out that The first one is not paired The symbol of , therefore , We need to use the stack for this problem
First , We can use a string to filter out the questions from several input strings Detected characters , Considering that only
/* and */ It's two characters , We can , When this pair is detected , use < and > Storage , Convenient judgment , Then use stack , The left side of the filtered string push Into the stack , And decide , Um. , There are many judgments on this question ...
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char> C;
string ss;
string s = ""; // Store the characters to be detected in the input
while(true)
{
getline(cin, ss);
if(ss == ".")
break;
for(int i = 0; i < ss.length(); i++)
{
if(ss[i] == '['|| ss[i] == '{'||ss[i] == '('||ss[i] == ']'||ss[i] == '}'||ss[i] == ')')
s += ss[i];
else if(ss[i] == '/' && ss[i+1] == '*')
{
s += '<';
i++;
}
else if(ss[i] == '*' && ss[i+1] == '/')
{
s += '>';
i++;
}
}
}
for(int i = 0; i < s.length() ; i++)
{
if(s[i] == '<'||s[i] == '{'||s[i] == '('||s[i] == '[')
C.push(s[i]);
else if(C.empty())
{
if(s[i] == '}'||s[i] == ')'||s[i] == ']')
{
cout << "NO" << endl;
cout << "?-" << s[i] << endl;
return 0;
}
else if(s[i] == '>')
{
cout << "NO" << endl;
cout << "?-*/" << endl;
return 0;
}
}
else if(C.top() == '<')
{
if(s[i] == '>')
C.pop();
else
{
cout << "NO" << endl;
cout << "/*-?" << endl;
return 0;
}
}
else if(C.top() == '{')
{
if(s[i] == '}')
C.pop();
else
{
cout << "NO" << endl;
cout << C.top() << "-?" << endl;
return 0;
}
}
else if(C.top() == '(')
{
if(s[i] == ')')
C.pop();
else
{
cout << "NO" << endl;
cout << C.top() << "-?" << endl;
return 0;
}
}
else if(C.top() == '[')
{
if(s[i] == ']')
C.pop();
else
{
cout << "NO" << endl;
cout << C.top() << "-?" << endl;
return 0;
}
}
}
if(!C.empty())
{
if(C.top() == '<')
{
cout << "NO" << endl;
cout << "/*-?" << endl;
}
else
{
cout << "NO" << endl;
cout << C.top() << "-?" << endl;
}
}
else
cout << "YES" << endl;
return 0;
}
版权声明
本文为[The so-called drunk alone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210953474217.html
边栏推荐
- XSS attack principle / classification / defense of Web Security
- 01-Read&Write
- Matplotlib draw 3dbox
- Teach you to develop an image compression tool on the cloud
- OpenSCA版本升级 | OpenSCA v1.0.4版本发布
- 【机器学习】长短时记忆网络(LSTM)
- Provincial competition exercise 2 -- the 8th Fujian college student programming competition & supplementary questions
- DOM event flow and event delegation
- ObjectMapper,别再像个二货一样一直new了
- Gaussian distribution -- Derivation in error measurement
猜你喜欢
随机推荐
spark 安装与使用 educoder
Pod of kubernetes cluster said, can I die with dignity?
L1-050 penultimate string (15 points)
WinPcap get device list
Experts have information | Zhang Zuyou: Tencent cloud devsecops practice and open source governance exploration
Sumo circle driving
Use the nohup command to mount the program and execute it in the background
How do I test the shuttle application? Unit test
Final of the 16th programming competition of Beijing Normal University - reproduction competition & supplementary questions
Summary of knowledge points of objects and classes
【openEuler】Failed to download metadata for repo ‘EPOL‘: Cannot d
Sr-te policy (Cisco) -- supplement
高斯分布——在误差测量中的推导
02-SparkSQL
Introduction to Intel edge software center
If you want to change your career to take the test, I advise you to understand these contents first
英特尔边缘软件中心介绍
Keras deep learning practice (2) -- using keras to construct neural network
04-Functions
Websocket learning








