括号匹配

C++

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 bool match(string s)
 5 {
 6    stack<char> sk;
 7    for(int i = 0; i < (int)s.size(); i++){
 8       if(s[i] == '[' || s[i] == '(' )
 9          sk.push(s[i]);
10       else if(sk.empty()
11          ||(s[i] == ')' && sk.top() != '(')
12          ||(s[i] == ']' && sk.top() != '['))
13          return false;
14       else
15          sk.pop();
16    }
17    return sk.empty();
18 }
19 
20 int main()
21 {
22    string s;
23 
24    while(cin >> s){
25       cout << s << endl;
26       cout << match(s) << endl;
27    }
28 
29    return 0;
30 }
31 /*
32 ([]())
33 [([][])]
34 [(])
35 ([()]]
36 (([])
37 */

 

#include <iostream>

using namespace std;
typedef long long ll;

ll F(ll a, ll b, int c)
{
return b / c - (a - 1) / c;
}

int main()
{
ll a, b, c, d;
while(cin >> a >> b >> c >> d){
ll c2018 = F(a, b, 2018);
ll c1009 = F(a, b, 1009) - c2018;
ll c2 = F(a, b, 2) - c2018;
ll ans = c2018 * (d - c + 1) + c1009 * F(c, d, 2) + c2 * F(c, d, 1009) + (b - a + 1 - c2018 - c1009 - c2) * F(c, d, 2018);
cout << ans << endl;
}

return 0;
}

posted on 2018-04-18 20:56 天地过客 阅读(...) 评论(...) 编辑 收藏

导航

公告