Handwriting algorithm in written test

Time:2020-11-27

As a developer, it is particularly important to have a learning atmosphere and a communication circleMy IOS communication group: 761407670No matter you are Xiaobai or Danio, welcome to join us, share bat, Ali interview questions, interview experience, discuss technology, let’s communicate, learn and grow together!

The following is basically implemented in C language

1. Instead of intermediate variables, exchange the values of a and B in two ways?

//1. Intermediate variable
void swap(int a, int b) {
   int temp = a;
   a = b;
   b = temp;
}

//2. Addition
void swap(int a, int b) {
   a = a + b;
   b = a - b;
   a = a - b;
}

//3. Exclusive or (the same is 0, but the difference is 1. It can be understood as carry free addition)
void swap(int a, int b) {
   a = a ^ b;
   b = a ^ b;
   a = a ^ b;
}

2. Find the greatest common divisor?

/**1. Direct traversal method*/
int maxCommonDivisor(int a, int b) {
    int max = 0;
    for (int i = 1; i <=b; i++) {
        if (a % i == 0 && b % i == 0) {
            max = i;
        }
    }
    return max;
}
/**2*/
int maxCommonDivisor(int a, int b) {
    int r;
    while(a % b > 0) {
        r = a % b;
        a = b;
        b = r;
    }
    return b;
}

//Extension: least common multiple = (a * b) / maximum common divisor

3. Simulate stack operation?

/**
 *Stack is a data structure, features: first in, then out
 *Exercise: use global variables to simulate the operation of the stack
 */
#include 
#include 
#include 
//Protect global variables: after adding static before global variables, the global variables can only be used in this file
Static int data [1024]; // the stack can hold up to 1024 data
Static int count = 0; // how many numbers have been put (equivalent to the top of the stack)

//Data push
void push(int x){
    Assert (! Full()); // prevent array from crossing bounds
    data[count++] = x;
}
//Data out of stack pop
int pop(){
    assert(!empty());
    return data[--count];
}
//View the top element of the stack
int top(){
    assert(!empty());
    return data[count-1];
}

//Query stack full
bool full() {
    if(count >= 1024) {
        return 1;
    }
     return 0; 
}

//Query stack empty
bool empty() {
    if(count <= 0) {
        return 1;
    }
    return 0;
}

int main(){
    //Stack
    for (int i = 1; i <= 10; i++) {
        push(i);
    }
  
    //Out of the stack
    while(!empty()){
        Printf (% D, top()); // stack top element
        pop(); //Out of the stack
    }
    printf("n");
    
    return 0;
}

4. Sorting algorithm?

The three sorting algorithms of selection sort, bubble sort and insert sort can be summarized as follows:
Both divide the array into sorted and unsorted parts.

  1. Select sort to define the sorted part on the left, and then select the smallest element of the unsorted part to exchange with the first element of the unsorted part.
  2. Bubble sort defines the sorted part at the right end, and exchanges the largest element to the right during traversing the unsorted part.
  3. Insert sort defines the sorted part at the left end, and inserts the first element of the element of the unsorted part into the proper position of the sorted part.

Select sort

/** 
 *[sorting by selection]: the maximum value appears at the beginning
 *  
 *The first time: find the exchange position between the smallest (largest) number and the first number in N numbers
 *Find the second largest number in n-2
 *Repeat the operation... Switch positions with the third, fourth, number in turn
 *In the N-1 trip, the data can be sorted in ascending order (descending order).
 *
 */
