• SEUOJ上几道水题


    自己学校OJ上题目的结题报告在网上很难找,所以好歹先贴几个

    地址:http://acm.seu.edu.cn/oj

    1. 校赛题: 特殊的树   这题貌似只能Kruskal,不能prim,虽然同是贪心,但前者每次取最短边,而后者每次取总权值增加最小的边
      #include<iostream>
      #include<cstdio>
      #include<algorithm>
      #include<cstring>
      using namespace std;
      
      const int maxn=200;
      int parent[maxn];
      int Rank[maxn];
      int n,m,ne;//集元素的个数 
      
      struct edgenode{
          int u,v,w;
      };
      
      int find(int x){
          while(parent[x]!=x)   
              x=parent[x];
          return x;
      }
      
      void Union(int x,int y){
          x=find(x);
          y=find(y);
          if(Rank[x]==Rank[y])    //同秩的时候,令y成为x的父节点 
          {
              Rank[y]++;
              parent[x]=y;
          } 
          else if(Rank[x]>Rank[y])
              parent[y]=x;
          else
              parent[x]=y;
      
      }   //加权规则 
      
      void makeSet(){
          for(int i=0;i<n;i++)  //注意下标是0开头还是1开头 
          {
              Rank[i]=0;
              parent[i]=i;
          }
      }
      
      bool cmp (edgenode ed1,edgenode ed2){
          return ed1.w<ed2.w;
      }
      
      int sum;
      int x,y; 
      int main(){
          
          //freopen("test.in","r",stdin);
          //freopen("out.txt","w",stdout);
          int u,v,w;
          while(cin>>n>>m){
              
              edgenode edge[m];
              makeSet();
              
              for(int i=0;i<m;i++){
                  cin>>u>>v>>w;
                  edge[i].u=u;
                  edge[i].v=v;
                  edge[i].w=w;
              }
              
              sum=0;
              sort(edge,edge+m,cmp);  
              int tmp,cnt=1;    
              for(int i=0;i<m;i++){
                
                  x=find(edge[i].u);
                  y=find(edge[i].v);
                  if(x!=y){
                      parent[x]=y;
                      tmp=edge[i].w;
                      sum+=edge[i].w;  
                      cnt++;//新加的 
                      if(cnt>=n)
                          break;
                  }
              }
              cout<<sum<<" "<<tmp<<endl;
          }
          return 0;
      }
      /*
      9 14
      0 1 4
      0 7 8
      1 2 8
      2 3 7
      1 7 11
      2 8 2
      2 5 4
      3 5 14
      3 4 9
      4 5 10
      5 6 2 
      6 7 1
      6 8 6
      7 8 7
      */
      View Code
    2. 校赛题:DG之送外卖 简单贪心
      /*
      ID: neverchanje
      PROG:
      LANG: C++11
      */
      #include<vector>
      #include<iostream>
      #include<cstring>
      #include<string>
      #include<algorithm>
      #include<cmath>
      #include<cstdio>
      #include<set>
      #include<queue>
      #include<map>
      #define INF 0Xfffffffff
      #define ll long long
      using namespace std;
      
      int n,t[100005];
      double res,sum; 
      int main(){
      //    freopen("a.txt","r",stdin);
      //    freopen(".out","w",stdout);
          while(cin>>n){
              if(!n)    break;
              for(int i=0;i<n;i++)
                  scanf("%d",&t[i]);
              sort(t,t+n);
              res=sum=0;
              for(int i=0;i<n;i++)
              {
                  sum+=t[i];
                  res+=sum;
              }
              printf("%.3f
      ",res/n);
          }
          return 0;
      }
      
      /*
      DESCRIPTION:
      贪心 
      */
      View Code
    3. 校赛题:DG之社团调查(并查集求联通分量的个数)
      /*
      ID: neverchanje
      PROG:
      LANG: C++11
      */
      #include<vector>
      #include<iostream>
      #include<cstring>
      #include<string>
      #include<algorithm>
      #include<cmath>
      #include<cstdio>
      #include<set>
      #include<queue>
      #include<map>
      #define INF 0Xfffffffff
      #define ll long long
      using namespace std;
      
      int n,m,cnt;
      int parent[50005];
      
      int find(int x){
          if(x==parent[x])
               return x;
          return x=find(parent[x]);
      }
      
      void uni(int x,int y){
          x=find(x);
          y=find(y);
          if(x!=y){
              parent[x]=y;
          }
      }
      
      int main(){
      //    freopen("a.txt","r",stdin);
      //    freopen(".out","w",stdout);
          while(cin>>n>>m){
              if(!(n|m))    break;
              
              int x,y;
              cnt=0;
              for(int i=1;i<=n;i++)
                  parent[i]=i;
                  
              while(m--){
                  scanf("%d%d",&x,&y);
                  uni(x,y);
              }
              
              for(int i=1;i<=n;i++)
              {
                  if(i==parent[i])
                      cnt++;
              }
              cout<<cnt<<endl;
          }
          return 0;
      }
      
      /*
      DESCRIPTION:
      并查集求连通分量的个数 
      */
      View Code
    4. 93题:编辑距离 (最短编辑距离)算导上的经典DP,跟LCS很像
      //http://ccl.pku.edu.cn/doubtfire/Course/Computational%20Linguistics/contents/Minimum%20Edit%20Distance.pdf
      //最短编辑距离
       
      #include<iostream>
      #include<algorithm>
      #include<cmath>
      #include<cstring>
      using namespace std;
      
      char s1[501],s2[502];
      int l1,l2;
      
      int SED(){ 
          //dp[目标串位置][原串位置]//将原串转换成目标串所需的最少步数 
          int dp[l1+1][l2+1];
          dp[0][0]=0;
          for(int i=1;i<=l1;i++)
              dp[i][0]=i;
          for(int j=1;j<=l2;j++)    
              dp[0][j]=j;
              
          for(int i=1;i<=l1;i++){
              for(int j=1;j<=l2;j++)
              {
                  if(s1[i]==s2[j])
                      dp[i][j]=dp[i-1][j-1];
                  else
                      dp[i][j]=min(dp[i][j-1]+1,min(dp[i-1][j]+1,dp[i-1][j-1]+1));
              }
          }
          return dp[l1][l2];
      } 
      
      int main (){
          
          while(gets(s1+1))
          {
              gets(s2+1);
              l1=strlen(s1+1);
              l2=strlen(s2+1);
              cout<<SED()<<endl;
          }
          return 0;
      } 
      
      /*
      AGTCTGACGC
      AGTAAGTAGGC
      */
      View Code
    5. 校赛题 简单的排列
      #include<iostream>
      #include<cstring>
      #include<algorithm>
      using namespace std;
      
      int t,n;
      int c[11];
      char tmp;
      int main(){//都是小写字母? 
          
          cin>>t;
          while(t--){
              
              cin>>n;
              getchar();
              for(int i=0;i<n;i++){
                  cin>>tmp;
                  getchar();
                  c[i]=tmp-'a';
              }
              sort(c,c+n);
              do{
                  for(int i=0;i<n;i++)
                      printf("%c",c[i]+'a');
                  cout<<endl;
              }while(next_permutation(c,c+n));
              cout<<endl;
          }
      } 
      View Code
    6. 华为杯”苏、鲁高校大学生程序设计大赛总决赛 一道超烦模拟题:12点游戏,不得不说,出的非常好,解法就是枚举,分析,本来可能有54种,这样枚举很辛苦,但是仔细地去重以后会发现并没有那么多种,另外就是题目的格式比较蛋疼,最后一个数据后面不能有空行,不知道不看数据是怎么A掉的。。。

    7. /*
      ID: neverchanje
      PROG:
      LANG: C++11
      */
      #include<vector>
      #include<iostream>
      #include<cstring>
      #include<string>
      #include<algorithm>
      #include<cmath>
      #include<cstdio>
      #include<set>
      #include<queue>
      #include<map>
      #define INF 0Xfffffffff
      #define ll long long
      using namespace std;
      
      int a,b,c;
      int cnt,res;
      
      void swap(int& a,int& b){
          int t=a;a=b;b=t;
      }
      
      int f(int a,int b,int op){ 
          switch(op){
              case 0:
                  return a+b;
              case 1:
                  return a-b;
              case 2:
                  return a*b;
          }
      } 
      
      void solve1(){
          if(a+b+c==12)    cnt++;
          if(a*b*c==12)    cnt++;
          
          if((a+b)*c==12)    cnt++;
          if((a+c)*b==12)    cnt++;
          if((c+b)*a==12)    cnt++;
          
          if((b-a)*c==12)    cnt++;
          if((c-a)*b==12)    cnt++;
          if((c-b)*a==12)    cnt++;
          
          if(c+a*b==12)    cnt++;
          if(-c+a*b==12)    cnt++;
          if(a+b*c==12)    cnt++;
          if(-a+b*c==12)    cnt++;
          if(b+a*c==12)    cnt++;
          if(-b+a*c==12)    cnt++;
          
          if(a+b-c==12)    cnt++;
          if(b+c-a==12)    cnt++;
          if(a+c-b==12)    cnt++;
      }
      
      void solve2(){
          
          if(a+b+c==12)    cnt++;
          if(a*b*c==12)    cnt++;
          
          if((a+b)*c==12)    cnt++;
          if((a+c)*b==12)    cnt++;
          
          if((c-a)*b==12)    cnt++;
          
          if(c+a*b==12)    cnt++;
          if(-c+a*b==12)    cnt++;
          if(a+b*c==12)    cnt++;
          if(-a+b*c==12)    cnt++;
          
          if(a+b-c==12)    cnt++;
          if(b+c-a==12)    cnt++;
      }
      
      void solve3(){
          
          if(a+b+c==12)    cnt++;
          if(a*b*c==12)    cnt++;
          
          if((a+b)*c==12)    cnt++;
          
          if(c+a*b==12)    cnt++;
          if(-c+a*b==12)    cnt++;
          
          if(b+c-a==12)    cnt++;
      }
      
      void solve4(){
          
          if(a+b+c==12)    cnt++;
          
          if((a+b)*c==12)    cnt++;
          if((a+c)*b==12)    cnt++;
          
          if((c-a)*b==12)    cnt++;
      
          if(a+b*c==12)    cnt++;
          if(-a+b*c==12)    cnt++;
          
          if(a+b-c==12)    cnt++;
          if(b+c-a==12)    cnt++;
      }
      
      void solve5(){
          if(a+b+c==12)    cnt++;
          if(a*b*c==12)    cnt++;
          
          if((a+b)*c==12)    cnt++;
          if((c+b)*a==12)    cnt++;
          
          if((b-a)*c==12)    cnt++;
          
          if(c+a*b==12)    cnt++;
          if(-c+a*b==12)    cnt++;
          if(a+b*c==12)    cnt++;
          if(-a+b*c==12)    cnt++;
          
          if(b+c-a==12)    cnt++;
      }
      
      
      int main(){
          freopen("test.in","r",stdin);
          freopen("out.txt","w",stdout);
          
          cin>>a>>b>>c;
          cnt=0;
          cout<<a<<" "<<b<<" "<<c<<" ";
          
          if(a>b) swap(a,b);
          if(b>c)    swap(b,c);
          if(a>b)    swap(a,b);
          
          if(a==b){
              if(a==2)
                  solve4();
              else if(b==c)
                  solve3();
              else
                  solve2();
          } 
          else{
              if(b==c){
                  solve5();
              }
              else
                  solve1();
          }
          cout<<cnt;
          
          while(cin>>a>>b>>c){
              cout<<endl;
              cnt=0;
              cout<<a<<" "<<b<<" "<<c<<" ";
              
              if(a>b) swap(a,b);
              if(b>c)    swap(b,c);
              if(a>b)    swap(a,b);
              
              if(a==b){
                  if(a==2)
                      solve4();
                  else if(b==c)
                      solve3();
                  else
                      solve2();
              } 
              else{
                  if(b==c){
                      solve5();
                  }
                  else
                      solve1();
              }
              cout<<cnt;
          }
          return 0;
      }
      
      /*
      DESCRIPTION: 
      
      */
      View Code
  • 相关阅读:
    《ElasticSearch6.x实战教程》之准备工作、基本术语
    《ElasticSearch6.x实战教程》正式推出
    【好书推荐】《剑指Offer》之硬技能(编程题12~16)
    synchronized凭什么锁得住?
    synchronized到底锁住的是谁?
    Java面试宝典(2020版)
    50道Redis面试题及答案整理,史上最全!
    MySQL面试题及答案整理,史上最全!
    Github 上优秀的 Java 项目推荐
    100道MySQL常见面试题总结
  • 原文地址:https://www.cnblogs.com/neverchanje/p/3653251.html
Copyright © 2020-2023  润新知