zoukankan      html  css  js  c++  java
  • C/C++程序员面试宝典2

    9. 关系数据库的范式

    10. 数据库操作中的事务

    事务的4个特性:

    11. 数字矩阵的生成:

    对应的代码如下:

    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    #define print_arr2d(arr,N) {for(int i=0;i<N;i++){\
        for(int j=0;j<N;j++)\
        cout<<setw(3)<<arr[i][j];\
        cout<<endl;\
    }}
    using namespace std;
    /*
      1  2  9 10 25
      4  3  8 11 24
      5  6  7 12 23
     16 15 14 13 22
     17 18 19 20 21 
     
    */
    void mat_1(){
        const int N=5;
        int arr[N][N];
        int m=1;
        for(int i=0;i<N;i++){
            if(i&0x1){
                for(int j=0;j<=i;j++)
                    arr[j][i]=m++;
                for(int j=i-1;j>=0;j--)
                    arr[i][j]=m++;
            }
            else{
                for(int j=0;j<=i;j++)
                    arr[i][j]=m++;
                for(int j=i-1;j>=0;j--)
                    arr[j][i]=m++;
            }
        }
        print_arr2d(arr,N);
    }
    /*
      1  2  6  7 15
      3  5  8 14 16
      4  9 13 17 22
     10 12 18 21 23
     11 19 20 24 25
    */
    void mat_2(){
        const int N=5;
        int a[N][N];
        int m=1;
        int K=0;
        for(int i=0;i<N;i++,K++){
            for(int j=0;j<=i;j++)
                if(K%2==0)
                    a[K-j][j]=m++;
                else
                    a[j][K-j]=m++;
        }
        for(int i=1;i<N;i++,K++){
            for(int j=i;j<N;j++)
                if(K%2==0)
                    a[K-j][j]=m++;
                else
                    a[j][K-j]=m++;
        }
        print_arr2d(a,N);
    }
    /*
      1  2  3  4  5
     16 17 18 19  6
     15 24 25 20  7
     14 23 22 21  8
     13 12 11 10  9
     
    */
    void mat_3(){
        const int N=6;
        int a[N][N]={0};
        int m=1;
        int K=N/2;
        if(N%2!=0)
            a[K][K]=N*N;
        for(int i=0;i<K;i++) {
            for(int j=i;j<=N-1-i;j++)
                a[i][j]=m++;
        print_arr2d(a,N);
        cout<<"================="<<endl;
            for(int j=i+1;j<=N-1-i;j++)
                a[j][N-1-i]=m++;
        print_arr2d(a,N);
        cout<<"================="<<endl;
            for(int j=N-2-i;j>=i;j--)
                a[N-1-i][j]=m++;
        print_arr2d(a,N);
        cout<<"================="<<endl;
            for(int j=N-2-i;j>=i+1;j--)
                a[j][i]=m++;
        print_arr2d(a,N);
        cout<<"================="<<endl;
        }
        print_arr2d(a,N);
    }
    /*
     13 12 11 10 25
     14  3  2  9 24
     15  4  1  8 23
     16  5  6  7 22
     17 18 19 20 21
     
    */
    void mat_4(){
        const int N=6;
        int a[N][N]={0};
        int m=N*N;
        int K=N/2;
        if(N%2!=0)
            a[K][K]=1;
        for(int i=0;i<K;i++){
            for(int j=i;j<=N-1-i;j++)
                a[j][N-1-i]=m--;
            for(int j=N-2-i;j>=i;j--)
                a[N-1-i][j]=m--;
            for(int j=N-2-i;j>=i;j--)
                a[j][i]=m--;
            for(int j=i+1;j<=N-2-i;j++)
                a[i][j]=m--;
        }
        print_arr2d(a,N);
    }
    int main(){
        mat_4();
    }

     

    12. 大数乘法问题:

    #include <string>
    #include <iostream>
    using namespace std;
    string big_multi(const string& x,
            const string& y){
        int szx=x.size();
        int szy=y.size();
        int *C=new int[szx+szy+1]();
        for(int i=szx-1,k=0;i>=0;i--,k++){
            int carry=0;
            int m=k;
            for(int j=szy-1;j>=0;j--,m++){
                int val=(x[i]-'0')*(y[j]-'0')+carry+C[m];
                C[m]=val%10;
                carry=val/10;
            }
            if(carry!=0)
                C[m]=carry;
        }
        string z;
        int i=szx+szy;
        while(C[i]==0)
            i--;
        for(;i>=0;i--)
            z+=(char)(C[i]+'0');
        delete C;
        return z;
    }
    int main(){
        int a=78,b=42;
        string x="78",y="42";
        string z=big_multi(x,y);
        cout<<z<<"  "<<a*b<<endl;

    }

    13. 递归的方法将两个有序链表进行合并

  • 相关阅读:
    Mysql多个字段同时满足多组条件
    spring-boot 配置Druid监控
    回顾存储过程简单使用
    win10环境下使用docker部署spring-boot项目
    LeetCode 35. 搜索插入位置
    二分查找
    归并排序(二)
    归并排序
    剑指 Offer 68
    剑指 Offer 68
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2720503.html
Copyright © 2011-2022 走看看