void selectSort(int *arr, int length) {
    For (int i = 0; I < length - 1; I + +) {// number of trips
        For (int j = I + 1; J < length; j + +) {// comparison times
            if (arr[i] > arr[j]) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

Bubble sort

/** 
 *Bubble sort: compare adjacent elements in pairs, and the maximum value appears at the end of the comparison
 *The first time: compare the two adjacent numbers in turn, continuously exchange (before the decimal and after the large number) and advance one by one, and the maximum value finally appears in the position of the nth element
 *The second time: compare the two adjacent numbers in turn, continuously exchange (before decimal, after large number) and advance one by one, and the maximum value finally appears in the N-1 element position
 *   ……   ……
 *Pass n-1: compare the two adjacent numbers in turn, and constantly exchange (before the decimal and after the large number), advance one by one, and the maximum value finally appears at the position of the second element 
 */
void bublleSort(int *arr, int length) {
    For (int i = 0; I < length - 1; I + +) {// number of trips
        For (int j = 0; J < length - I - 1; j + +) {// comparison times
            if(arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        } 
    }
}

5. Binary search

/**
 *Half search: optimize search time (without traversing all data)
 *
 *The principle of binary search:
 *The array must be ordered
 *2 > min and Max must be known
 *3 > dynamically calculate the mid value, and take out the corresponding value of mid for comparison
 *4 > if the corresponding value of mid is greater than the value to be searched, then Max should be reduced to mid-1
 *5 > if the value of mid is less than the value to be searched, min should be increased to mid + 1
 *
 */ 

//Given an ordered array and a key, it is required to find the index position corresponding to the key from the array 
int findKey(int *arr, int length, int key) {
    int min = 0, max = length - 1, mid;
    while (min <= max) {
        Mid = (min + max) / 2; // calculate the intermediate value
        if (key > arr[mid]) {
            min = mid + 1;
        } else if (key < arr[mid]) {
            max = mid - 1;
        } else {
            return mid;
        }
    }
    return -1;
}

6.9 * 9 multiplication table?

main(){
 int x,y,z;
 for(x=1;x<=9;x++){
  for(y=1;y<=x;y++) {
   z=x*y;
   printf("%d*%d=%d  ",y,x,z);
  }
  printf("n");
 }
}

6. If a number is n, then in C language, the number of bits, tens, hundreds and thousands are calculated as follows: n / 1% 10, N / 10% 10, N / 100% 10, N / 1000% 10?

int main(){
    int n = 123456;
    int unitPlace = n / 1 % 10;
    int tenPlace = n / 10 % 10;
    int hundredPlace = n / 100 % 10;
    int thousandPlace = n / 1000 % 10;
    Printf ("bits: DN, tens of bits: DN, hundreds of bits: DN, thousands of bits: DN", unitplace, tenplace, hundredplace, thousandplace);
 
    getchar();
    return 0;
}

7. Swap the values of the first element and the last element of an int array?

int[] a={2,3,4,5,7};
int temp=a[0];
a[0]=a[a.length-1];
a[a.length-1]=temp;

8. Input an integer n from the keyboard, please enter 1,2 If n = 3, the output is

1 2 3

1 3 2

2 1 3

2 3 1

void perm(int* data, int n, int curr){
if (curr==n-1){
for (int i = 0; i < n; ++i)
printf("%d", data[i]);
printf("n");
}else{
for (int i = curr; i < n; ++i){
int t;
t = data[curr], data[curr] = data[i], data[i] = t;
perm(data, n, curr+1);
t = data[curr], data[curr] = data[i], data[i] = t;
     }
  }
}
int main(){
int array[128] = {0};
int n = 0, i = 0;

Scanf ('% d', & n); / * no data validity check*/
for (i = 0; i < n; ++i) 
array[i] = i+1;
perm(array, n, 0);
return 0;
}

9. Input a positive integer and output it in reverse order. For example, input 12345, output 54321?

void main(){
long n,m;
Printf ("enter a positive integer");
scanf("%d",&n);
while(n!=0){ 
m=n%10;
n=n/10;
printf("%d",m);
    }
}

10. Use the nesting of conditional operators to complete this question: students with academic achievement > = 90 points are represented by a, those between 60 and 89 points are represented by B, and those below 60 points are represented by C.

void main(){
int grade;
printf("Inter grade:");
scanf("%d",&grade);
if(grade>=90&&grade<100)
printf("Grade is:An");
else if(grade>=60&&grade<=89)
printf("Grade is:Bn");
else if(grade<60&&grade>=0)
printf("Grade is:Cn");
else printf("errorn");

11. Judging prime number

//How to judge whether M is a prime: let m be divided by I (I changed from 2 to k = sqrt (m))
//If M is divisible by some I (an integer between 2 and K), then M must not be a prime
void main(){
int m,i,k;
printf("please enter an integer number:");
scanf("%d",&m);
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0) break;
if(i>k)
printf("%d is a prime number.n",m);
else printf("%d is not a prime number.n",m);
}

12. Program to find 1 + (1 + 2) + +(1+2+… +n) , n is entered by the keyboard. The for statement is required

 
void main(){
int j,k,i,sum=0,n=1,m,a=0;
Printf ("how many inputs are accumulated?");
scanf("%d",&j);
for(i=1;i<=j;i++){
sum=sum+n;
n++;
for(m=1;m<=1;m++){
a=a+sum;
   }
 }
Printf ("the number is% DN", a);
}