zoukankan      html  css  js  c++  java
  • 大一训练赛-20180929-整套代码

    A-JiaoZhu and SC用map直接模拟存名字,输出即可

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<unordered_map>
    using namespace std;
    int main(){
      int n,m;
      unordered_map<string,char> p;
      string a;
      char c;
      while(~scanf("%d%d",&n,&m)){
         for (int i=1;i<=n;i++){
           cin>>a>>c;
           p[a]=c;
         }
         string b;
         char k,kk;
         while(m--){
           cin>>a;
           cin>>b;
           k=p[a];
           kk=p[b];
           if((k=='T' && kk=='Z')|| (k=='Z' && kk=='P') || (k=='P' && kk=='T')){
             printf("XiaoM Wins!
    ");
           }else if((k=='T' && kk=='T')||(k=='Z' && kk=='Z')||(k=='P' && kk=='P')){
             printf("End in a draw!
    ");
           }else printf("TianT Wins!
    ");
         }
    
      }
      return 0;
    }
    View Code

    B-逃课的孩子时间卡的比较紧张,卡了cin和cout,可以用scanf("%s")写字符串,然后直接赋值给string,用map存即可

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<unordered_map>
     6 using namespace std;
     7 int main(){
     8    int n,m;
     9    char a[20];
    10    while(~scanf("%d%d",&n,&m)){
    11 
    12       unordered_map<string,int>p;
    13       for (int i=1;i<=n;i++){
    14         scanf("%s",a);
    15          p[a]=1;
    16       }
    17       while(m--){
    18        scanf("%s",a);
    19        if (p[a]){
    20         printf("yes
    ");
    21        }else {
    22         printf("no
    ");
    23        }
    24       }
    25 
    26 
    27    }
    28   return 0;
    29 }
    View Code

    C-萌萌哒十五酱的衣服~multiset的用法,注意读题,然后说一下函数

    函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置。

    函数upper_bound()在first和last中的前闭后开区间进行二分查找,返回大于val的第一个元素位置。如果所有元素都小于val,则返回last的位置。

    最后提醒一下。。。注意set.end()里面是没有元素的

     1 #include<iostream>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<stdio.h>
     5 #include<set>
     6 using namespace std;
     7 const long long mod = 1000000;
     8 int main()
     9 {
    10     int n;
    11     int tmp1,tmp2;
    12     multiset<int>p;
    13     while(~scanf("%d",&n))
    14     {
    15         p.clear();
    16         int flag;
    17         long long sum=0;
    18         for (int i=1;i<=n;i++)
    19         {
    20             scanf("%d%d",&tmp1,&tmp2);
    21             if (i==1 || p.empty())
    22             {
    23                 flag=tmp1;
    24                 p.insert(tmp2);
    25             }
    26             else
    27             {
    28                 if (tmp1==flag)
    29                 {
    30                     p.insert(tmp2);
    31                 }
    32                 else
    33                 {
    34                     multiset<int>::iterator it,itt;
    35                     it=p.lower_bound(tmp2);
    36                     itt=it;
    37                     itt--;
    38                     if (it==p.begin()){
    39                         sum=sum+abs(*it-tmp2);
    40                         p.erase(it);
    41                     }else if (it==p.end()){
    42 
    43                         sum=sum+abs(*itt-tmp2);
    44                         p.erase(itt);
    45                     }else{
    46                        if(abs(*itt-tmp2)<=abs(*it-tmp2)){
    47                            sum=(sum+abs(*itt-tmp2))%mod;
    48                            p.erase(itt);
    49                        }else {
    50                            sum=(sum+abs(*it-tmp2))%mod;
    51                            p.erase(it);
    52                        }
    53                     }
    54                 }
    55             }
    56         }
    57                printf("%lld
    ",sum%mod);
    58     }
    59     return 0;
    60 }
    View Code

    D-店长终极推荐map存次数+迭代器遍历

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<set>
     5 #include<map>
     6 using namespace std;
     7 map<string,int>p;
     8 set<string> s;
     9 int main()
    10 {
    11     string a;
    12     int t;
    13     while(~scanf("%d",&t) && t){
    14         while(t--){
    15             getchar();
    16             p.clear();
    17             s.clear();
    18             getline(cin,a);
    19             int len=a.length();
    20             int maxx=0;
    21             for (int i=0;i<len-1;i++){
    22                 string k=a.substr(i,2);
    23                 p[k]++;
    24                 maxx=max(p[k],maxx);
    25             }
    26             map<string,int>::iterator it;
    27             for (it=p.begin();it!=p.end();it++){
    28                 if (it->second==maxx){
    29                     string f=it->first;
    30                     s.insert(f);
    31                 }
    32             }
    33             cout<<*s.begin()<<endl;
    34         }
    35         printf("
    ");
    36     }
    37     return 0;
    38 }
    View Code

    E-JiaoZhu and CS直接结构体排序即可

     1 #include<iostream>
     2 #include<string.h>
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<set>
     6 #include<algorithm>
     7 using namespace std;
     8 const int maxx = 100003;
     9 struct node{
    10   int a,w;
    11   char name[15];
    12 }gun[maxx];
    13 bool cmp(node x,node y){
    14    if (x.a==y.a){
    15        if(x.w==y.w){
    16          return strcmp(x.name,y.name)<0;
    17        }else {
    18          return x.w<y.w;
    19        }
    20    }else {
    21       return x.a>y.a;
    22    }
    23 }
    24 int main(){
    25    int n;
    26    while(~scanf("%d",&n)){
    27     getchar();
    28      for (int i=1;i<=n;i++){
    29         scanf("%s%d%d",gun[i].name,&gun[i].a,&gun[i].w);
    30      }
    31      sort(gun+1,gun+1+n,cmp);
    32      for (int i=1;i<=n;i++){
    33         printf("%s
    ",gun[i].name);
    34      }
    35    }
    36   return 0;
    37 }
    View Code

    F-ASCII Addition这题大模拟。。。,我的方法可能比较笨,就是写按照123...矩阵的不同,一个一个把数识别出来,然后存起来,转换成数字进行加和,再写一个字符串数组去表示每个数字,然后用stack倒序存,并输出即可

    思路还是比较简单,就是操作可能比较麻烦

    ps:我本来早早的写出来了。。。但是数组开小了。。。找了半天在bug。。。。一定长记性。。。

      1 #include<iostream>
      2 #include<string.h>
      3 #include<stdio.h>
      4 #include<string.h>
      5 #include<set>
      6 #include<algorithm>
      7 #include<stack>
      8 #define ll long long
      9 using namespace std;
     10 char a[30][700];
     11 string s[10]={"xxxxx.x...x.x...x.x...x.x...x.x...x.xxxxx.",
     12               "....x.....x.....x.....x.....x.....x.....x.",
     13               "xxxxx.....x.....x.xxxxx.x.....x.....xxxxx.",
     14               "xxxxx.....x.....x.xxxxx.....x.....x.xxxxx.",
     15               "x...x.x...x.x...x.xxxxx.....x.....x.....x.",
     16               "xxxxx.x.....x.....xxxxx.....x.....x.xxxxx.",
     17               "xxxxx.x.....x.....xxxxx.x...x.x...x.xxxxx.",
     18               "xxxxx.....x.....x.....x.....x.....x.....x.",
     19               "xxxxx.x...x.x...x.xxxxx.x...x.x...x.xxxxx.",
     20               "xxxxx.x...x.x...x.xxxxx.....x.....x.xxxxx.",
     21               };
     22 ll qpow(int a,int b)
     23 {
     24     ll ans=1;
     25     while(b)
     26     {
     27         if (b&1)ans=ans*a;
     28         a=a*a;
     29         b/=2;
     30     }
     31     return ans;
     32 }
     33 int ju(int c)
     34 {
     35     if (a[0][c]=='.')
     36     {
     37         if (a[1][c+2]=='.')
     38             return 1;
     39         else
     40             return -1;
     41     }
     42     if (a[0][c+1]=='.')
     43     {
     44         return 4;
     45     }
     46     else
     47     {
     48         if (a[1][c+4]=='.')
     49         {
     50             if (a[4][c]=='.')
     51             {
     52                 return 5;
     53             }
     54             else
     55             {
     56                 return 6;
     57             }
     58         }
     59         else
     60         {
     61             if (a[4][c]=='.')
     62             {
     63                 if (a[1][c]=='.'){
     64                     if (a[6][c+2]=='.')
     65                         return 7;
     66                     else
     67                         return 3;
     68                 }
     69                 else
     70                     return 9;
     71             }
     72             else
     73             {
     74                 if (a[3][c+2]=='.')
     75                 {
     76                     return 0;
     77                 }
     78                 else
     79                 {
     80                     if (a[4][c+4]=='.')
     81                        return 2;
     82                     else
     83                        return 8;
     84                 }
     85             }
     86         }
     87     }
     88 }
     89 int main()
     90 {
     91     while(~scanf("%s",a[0]))
     92     {
     93         for (int i=1; i<7; i++)
     94         {
     95             scanf("%s",a[i]);
     96         }
     97         int l=strlen(a[0]);
     98         int c=0;
     99         for (int i=0; i<l; i+=6)
    100         {
    101 
    102             if (a[1][i+2]=='x')
    103             {
    104                 break;
    105             }
    106             c++;
    107         }
    108 
    109         int sumc=(l+1)/6-1;
    110         int bac=sumc-c;
    111         c--;
    112         bac--;
    113         long long sum=0;
    114         int flag=0;
    115         for (int i=0; i<l; i+=6)
    116         {
    117             int tmp=ju(i);
    118            // cout<<tmp<<" ";
    119             if (tmp==-1){
    120                 flag=1;
    121                 continue;
    122             }
    123             if (flag==0){
    124               sum=sum+(ll)tmp*qpow(10,c);
    125               c--;
    126             }else {
    127               sum=sum+(ll)tmp*qpow(10,bac);
    128               bac--;
    129             }
    130         }
    131        // cout<<sum<<endl;
    132         int ss[30];
    133         int k=0;
    134         stack<int>sta;
    135         while(sum!=0){
    136            sta.push(sum%10);
    137            sum/=10;
    138         }
    139         int f=1;
    140         while(!sta.empty()){
    141             if (f==1 && sta.top()==0){
    142                 sta.pop();
    143                 continue;
    144             }else {
    145                 f=0;
    146             }
    147             k++;
    148             ss[k]=sta.top();
    149             sta.pop();
    150         }
    151         for (int i=0;i<7;i++){
    152            for (int j=1;j<=k;j++){
    153              int num=ss[j];
    154              if (j==k)
    155                 cout<<s[num].substr(i*6,5);
    156             else
    157              cout<<s[num].substr(i*6,6);
    158            }
    159            cout<<endl;
    160         }
    161     }
    162     return 0;
    163 }
    164 /*
    165 xxxxx.xxxxx.......xxxxx.....x
    166 x...x.x...x...x...x...x.....x
    167 x...x.x...x...x...x...x.....x
    168 x...x.xxxxx..xxx..x...x.....x
    169 x...x.....x...x...x...x.....x
    170 x...x.....x...x...x...x.....x
    171 xxxxx.xxxxx.......xxxxx.....x
    172 */
    View Code

    G-Zeroes只有5的倍数可以让0上升,所以直接算l到r区间有多少个5的倍数即可

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<algorithm>
     4 #include<string.h>
     5 #include<math.h>
     6 using namespace std;
     7 int main(){
     8   long long a,b;
     9   while(~scanf("%lld%lld",&a,&b)){
    10      if (a==0 && b==0)break;
    11      long long maxx=max(a,b)/5;
    12      long long minn=min(a,b)/5;
    13      printf("%lld
    ",maxx-minn+1);
    14 
    15   }
    16   return 0;
    17 }
    View Code

    H-Train Problem I用栈去模拟,首先遍历第一个01序列,然后看栈顶和它是不是一样,不一样就压进栈,否则就出栈,最后看能不能让栈清空。用一个flag数组标记整个过程的操作就行了。

    ps:为啥过的人这么少。。。我就过这题过的最快。。。

     1 #include<iostream>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<stdio.h>
     5 #include<stack>
     6 using namespace std;
     7 string a,b;
     8 int main(){
     9   int n;
    10   while(~scanf("%d",&n)){
    11     cin>>a;
    12     cin>>b;
    13     int len=a.length();
    14     stack<char> sta;
    15     int p=0;
    16     int flag[1000];
    17     int cnt=0;
    18     for (int i=0;i<len;i++){
    19       sta.push(a[i]);
    20       flag[cnt]=0;
    21       cnt++;
    22       while(!sta.empty()&&sta.top()==b[p]){
    23         flag[cnt]=1;
    24         cnt++;
    25         p++;
    26         sta.pop();
    27       }
    28     }
    29     if(sta.empty()){
    30         printf("Yes.
    ");
    31         for (int i=0;i<cnt;i++){
    32             if (flag[i]==0){
    33                 printf("in
    ");
    34             }else {
    35                 printf("out
    ");
    36             }
    37         }
    38     }
    39     else {
    40        printf("No.
    ");
    41     }
    42     printf("FINISH
    ");
    43   }
    44   return 0;
    45 }
    View Code

    I-士兵队列训练问题用队列模拟,不断循环的往队列里面放东西,用一个0去分开是这次报数还是下一次。。。注意题意思是让你在每一轮后在判断人数是否小于等于3,所以可能出现N很大,但最后只剩下一个或者两个

     1 #include<iostream>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<stdio.h>
     5 #include<stack>
     6 using namespace std;
     7 string a,b;
     8 int main(){
     9   int n;
    10   while(~scanf("%d",&n)){
    11     cin>>a;
    12     cin>>b;
    13     int len=a.length();
    14     stack<char> sta;
    15     int p=0;
    16     int flag[1000];
    17     int cnt=0;
    18     for (int i=0;i<len;i++){
    19       sta.push(a[i]);
    20       flag[cnt]=0;
    21       cnt++;
    22       while(!sta.empty()&&sta.top()==b[p]){
    23         flag[cnt]=1;
    24         cnt++;
    25         p++;
    26         sta.pop();
    27       }
    28     }
    29     if(sta.empty()){
    30         printf("Yes.
    ");
    31         for (int i=0;i<cnt;i++){
    32             if (flag[i]==0){
    33                 printf("in
    ");
    34             }else {
    35                 printf("out
    ");
    36             }
    37         }
    38     }
    39     else {
    40        printf("No.
    ");
    41     }
    42     printf("FINISH
    ");
    43   }
    44   return 0;
    45 }
    View Code

    总结:我自己的STL用的也不是太好,做事情还是太拖沓,希望未来能更加努力!!!

    人一我十!人百我千! 最后祝我自己和大家哈哈,国庆快乐!!!

    岂能尽如人意,但求无愧我心by bluefly

    有不懂欢迎咨询 QQ:1326487164(添加时记得备注)
  • 相关阅读:
    设计算法,求AB两个整数集合的交集
    C++函数传递指针面试题
    C++构造函数、析构函数与抛出异常
    c++ 虚函数表解析
    c++ 对象的内存布局
    09 构造函数能调用虚函数吗?
    C++构造函数初始化顺序
    计数排序,基数排序和桶排序
    80 求论坛在线人数
    79 两个整数集合A和B,求其交集
  • 原文地址:https://www.cnblogs.com/bluefly-hrbust/p/9733321.html
Copyright © 2011-2022 走看看