# 笔试小难题

xiaoxiao2021-02-28  6

# 1. 蛇形矩阵

n=3

output:

1 2 3

8 9 4

7 6 5

http://blog.csdn.net/z_x_b5/article/details/51056536?locationNum=1

[cpp]  view plain  copy #include <stdio.h>      int Ori_width,New_width;        void resort(int *temp, int *temp1){      int i,j;            if(New_width == 1 || New_width == 0){          if(New_width == 0)              return;          *temp1 = Ori_width*Ori_width;          return;      }      else      {   //right          for(i=0;i<New_width;i++)              *(temp1+i)=*(temp+i);                            //down           for(i=0;i<New_width-1;i++)              *(temp1+New_width-1+(i+1)*Ori_width)=*(temp+New_width+i);                                          //left          for(i=0;i<New_width-1;i++)              *(temp1+Ori_width*(New_width-1)+New_width-2-i)=*(temp+2*New_width-1+i);                    //up          for(i=0;i<New_width-2;i++)              *(temp1+(New_width-2-i)*Ori_width)=*(temp+3*New_width-2+i);            New_width -= 2;          resort(temp+4*(New_width+1),temp1+Ori_width+1);                 }            }    int main()  {            int i,j;      printf("hello world!\n");      while(1){          printf("****************\n");          printf("****************\n");          printf("Please input your width of matrix: ");                    scanf("%d",&Ori_width);           New_width = Ori_width;          printf("width is %d\n",Ori_width);          do{              int Ori_matrix[Ori_width][Ori_width],Sorted_matrix[Ori_width][Ori_width];                            for(i=1;i<=Ori_width;i++){                  for(j=1;j<=Ori_width;j++){                      Ori_matrix[i-1][j-1] = (i-1)*Ori_width + j;                      Sorted_matrix[i-1][j-1] = Ori_matrix[i-1][j-1];                   }              }                 resort(*Ori_matrix,Sorted_matrix[0]);              for(i=1;i<=Ori_width;i++){                  for(j=1;j<=Ori_width;j++){                      printf("%d ",Sorted_matrix[i-1][j-1]);                  }                  printf("\n");              }              printf("\n");          }while(0);      }            return 0;  }   运行结果：

2. java知识考察

[java]  view plain  copy public static void main(String[] args){      Integer i1=127,i2=127,i3=128,i4=128;      System.out.println(i1==i2);      System.out.println(i1.equals(i2));      System.out.println(i3==i4);      System.out.println(i3.equals(i4));  }   请问代码最终输出情况：

3.数据库知识

4.二叉树遍历

bcdea

5.IP知识

255.255.240.0

IP地址159.226.181.1是____A类？B类？C类？D类？IP地址。

B类

6.指针数组

