Practical notes of algorithm notes - 2.5 array (exercise 6-4 orderly insertion, question C: exercise 6-6 Yang Hui triangle, question D: exercise 6-12 decryption)

preface        ...
preface  
My notes

preface  

         In order to prepare for the written examination of senior Huawei, we decided to use the book "algorithm notes" recommended by our seniors, which is supported by the book "algorithm notes · practical guide for computer training". We hope to pass the written examination after learning this book and lay a foundation for getting an offer from a large factory 💪

         This article is the fourth in the actual combat series.



My notes

      1. Question A: exercises 6-4 are inserted in order

        There is a problem with the input assignment of the array. After searching, we know that we can't input a large string.

#include <stdio.h> #include <string.h> int main() { int a[10]; int c, i; for(i = 0; i<9; i++) { scanf("%d", &a[i]); } // scanf("%d %d %d %d %d %d %d %d %d\n", &a[10]);// The array input method here is incorrect 🤣 scanf("%d", &c); a[9] = c; //Note that the subscript of the array with length of 10 is up to 9!! for(i = 0; i < 10; i++) { if(c<a[i]) { printf("%d\n", c); for(i; i < 9; i++) { printf("%d\n", a[i]); } break; //When typing the code, I wonder whether the loop can be ended here. Note that break ends the loop, and if is only conditional judgment } else { printf("%d\n", a[i]); } } return 0; }

        2. Yang Hui triangle

         At the beginning, I didn't expect to use two-dimensional array and one-dimensional array for a long time, but I didn't succeed. When I always calculated a[3], I couldn't get the data of the previous line a[2], and then I defined an array b[10], which was not done well; Another intermediate variable t is defined. Unfortunately, patience is exhausted... If the debugging is not successful, give up and search for the answer; Get tips on the Internet and use two-dimensional arrays. It's really easy to do;

#include <stdio.h> int main() { int i, j, n; int a[10][10] = {}; scanf("%d", &n); a[0][0] = 1; //You don't need this if you have initialization for(i = 1; i < n; i++) { for(j = 0; j <= i; j++) { a[i][0] = 1; //The first number in each line is 1 a[i][i] = 1; //The last number in each line is 1 if(i == j) continue; //If the last number in each line has been assigned, it will not participate in the following operations a[i][j] = a[i-1][j-1] + a[i-1][j]; //The lower number is equal to the sum of the upper two numbers } } for(i = 0; i < n; i++) { for(j = 0; j <= i; j++) { printf("%d ", a[i][j]); } printf("\n"); } return 0;

         Then, you can also see the code using one-dimensional array, including redefining one-dimensional array and redefining two intermediate variables; It can be seen that there is no problem with the idea, and the mentality collapsed, resulting in the failure to write runnable code in the end.

         Intermediate variable: I haven't fully understood the code. I don't know the logic of the latter two variables;; The operation of initializing a[10] to (0, 1) is because the following j starts from 1, and the output also starts from a[1] = 1. This step can make the first number of each line 1.

#include <stdio.h> int main() { int i, j, n, k, t; int a[10] = ; //Used to save a row of data // scanf("%d",&n); n = 4; for( i = 1; i <= n; i++) { t = 0; for( j = 1; j <= i; j++) { k = a[j]; a[j] = t + k; //Each number is the sum of the above two numbers t = k; printf("%d ", a[j]); } printf("\n"); } return 0; }

          Two one-dimensional arrays:

#include <stdio.h> int main(){ int i,j,n=0,a[10]=,b[10]; scanf("%d",&n); for(i=0;i<n;i++) { b[0]=a[0]; for(j=1;j<=i;j++) b[j]=a[j-1]+a[j]; //Each number is the sum of the above two numbers for(j=0;j<=i;j++){ //Output Yang Hui triangle a[j]=b[j]; //The calculated new line is assigned to a for printing and the next calculation printf("%d ",a[j]); } printf("\n"); } return 0; }

         3. Question D: exercise 6-12

        I choose two output methods for this topic, but one can pass and the other can't. the answer is wrong. I can run the display myself. I don't understand.

#include <stdio.h> int main(){ char a[15]; gets(a); for(int i = 0; i < 15; i++) { if(a[i] >= 65&&a[i] <= 90) a[i] = 155 - a[i]; if(a[i] >= 97&&a[i] <= 122) a[i] = 219 - a[i]; //printf("%c", a[i]); // This kind of output can't pass } printf("%s", a); return 0 ; }

12 September 2021, 14:54 | Views: 5191

Add new comment

For adding a comment, please log in
or create account

0 comments