【csp】2017-9

1、打酱油

题目:

题意:如上。

题解:经典问题。看代码吧。qwq

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<queue>
 6 #include<stack>
 7 #include<cstring>
 8 using namespace std;
 9 #define ll long long 
10 const int maxn = 100+10;
11 
12 int main(){
13     int n;
14     cin>>n;
15 
16     cout<<( (n/50)*7 + (n%50/30)*4 + (n%50%30)/10 )<<endl;
17 
18     return 0;
19 }
View Code

2、公共钥匙盒

题目:

 

题意:题意描述很清楚啦。

题解:一个小型模拟题。。可能第一题太简单了。。第二题其实卡了一下。。考虑还钥匙和借钥匙。每一个都开vector去进行模拟。借就把原数组置0,还就对比原数组在0的位置赋值。

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<vector>
  6 using namespace std;
  7 const int maxn = 1000+10;
  8 
  9 int n,k;
 10 int a[maxn];
 11 
 12 vector<int> re;
 13 vector<int> br;
 14 
 15 struct  node{
 16     int w;
 17     int s;
 18     int t;
 19 }tea[maxn];
 20 
 21 bool cmp(node a,node b){
 22     if(a.s != b.s){
 23         return a.s < b.s;
 24     }
 25     return a.t < b.t;
 26 }
 27 
 28 
 29 void init(int n){
 30     for(int i = 1 ;i <= n ;i++){
 31         a[i] = i;
 32     }
 33 }
 34 
 35 
 36 void findre(int t){
 37     for(int i = 0; i < k ;i++){
 38         if(tea[i].t == t){
 39             re.push_back( tea[i].w);
 40         }
 41     }
 42 }
 43 
 44 void findbr(int t){
 45     for(int i = 0; i < k ;i++){
 46         if(tea[i].s == t){
 47             br.push_back( tea[i].w);
 48         }
 49     }    
 50 
 51 }
 52 
 53 void returnkey(){
 54     sort(re.begin(),re.end());
 55     for( int i = 0; i < re.size(); i++){
 56         for( int j = 1; j <= n; j++){
 57             if(a[j] == 0){
 58                 a[j] = re[i];
 59                 break;
 60             }
 61         }
 62     }
 63     re.clear();
 64 
 65 }
 66 
 67 void borrowkey(){
 68     sort(br.begin(),br.end());
 69     for( int i = 0; i < br.size(); i++){
 70         for( int j = 1; j <= n; j++){
 71             if(a[j] == br[i]){
 72                 a[j] = 0;
 73                 break;
 74             }
 75         }
 76     }
 77     br.clear();    
 78 }
 79 
 80 
 81  int main(){
 82      cin>>n>>k;
 83      int w,s,c;
 84      init(n);
 85      for(int i = 0; i < k ;i++){
 86          cin>>w>>s>>c;
 87          tea[i].w = w;
 88          tea[i].s = s;
 89          tea[i].t = s+c;
 90      }
 91      sort(tea,tea+k,cmp);
 92 
 93      int maxt = 0;
 94      for(int i = 0; i < k; i++){
 95          if(maxt < tea[i].t){
 96              maxt = tea[i].t;
 97          } 
 98      }
 99      //cout<<maxt<<endl;
100 
101      for(int i = 1 ;i <= maxt; i++){
102         findre(i);
103         findbr(i);
104         //
105         if(re.size()){
106                returnkey();
107         }
108         //
109         if(br.size()){            
110             borrowkey();
111             
112         }
113     }
114 
115     for(int i = 1; i <= n ;i++){
116         cout<<a[i]<<" ";
117     }
118     cout<<endl;
119 
120      return 0;
121  }
View Code

3、JSON查询

题目: 

题意:有点被恶心到。其实就是按照规则模拟。规则在图上。

题解:输入的时候一行一行判断。map直接进行映射。

1、{   判断,当前是不是需要成为一个object。存储头。对值的存储进行标记。

2、"  判断是起始的还是结束的。以此获取key值。通过标记判断是对象还是直接映射。

3、}  判断是结束标志,还是对象结束标志。分情况进行取值。

4、后面都是一些字符的特判。稍作理解即可。

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<queue>
  6 #include<stack>
  7 #include<cstring>
  8 #include<map>
  9 
 10 using namespace std;
 11 #define ll long long
 12 const int maxn = 100+10;
 13 
 14 map<string,string> mp;
 15 
 16 int main(){
 17     int n,m;
 18     cin>>n>>m;
 19     string s;
 20     int flag = 1;
 21     string header = "";
 22     string val = "";
 23     string key = "";
 24     cin.get();
 25     while(n--){
 26         getline(cin,s);
 27         int len = s.size();
 28        
 29         for (int i = 0; i < len; i++){
 30             if(s[i] == '{'){
 31                 if(key.size()){
 32                     header =  key;
 33                     header += '.';
 34                 }
 35                 flag = 1;
 36             }
 37 
 38             else if (s[i] == '"'){
 39                 if(val.size()){
 40                     if(flag){
 41                         if(key.size() > 0){
 42                             val = header + val;
 43                         }
 44                         mp[val] = "OBJECT";
 45                         key = val;
 46                     }
 47                     else{
 48                         mp[key] = val;
 49                     }
 50                     val = "";
 51                 }
 52             }  
 53 
 54             else if (s[i] == ':')
 55                 flag = 0;
 56             
 57             else if (s[i] == ',' ){
 58                 flag = 1;
 59             }
 60             
 61             else if(s[i] == '\'){
 62                 i++;
 63                 val += s[i];
 64             }
 65             else if (s[i] == ' ')continue;
 66             
 67             else if (s[i] == '}'){
 68                 int t = header.rfind('.',header.size()-2);
 69                 if(t > 0)
 70                     header = header.substr(0,t+1);
 71                 else
 72                     header = "";
 73             }
 74             else{
 75                 val += s[i];
 76             }
 77             
 78         }
 79         //cout<<key<<" "<<val<<endl;
 80     }
 81 /*
 82     map<string,string> :: iterator it = mp.begin();
 83     while(it != mp.end()){
 84         cout<<it->first<<" "<<it->second<<endl;
 85         it++;
 86     }
 87 
 88 */
 89 
 90     for (int i = 0 ; i < m ; i ++) {
 91         getline(cin,s);
 92          
 93         if(mp.count(s)){
 94             if(mp[s] != "OBJECT")
 95                 cout<<"STRING "<<mp[s]<<endl;
 96             else
 97                 cout<<mp[s]<<endl;
 98         }
 99         else
100             cout<<"NOTEXIST"<<endl;
101     }
102     
103     return 0;
104 }
View Code
原文地址:https://www.cnblogs.com/Asumi/p/9640610.html