Touge practice teaching platform C language one-dimensional array and two-digit array

Level 7: Yang Hui Triangle

The code of the Yang Hui triangle refers to this blogger's article:

(41 messages) Detailed Explanation of c Language Yanghui Triangle Code (Super detailed, don’t you really come in and take a look?)

Title description: Do you still remember the Yang Hui triangle that you learned in middle school? The specific definition will not be described here, you can refer to the following graphics:

output

Print out 10 lines of the Yang Hui triangle figure. See the topic description section for the format. Each integer is followed by a space to separate the integers

#include <stdio.h>
int main(void)
{
    int data[10][10];//define a two-dimensional array
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            data[i][j]=1;//The two-dimensional array is all initialized to 1
        }
    }
    //Process the data in the middle: starting from the 3rd row and the 2nd column, the current number = the number in the previous row + the number in front of the previous row
    for(int k=1;k<10;k++){
        for(int p=1;p<k;p++){
            data[k][p]=data[k-1][p]+data[k-1][p-1];
        }
    }
    //Output Data
    for(int i=0;i<10;i++){
        for(int j=0;j<=i;j++){
            printf("%d ",data[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Level 6: Delete the maximum value

Title description: Input 10 different integers and save them in an array, find the largest element and delete it, and output the deleted array

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int a[10];

    for(int i=0;i<10;i++){
        scanf("%d",&a[i]);
    }
    int max=a[0],num=0;
    for(int i=0;i<10;i++){//find the maximum value max, num
        if(a[i]>max){
            max=a[i];
            num=i;
        }
     }
     for(int i=num;i<9;i++){//remove the largest value a[num]
        a[i]=a[i+1];
     }
    //output array
    for(int i=0;i<9;i++){
        printf("%d ",a[i]);
    }
    /*********End**********/
    return 0;
}

Level 5: Saddle Point

Title description: Find the "saddle point" of a two-dimensional array Array with m rows and n columns, that is, the element at this position is the largest on the row and the smallest on the column, where 1<=m, n<=10.

enter

The input data has multiple lines, the first line has two numbers m and n, and there are m lines below, each line has n numbers.

output

Output the saddle point in the following format: Array[i][j]=x where x represents the saddle point, i and j are the array row and column subscripts where the saddle point is located, and we stipulate that the array subscript starts from 0. A two-dimensional array does not necessarily have a saddle point, please output None at this time and we guarantee that there will not be two saddle points, for example:

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int m,n;
    scanf("%d %d",&m,&n);
    int a[m][n];
    //enter
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    int MAX=a[0][0],row=0,col=0;
    int MIN;
    //Determine whether it is a saddle point
    //Determine whether the largest number in the same row is the smallest number in the same column
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
                //find the largest number in a row

                if(a[i][j]>MAX){
                    MAX= a[i][j];
                    //printf("%d***\n",MAX);
                    row=i;
                    col=j;
                }
        }

        //Determine whether the number is the smallest number in the same column
        int flag=0;
        for(int i=0;i<m;i++){
            if(a[i][col]<a[row][col]){
                flag++;//If the flag variable becomes 1, it means that the number is not the smallest number in the same column
                MIN=a[i][col];
                row=i;

            }
             flag=0;

        }
        if(flag==0){
            printf("Array[%d][%d]=%d",row,col,a[row][col]);
            break;
        }

    }

    return 0;
}

Level 4: Binary Search

Title description: Number n integers sorted from small to large (n<1000000) from 1 to n, and an integer m to be searched, please use the dichotomy method to search.

Input The input consists of 3 lines, the first line contains integer n, the second line contains n integers separated by spaces, and the third line contains integer m.

Output If the integer m can be found in the sequence, output the number (if there are multiple numbers, return the smallest number), if not, output None.

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int m,n,t;

    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    //to sort
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]>a[j]){
                t=a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    scanf("%d",&m);

    int low=0;
    int high=n-1;
    while(1){
        int mid=(low+high)/2;
        if(a[mid]==m){
            printf("%d",mid+1);
            break;
        }else if(a[mid]<m){
            low=mid+1;
        }else if(a[mid]>m){
            high=mid-1;
        }
         if(low>high){
            printf("None");
            break;
        }

    }
    return 0;
}

Level 3: Calculate the maximum value of the elements in the array and the subscript values ​​of the rows and columns where they are located

Title description: According to the following function prototype programming, input a two-dimensional array with m rows and n columns from the keyboard, and then calculate the maximum value of the elements in the array and the subscript values ​​of the rows and columns where they are located. Among them, the values ​​of m and n are input by the user's keyboard. Neither m nor n is known to exceed 10.

enter

Enter the size of the array: "%d,%d" Enter the elements in the array below.

output

Output format: Array size input prompt information: "Input m, n:" Array element input prompt information: "Input %d*%d array: " Output format: "max=%d, row=%d, col=%d "

 

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int m,n,col,row;
    printf("Input m, n:");
    scanf("%d,%d",&m,&n);
    int a[m][n];
    printf("Input %d*%d array:",m,n);
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            
            scanf("%d",&a[i][j]);
        }
    }
    //output
    int max=a[0][0];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(a[i][j]>=max){
                max= a[i][j];
                row=i+1;
                col=j+1;
            }
        }
    }
    printf("\nmax=%d, row=%d, col=%d",max,row,col);
    return 0;
}

Level 2: Finding Integers

Title description: Given a sequence containing n integers, ask what is the first occurrence of the integer a in the sequence.

enter

The first line contains an integer n. The second line contains n non-negative integers, which is a given sequence, and each number in the sequence is not greater than 10000. The third line contains an integer a, which is the number to be searched.

output

If a appears in the sequence, output its first occurrence position (the position is numbered from 1), otherwise output -1.

Sample input:

6

1 9 4 8 3 9

9

Sample output:

2

Tip: Data size and conventions. 1 <= n <= 1000

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int a[10],n,b,c=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    scanf("%d",&b);
    for(int i=0;i<n;i++){
        if(a[i]==b){
            c++;
            printf("%d",i+1);
            break;
        }
    }
    if(c!=0){

    }else{
        printf("-1");
    }

    return 0;
}

Level 1: Sorting Problems

The task of this level: Arrange the ten numbers in descending order.

Input Enter ten integers.

Output Output the ten numbers in descending order.

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int a[10],t;
    for(int i=0;i<10;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<10;i++){
        for(int j=i+1;j<10;j++){
            if(a[i]<a[j]){
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    for(int m=0;m<10;m++){
        printf("%d ",a[m]);
    }
    return 0;
}

Tags: C programming language

Posted by senorfrog on Sat, 10 Dec 2022 20:17:29 +0530