zoukankan      html  css  js  c++  java
  • 喵哈哈村的魔法考试 Round #6 (Div.3) 题解


    原题链接: 35~39 http://qscoj.cn/problems/

    A.喵哈哈村的代码传说 第一章 冒泡排序

    分析:排个序输出

    #include "iostream"
    #include "cstdio"
    #include "algorithm"
    #include "cmath"
    using namespace std;
    
    int s[100000+10],n;
    int main(){
      while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)  scanf("%d",s+i);
        sort(s,s+n);
        for(int i=0;i<n;i++)  printf("%d ",s[i]);
        printf("
    ");
      }
      return 0;
    }
    

    B.喵哈哈村的代码传说 第二章 神经网络

    分析:异或运算

    #include "iostream"
    #include "cstdio"
    #include "algorithm"
    #include "cmath"
    using namespace std;
    
    char a[2000],b[2000];
    int n,tmp[2000];
    int main(){
      while(cin>>n){
        for(int i=0;i<n;i++)  cin>>a[i];
        for(int i=0;i<n;i++)  cin>>b[i];
        for(int i=0;i<n;i++){
          tmp[i] = a[i]==b[i]?0:1;
        }
        for(int i=0;i<n;i++) printf("%d",tmp[i]);
        printf("
    ");
      }
      return 0;
    }
    

    C.喵哈哈村的代码传说 第三章 宽度优先搜索

    分析:广搜模板

    #include "iostream"
    #include "cstdio"
    #include "algorithm"
    #include "cmath"
    #include "cstring"
    #include "queue"
    using namespace std;
    
    char Map[200][200];
    int Mark[200][200];
    int n,m,sx,sy,ex,ey,ans;
    struct point{
      int x,y,cnt;
    }s,e;
    // 顺时针
    int dirx[4]={0,0,1,-1};
    int diry[4]={1,-1,0,0};
    bool bfs(){
      queue<point> q;
      Mark[s.x][s.y]=1;  s.cnt=0;
      q.push(s);
      while(!q.empty()){
        point t = q.front();
        q.pop();
        for(int i=0;i<4;i++){
          point now;
          now.x = t.x+dirx[i];  now.y = t.y+diry[i];
          if(now.x<0 || now.x>=n || now.y<0 || now.y>=m) continue;
          if(Map[now.x][now.y]=='1' && !Mark[now.x][now.y]){
            Mark[now.x][now.y]=1;
            now.cnt = t.cnt+1;
            q.push(now);
            if(now.x==e.x&& now.y==e.y){ printf("%d
    ",now.cnt); return true;}
          }
        }
      }
      return false;
    }
    int main(){
      while(cin>>n>>m>>sx>>sy>>ex>>ey){
        memset(Mark,0,sizeof(Mark));
        for(int i=0;i<n;i++)
          for(int j=0;j<m;j++)
            cin>>Map[i][j];
        s.x=sx-1, s.y=sy-1, s.cnt=0,e.x=ex-1, e.y=ey-1;
        if(!bfs()) cout<<"-1"<<endl;
      }
      return 0;
    

    }

    D.喵哈哈村的代码传说 第四章 并查集

    分析:并查集模板,蜜汁WA,比赛时怎么交怎么WA,出来就AC...

    #include "iostream"
    #include "cstdio"
    #include "algorithm"
    #include "cmath"
    using namespace std;
    
    const int max_n = 100000+10;
    int par[max_n];
    int Rank[max_n];
    
    void init(int n){
      for(int i=0;i<=n;i++) { par[i]=i; Rank[i]=0; }
    }
    int find(int x){
      if(par[x]==x) return x;
      else {
        return par[x]=find(par[x]);
      }
    }
    void unite(int x,int y){
      x=find(x);  y=find(y);
      if(x==y)  return;
      if(Rank[x]<Rank[y]){
        par[x]=y;
      }else{
        par[y]=x;
        if(Rank[x]==Rank[y])  Rank[x]++;
      }
    }
    bool same(int x,int y){
      return find(x)==find(y);
    }
    int main(){
      int n,m,ask,x,y;
      while(cin>>n>>m){
        init(n);
        for(int i=0;i<m;i++){
          cin>>ask>>x>>y;
          if(ask & 1){
            unite(x,y);
          }else{
            if(same(x,y))  cout<<"Yes
    ";
            else           cout<<"No
    ";
          }
        }
      }
      return 0;
    }
    

    E.喵哈哈村的代码传说 第五章 找规律

    分析:裸SG函数
    sg[0]=0
    当x=8k+7时sg[x]=8k+8,
    当x=8k+8时sg[x]=8k+7,
    其余时候sg[x]=x;(k>=0)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        int T,n;
        scanf("%d",&T);
        while(T--)
        {
            int ans=0;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                int x,sg;
                scanf("%d",&x);
                if(x%8!=0&&x%8!=7)
                    sg=x;
                else
                    if(x%8==0) sg=x-1;else sg=x+1;
                ans^=sg;
            }
            if(ans) printf("First player wins.
    ");else printf("Second player wins.
    ");
        }
        return 0;
    }
    如要转载请注明转载出处:http://www.cnblogs.com/WArobot
  • 相关阅读:
    yii2 gii 命令行自动生成控制器和模型
    控制器中的方法命名规范
    Vue Property or method "" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based
    IDEA插件:GsonFormat
    Spring Boot : Access denied for user ''@'localhost' (using password: NO)
    Typora添加主题
    Git基础命令图解
    Java Joda-Time 处理时间工具类(JDK1.7以上)
    Java日期工具类(基于JDK1.7版本)
    Oracle SQL Developer 连接Oracle出现【 状态: 失败 -测试失败: ORA-01017: invalid username/password; logon denied】
  • 原文地址:https://www.cnblogs.com/WArobot/p/6538569.html
Copyright © 2011-2022 走看看