zoukankan      html  css  js  c++  java
  • 牛客网北邮机试习题

    题目部分

    答案部分(按顺序)

     1 #include<iostream>
     2 using namespace std;
     3 string calcu(int n){
     4     int q,r;
     5     string num;
     6     char ch;
     7     while(n>1){
     8         q=n/2;
     9         r=n%2;
    10         ch=r+48;
    11         num=ch+num;
    12         n=q;
    13     }
    14     if(q){
    15         ch=q+48;
    16         num=ch+num;
    17     }
    18    return num;
    19 }
    20 int main(){
    21     int n;cin>>n;
    22     cout<<calcu(n)<<endl;
    23     return 0;
    24 }
     1 #include<iostream>
     2 #include<queue>
     3 #include<vector>
     4 using namespace std;
     5 int main(){
     6     priority_queue<int,vector<int>,greater<int>> p;
     7     int n,w,sum=0;cin>>n;
     8     while(n--){
     9         cin>>w;
    10         p.push(w);
    11     }
    12     int a,b;
    13     while(p.size()>1){
    14         a=(int)p.top();p.pop();
    15         b=(int)p.top();p.pop();
    16         p.push(a+b);
    17         sum=sum+a+b;
    18     }
    19     cout<<sum<<endl;
    20     return 0;
    21 }
     1 #include<iostream>
     2 using namespace std;
     3 int main(){
     4     int n,v;
     5     int sumOdd=0,sumEve=0;
     6     cin>>n;
     7     while(n--){
     8         cin>>v;
     9         if(v%2){
    10             sumOdd++;
    11         }else{
    12             sumEve++;
    13         }
    14     }
    15     if(sumEve>sumOdd){
    16         cout<<"NO"<<endl;
    17     }else{
    18         cout<<"YES"<<endl;
    19     }
    20     return 0;
    21 }
     1 #include<iostream>
     2 #include<set>
     3 #include<iterator>
     4 using namespace std;
     5 int main(){
     6     int n,k,i;cin>>n;
     7     set<int>s;
     8     while(n--){
     9         cin>>k;
    10         s.insert(k);
    11     }
    12     cin>>k;
    13     set<int>::iterator it;
    14     for(it=s.begin(),i=0;i<k-1;i++,it++);
    15          cout<<*it<<endl;
    16     return 0;
    17 }
     1 #include<iostream>
     2 #include<string.h>
     3 using namespace std;
     4 int main(){
     5     int n,k;cin>>n>>k;
     6     int i,j;
     7     int **a=new int*[n];
     8     int **tmp=new int*[n];
     9     int **res=new int*[n];
    10     for(i=0;i<n;i++){
    11         a[i]=new int[n];
    12         tmp[i]=new int[n];
    13         res[i]=new int[n];
    14         for(j=0;j<n;j++){
    15             cin>>a[i][j];
    16             tmp[i][j]=a[i][j];
    17         }
    18     }
    19     int sum;
    20     while(--k){
    21       for(i=0;i<n;i++){
    22           for(j=0;j<n;j++){
    23               sum=0;
    24               for(int m=0;m<n;m++){
    25                   sum=sum+tmp[i][m]*a[m][j];
    26               }
    27               res[i][j]=sum;
    28           }
    29       }
    30         for(i=0;i<n;i++)
    31           for(j=0;j<n;j++){
    32               tmp[i][j]=res[i][j];
    33           }
    34     }
    35     for(i=0;i<n;i++){
    36           for(j=0;j<n;j++){
    37               if(j<n-1){
    38                   cout<<tmp[i][j]<<" ";
    39               }else{
    40                   cout<<tmp[i][j];
    41               }
    42           }
    43         cout<<endl;
    44     }
    45     return 0;
    46 }
     1 #include<iostream>
     2 using namespace std;
     3 //important 坑所在地点,注意不在对角线的情况 
     4 void antiMatrix(int a[5][5],int x,int y,int cnt){
     5     int i,j,bi,bj;
     6     int**b=new int*[cnt];
     7     for(i=0;i<cnt;i++){
     8         b[i]=new int[cnt];
     9     }
    10     for(bi=0,i=x-1;bi<cnt,i<5&&i<x+cnt-1;bi++,i++)//copy
    11         for(bj=0,j=y-1;bj<cnt,j<5&&j<y+cnt-1;bj++,j++)
    12             b[bi][bj]=a[i][j];
    13     for(i=x-1,bi=0;i<5&&i<x+cnt-1,bi<cnt;i++,bi++)//anti
    14         for(j=y-1,bj=0;j<5&&j<y+cnt-1,bj<cnt;j++,bj++)
    15             a[i][j]=b[bj][bi];
    16 }
    17 void inverse(int *a,int n){//一维数组
    18     int *tmp=new int[n];
    19     for(int i=0;i<n;i++)
    20         tmp[i]=a[i];
    21     for(int i=0;i<n;i++)
    22         a[i]=tmp[n-1-i];
    23 }
    24 void rotateClockwise(int a[5][5],int x,int y,int cnt,bool anti){
    25     if(!anti){
    26         antiMatrix(a,x,y,cnt);
    27         for(int i=x-1;i<5&&i<x-1+cnt;i++){
    28             if(y-1+cnt-1<5){
    29                 inverse(&a[i][y-1],cnt);
    30             } else{
    31                 inverse(&a[i][y-1],5-y+1);
    32             }
    33         }
    34     }else{
    35          for(int i=x-1;i<5&&i<x-1+cnt;i++){
    36             if(y-1+cnt-1<5){
    37                 inverse(&a[i][y-1],cnt);
    38             } else{
    39                 inverse(&a[i][y-1],5-y+1);
    40             }
    41         }
    42         antiMatrix(a,x,y,cnt);
    43     }
    44 }
    45   
    46 int main(){
    47     int a[5][5],c[4];
    48     int i,j,x,y;
    49     for(i=0;i<5;i++)
    50         for(j=0;j<5;j++)
    51             cin>>a[i][j];
    52     for(i=0;i<4;i++)
    53         cin>>c[i];
    54     if(c[0]==1&&c[1]==2){//顺时针90 4
    55         rotateClockwise(a,c[2],c[3],2,false);
    56     }else if(c[0]==1&&c[1]==3){//顺时针90 9
    57         rotateClockwise(a,c[2],c[3],3,false);
    58     }else if(c[0]==2&&c[1]==2){//逆时针90 4
    59         rotateClockwise(a,c[2],c[3],2,true);
    60     }else if(c[0]==2&&c[1]==3){//逆时针90.9
    61         rotateClockwise(a,c[2],c[3],3,true);
    62     }
    63      for(i=0;i<5;i++){
    64         for(j=0;j<5;j++)
    65             cout<<a[i][j]<<" ";
    66          cout<<endl;
    67      }
    68     return 0;
    69 }
     1 #include<iostream>
     2 #include<string.h>
     3 using namespace std;
     4 bool check(char key,int n,char *a){
     5     int m[10];
     6     memset(m,0,sizeof(m));
     7     for(int i=0;i<strlen(a);i++)
     8         m[a[i]-48]++;
     9     for(int i=key-48+1;i<10;i++)
    10         if(m[i]>=n)
    11            return true;
    12     return false;
    13 }
    14 bool handler(int key,char *a,char *b){
    15     bool m[10];
    16     memset(m,false,sizeof(m));
    17     for(int i=0;i<strlen(a);i++)
    18         if(a[i]-48>key)
    19             m[a[i]-48]=true;
    20     int sum=0;
    21     for(int i=0;i<10-1;i++){ 
    22         if(sum>=5)
    23         return true;
    24         if(m[i]&&m[i+1])
    25             sum++;
    26         else
    27             sum=0;
    28     } 
    29     if(sum>=5)
    30         return true;
    31     return false;
    32 }
    33 bool fiveHandler(char* a,char* b){
    34     bool flag=false;
    35     switch(b[0]){
    36         case '1':
    37             flag=handler(1,a,b);
    38             break;
    39         case '2':
    40             flag=handler(2,a,b);
    41             break;
    42         case '3':
    43             flag=handler(3,a,b);
    44             break;
    45         case '4':
    46             flag=handler(4,a,b);
    47             break;
    48         default:
    49             flag=false;  
    50     }
    51     return flag;
    52 }
    53 int main(){
    54     char a[100],b[100];
    55     cin>>a>>b;
    56     bool flag=false;
    57     switch(strlen(b)){
    58         case 1:
    59             flag=check(b[0],1,a);
    60             break;
    61         case 2:
    62             flag=check(b[0],2,a);
    63             break;
    64         case 3:
    65             flag=check(b[0],3,a);
    66             break;
    67         case 4:
    68             flag=check(b[0],4,a);
    69             break;
    70         case 5:
    71             flag=fiveHandler(a,b);
    72             break;
    73     }
    74     if(flag)
    75         cout<<"YES"<<endl;
    76     else
    77         cout<<"NO"<<endl;
    78     return 0;
    79 }
     1 #include<iostream>
     2 #include<math.h>
     3 using namespace std;
     4 int main(){
     5     int n,i,d;cin>>n;
     6     int *v=new int[n+1];
     7     for(i=1;i<=n;i++)
     8         cin>>v[i];
     9     cin>>d;
    10     if(pow(2,d-1)>n){
    11         cout<<"EMPTY"<<endl;
    12         return 0;
    13     }
    14     for(i=pow(2,d-1);i<=n&&i<pow(2,d);i++){
    15         if(i==n||i==pow(2,d)-1){
    16             cout<<v[i]<<endl;
    17         }else{
    18             cout<<v[i]<<" ";
    19         }
    20     }
    21     return 0;
    22 }
     1 #include<iostream>
     2 #include<string.h>
     3 #include<string>
     4 #include<algorithm>
     5 using namespace std;
     6 void orderHandler(string order,string &ch){
     7     int i,j,begin,len;
     8     begin=order[1]-'0';
     9     len=order[2]-'0';
    10     if(order[0]=='0'){//reverse
    11        /* char tmp[101];
    12         for(i=begin,j=0;i<begin+len;i++,j++)
    13             tmp[j]=ch[i];
    14         tmp[j]='';
    15         cout<<begin<<" "<<tmp<<endl;
    16         for(i=begin,j=strlen(tmp)-1;i<begin+len,j>=0;i++,j--)
    17             ch[i]=tmp[j];*/
    18         reverse(ch.begin()+begin,ch.begin()+begin+len);
    19     }else if(order[0]=='1'){//replace
    20         /*for(i=begin,j=3;i<ch.length()&&i<begin+len&&j<order.length();i++,j++)
    21             ch[i]=order[j];*/
    22         ch.replace(begin,len,order.substr(3,order.length()-3));
    23     }
    24 }
    25 int main(){
    26     string ch,order;
    27     int n;
    28     while(cin>>ch>>n){
    29         while(n--){
    30             cin>>order;
    31             orderHandler(order,ch);
    32             cout<<ch<<endl;
    33         }
    34     }
    35     return 0;
    36 }
     1 import java.util.Comparator;
     2 import java.util.PriorityQueue;
     3 import java.util.Scanner;
     4 public class Main {
     5     class node{
     6         int a,b,sqr;
     7         String s;
     8         node(){}
     9         node(String str){
    10             s=str;
    11             if(str.contains("+")){
    12                 String []tmp=str.split("\+");
    13                 a=Integer.parseInt(tmp[0]);
    14                 tmp[1]=tmp[1].substring(1, tmp[1].length());
    15                 b=Integer.parseInt(tmp[1]);
    16                 sqr=a*a+b*b;
    17             }
    18         }
    19     };
    20     Scanner scanner;
    21     int n;
    22     String order;
    23     PriorityQueue<node>pq;
    24     Comparator<node>cmp;
    25     Main(){
    26         scanner=new Scanner(System.in);
    27         cmp=new Comparator<node>() {
    28             @Override
    29             public int compare(node o1, node o2) {
    30                 // TODO Auto-generated method stub
    31                 return o2.sqr-o1.sqr;//MaxHeap
    32             }
    33         };
    34         pq=new PriorityQueue<>(cmp);
    35     }
    36     public void func(){
    37         n=scanner.nextInt();
    38         scanner.nextLine();//important
    39         while(n-->0){
    40             order=scanner.nextLine();
    41             if(order.equals("Pop")){
    42                 if(pq.size()==0){
    43                     System.out.println("empty");
    44                 }else{
    45                     System.out.println(pq.poll().s);
    46                     System.out.println("SIZE = "+pq.size());
    47                 }
    48             }else{
    49                 String str=order.split(" ")[1];
    50                 pq.add(new node(str));
    51                 System.out.println("SIZE = "+pq.size());
    52                 
    53             }
    54         }    
    55     }
    56     public static void main(String[] args)throws Exception{
    57         new Main().func();
    58     }
    59 }
     1 #include<iostream>
     2 using namespace std;
     3 struct node{
     4     int val;
     5     node *left,*right;
     6     node(int v):val(v){left=right=NULL;}
     7 };
     8 void insert(node* &root,node* e){
     9      if(root){
    10         if(e->val<root->val)
    11             insert(root->left,e);
    12         else if(e->val>root->val)
    13             insert(root->right,e); 
    14     }else{
    15         root=e;
    16     }
    17 }
    18 void preOrder(node *root){
    19     if(root){
    20         cout<<root->val<<" ";
    21         preOrder(root->left);
    22         preOrder(root->right);
    23     }
    24 }
    25 void inOrder(node *root){
    26     if(root){
    27         inOrder(root->left);
    28         cout<<root->val<<" ";
    29         inOrder(root->right);
    30     }
    31 }
    32 void postOrder(node *root){
    33     if(root){
    34         postOrder(root->left);
    35         postOrder(root->right);
    36         cout<<root->val<<" ";
    37     }
    38 }
    39 int main(){
    40     int n,v;
    41     while(cin>>n){
    42         node *root=NULL;
    43         while(n--){
    44             cin>>v;
    45             insert(root,new node(v));
    46         }
    47         preOrder(root);cout<<endl;
    48         inOrder(root);cout<<endl;
    49         postOrder(root);cout<<endl;
    50     }
    51     return 0;
    52 }
     1 #include<iostream>
     2 using namespace std;
     3 int main(){
     4     int n,x,y;cin>>n;
     5     int minx,miny;minx=miny=0xffffff;
     6     while(n--){
     7         cin>>x>>y;
     8         if(x<minx){
     9             minx=x;
    10             miny=y;
    11         }else if(x==minx){
    12             if(y<miny)
    13                 miny=y;
    14         }
    15     }
    16     cout<<minx<<" "<<miny<<endl;
    17     return 0;
    18 }
     1 #include<iostream>
     2 using namespace std;
     3 bool seek(int *a,int n,int key){
     4     for(int i=0;i<n;i++)
     5         if(a[i]==key)
     6             return true;
     7     return false;
     8 }
     9 int main(){
    10     int n,m;
    11     while(cin>>n){
    12         int a[100],b[100];
    13         for(int i=0;i<n;i++)
    14             cin>>a[i];
    15         cin>>m;
    16         for(int i=0;i<m;i++)
    17             cin>>b[i];
    18         for(int i=0;i<m;i++){
    19             if(seek(a,n,b[i]))
    20                 cout<<"YES"<<endl;
    21             else
    22                 cout<<"NO"<<endl;
    23         }
    24     }
    25 }
  • 相关阅读:
    CSS学习笔记-盒子阴影及文字阴影
    CSS学习笔记-2D转换模块
    CSS学习笔记-过度模块-编写过渡效果
    CSS学习笔记-过渡模块
    Unity3D_Transform_位置、角度、缩放及其他
    微信小程序实现生成画报并且实现下载功能
    前端知识点
    vue导出excel表格用到的两个文件
    react 的高阶组件
    代理的配置
  • 原文地址:https://www.cnblogs.com/yuelien/p/10445209.html
Copyright © 2011-2022 走看看