[cpp]  view plain  copy int a[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  int (*ptr)[3];  ptr = (int (*)[3])a;  printf("result is %d\n",a[3][1] - ptr[3][1]);   答案是： 3

1 2 3

4 5 6

7 8 9

10 11 12

...

14-11=3

7.进程死锁

16

8.可能出栈顺序

9.简单练习——C调序

1,2,3,4

1,3,2,4

[cpp]  view plain  copy #include <stdio.h>    #define MAX_LEN 1000    int main()  {      char end;      int arr[MAX_LEN];      int temp=0,i =0,j=0,k=0;      do{          scanf("%d",&arr[i++]);          if(arr[i-1]>100)              arr[i-1]=100;          else if(arr[i-1]<1)              arr[i-1] = 1;      }while((end = getchar())!='\n');            while(1)      {          for(j=0;j<i;j++)          {              if(arr[j]%2==0)                  break;          }                    for(k=i-1;k>0;k--)          {              if(arr[k]%2==1)                  break;            }          if(j<k)          {              temp=arr[j];              arr[j]=arr[k];              arr[k]=temp;          }          else          {              break;          }      }             for(j=0;j<i;j++)          if(j!=(i-1))              printf("%d,",arr[j]);          else              printf("%d\n",arr[j]);      return 0;  }   运行结果：

10.简单练习——二维卷积

2 3 3

1 2 1 0 2 2 2 1 1

-1 1 1 -1

-1 -1 3 0

[cpp]  view plain  copy #include <stdio.h>    #define MAX_LEN 1000    int main()  {      int temp[3];      int k,m,n;      int i,j,row,col;      for(i=0;i<3;i++){          scanf("%d",&temp[i]);          if(temp[i]>100)              temp[i] = 100;          else if(temp[i]<1)              temp[i] = 1;      }      k=temp[0];      m=temp[1];      n=temp[2];      //:can delete         if(m<=k)          m=k+1;      if(n<=k)          n=k+1;      //:~          do{          int A[m][n];//input          int h[k][k],t[k][k];          int B[m-k+1][n-k+1];//output                    for(row=0;row<(m-k+1);row++)              for(col=0;col<(n-k+1);col++)                  B[row][col] = 0;                    for(i=0;i<m;i++){              for(j=0;j<n;j++){                  scanf("%d",&A[i][j]);                  if(A[i][j]>100)                      A[i][j] = 100;                  else if(A[i][j]<-100)                      A[i][j]=-100;              }          }                           for(i=0;i<k;i++){              for(j=0;j<k;j++){                  scanf("%d",&h[i][j]);                  if(h[i][j]>100)                      h[i][j] = 100;                  else if(h[i][j]<-100)                      h[i][j]=-100;              }          }                                       for(row=0;row<(m-k+1);row++){              for(col=0;col<(n-k+1);col++){                  for(i=0;i<k;i++)                      for(j=0;j<k;j++)                          t[i][j] = A[i+row][j+col]*h[i][j];                  for(i=0;i<k;i++)                      for(j=0;j<k;j++)                          B[row][col] += t[i][j];              }          }          for(row=0;row<(m-k+1);row++)              for(col=0;col<(n-k+1);col++)                  printf("%d ",B[row][col]);          printf("\n");      }while(0);            return 0;  }   输出：

11.初级算法

[cpp]  view plain  copy #include <stdio.h>    #define MAX_TYPE 100     #define PRINT  //Just review shellsort, not necessary.   void shellsort(int *arr,int size)  {      int h=1,i=0;      while(h<size)      {          h=3*h+1;          }      h=(h-1)/3;      while(h>0)      {          for(i=0;(i+h)<size;i++)          {              if(arr[i+h]>arr[i])              {                  int temp = arr[i+h];                  arr[i+h] = arr[i];                  arr[i] = temp;              }             }          h=(h-1)/3;      }  }    void makeChange(int *moneytype,int typenum,int money,int *coin)  {      int i,k,min;      #ifdef PRINT      int j,n,flag;      int arr[money+1][money+1];      arr[0][0] = 0;      flag = 0;      #endif      coin[0] = 0;      for(i=1;i<money+1;i++)      {          min = i;          for(k=0;k<typenum;k++)          {              if(moneytype[k] <= i)              {                  int temp = coin[i-moneytype[k]]+1;                  if(temp < min)                  {                      min = temp;                       #ifdef PRINT                      flag = 1;                      j = k;                      #endif                  }                 }             }          coin[i] = min;          #ifdef PRINT          if(flag == 1)          {              for(n=0;arr[i-moneytype[j]][n]!=0;n++)                  arr[i][n] = arr[i-moneytype[j]][n];              arr[i][n] = moneytype[j];              arr[i][n+1] = 0;              flag = 0;             }          if(coin[i] == i)          {              for(n=0;n<i;n++)                  arr[i][n] = 1;              arr[i][i] = 0;          }          #endif      }      printf("如果我们要找%d的钱,我们最少需要使用%d个币\n",money,coin[money]);       #ifdef PRINT      printf("需要零的钱为：");      for(n=0;n<coin[money];n++)          printf("%d ",arr[money][n]);      printf("\n");      #endif  }    int main()  {      char end;      int changeType[MAX_TYPE],TotalMoney;      int i=0,j=0;      printf("有哪几种硬币(不超过100种)：");      do{          scanf("%d",&changeType[i++]);      }while((end=getchar())!='\n');             printf("Now Machine has ");      //shellsort(changeType,i);      for(j=0;j<i;j++)      {          printf("%d ",changeType[j]);          }       printf(",%d types of money\n",i);             while(1)      {          printf("输入总钱数，我会帮你找到最佳找零方法：");          scanf("%d",&TotalMoney);          do{              int MinCoins[TotalMoney+1];              makeChange(changeType,i,TotalMoney,MinCoins);          }while(0);      }      return 0;  }

12.链表操作