One-dimensional arrays and two-dimensional arrays

Level 1: Sorting Problems

mission details

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

Relevant knowledge (omitted)

programming requirements

According to the prompt, add the code at Begin-End of the editor on the right. Enter Enter ten integers.

Output Output the ten numbers in ascending order.

Test instruction

Sample input: 1 2 3 4 5 6 7 8 9 10

Sample output: 10 9 8 7 6 5 4 3 2 1

Start your quest and good luck!

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int a[10],i,j,t,m=10;   
	for(i=0;i<m;i++)
    scanf("%d",&a[i]);
    for(i=0;i<9;i++){
        for(j=0;j<9;j++){
            if(a[j]<a[j+1]){
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;                
            }  
        }
    } //The outer layer a[i] performs assignment calculation, and the inner layer loop performs size comparison
	for(i=0;i<10;i++)
    printf("%d ",a[i]);
    /*********End**********/
    return 0;
}

Level 2: Find Integers

mission details

Question Description: Given a sequence of n integers, ask the first occurrence of the integer a in the sequence.

Relevant knowledge (omitted)

programming requirements

According to the prompt, add the code at Begin-End of the editor on the right. The first line of input contains an integer n. The second line contains n non-negative integers for the given sequence, each number in the sequence is not greater than 10000. The third line contains an integer a, the number to be searched. Output If a appears in the sequence, output the position of its first occurrence (positions are numbered from 1), otherwise output -1.

Test instruction

Sample input: 6 1 9 4 8 3 9 9 Sample output: 2 Hint: Data size and convention. 1 <= n <= 1000

Start your mission and good luck

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int n,c=1,b[1000],i,a;
    scanf("%d\n",&n);
    for(i=1;i<=n;i++)
    scanf("%d\n",&b[i]);
    scanf("%d\n",&a);
    for(i=1;i<=n;i++){
        if(b[i]==a){
            printf("%d",i);
            c=0;
            goto out;
        }
    }
out:
    if(c==1)
    printf("-1");
    /*********End**********/
    return 0;
}

Level 3: Calculate the maximum value of the elements in the array and the subscript value of the row and column where it is located

100

  • Mission requirements
  • Comments 1359

mission details

Topic 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 value of the row and column where it is located. where the values ​​of m and n are input by the user's keyboard. Both m and n are known to have values ​​that do not exceed 10.

Relevant knowledge (omitted)

enter

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

output

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

sample input

5,5 1 2 3 4 5 4 5 6 100 2 3 2 1 5 6 1 2 3 5 4 3 5 6 4 8

Sample output

Input m, n:Input 5*5 array: max=100, row=2, col=4

Start your quest and good luck!

programming requirements

According to the prompt, add the code at Begin-End of the editor on the right.

Test instruction

The platform will run a test on your code, if the actual output is the same as the expected output, it is considered a clearance

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int m,n,i,j,max,row,col,a[10][10];
    row=col=1;
    printf("Input m, n:");
    scanf("%d,%d",&m,&n);
    printf("Input %d*%d array:\n",m,n);
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    max=a[0][0];//to initialize
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            if(a[i][j]>max){
                max=a[i][j];
                row=i+1;
                col=j+1;//Arrays start at 0, in reality the subscript starts at 1
            }
        }
    }
    printf("max=%d, row=%d, col=%d",max,row,col);
    /*********End**********/
    return 0;
}

Level 4: Binary Search

100

  • Mission requirements
  • Comments 1359

mission details

Topic description: Number n integers (n<1000000) sorted from small to large from 1 to n, and an integer m to be searched, please use the dichotomy method to search. ####Related knowledge (omitted) ####Programming requirements According to the prompts, add the code at the Begin-End editor on the right. Input The input consists of 3 lines, the first line contains the integer n, the second line contains n integers separated by spaces, and the third line contains the 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.

Test instruction

Sample Input: 10 1 2 4 5 6 7 8 9 10 11 10 Sample Output: 9

Start your quest and good luck!

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
 	int a[1000000],i,n,m,b,c=0;
  	scanf("%d",&n);
  	b=n/2;//binary search
  	for(i=1;i<=n;i++)
  	scanf("%d",&a[i]);
 	scanf("%d",&m);
 	if(m==a[b]){
     	printf("%d",b);
        return 0;
    }
 	if(m<a[b]){
  		for(i=1;i<b;i++){
   			if(m==a[i]){
				c=1;
				printf("%d",i);
                break;
			}			
  		}
	}
 	if(m>a[b]){
  		for(i=b;i<=n;i++){
   			if(m==a[i]){
				c=1;
				printf("%d",i);
                break;
			}
  		}
 	}
 	if(c==0) 
 	printf("None");
    /*********End**********/
    return 0;
}

Level 5: Saddle Point

100

  • Mission requirements
  • Comments 1359

mission details

Topic 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 this row and the smallest in this column, where 1<=m, n<=10.

Relevant knowledge (omitted)

programming requirements

enter

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

output

Output saddle points 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 specify that the array subscripts start from 0. A two-dimensional array does not necessarily have a saddle point. In this case, please output None. We guarantee that there will not be two saddle points, such as: 3 3 1 2 3 1 2 3 3 6 8

Test instruction

The platform will run a test on your code, and if the actual output is the same as the expected output, it will be considered a clearance.

Sample input:

3 3 1 2 3 4 5 6 7 8 9

Sample output:

Array[0][2]=3

Start your quest and good luck!

#include <stdio.h>
int main()
{
    int m,n,i,j,k,b,c,flag,q=0;
    scanf("%d %d",&m,&n);
    int a[m][n];
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<m;i++){//Compare first
        flag=1,b=i,c=0;
        for(j=0;j<n;j++){
            if(a[b][c]<a[i][j]){
                c=j;
            }
        }
        for(k=0;k<m;k++){//Compare columns again
            if(a[b][c]>a[k][c]){
                flag=0;
                break;
            }
        }
        if(flag){
            printf("Array[%d][%d]=%d\n",b,c,a[b][c]);
            q=1;
        }
    }
    if(!q)
        printf("None\n");
    return 0;
}

Level 6: Remove the maximum value

100

  • Mission requirements
  • Comments 1359

mission details

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

Relevant knowledge (omitted)

programming requirements

Please read the code on the right carefully, combine relevant knowledge, and supplement the code in the Begin-End area to complete the writing of the applet to delete the maximum value.

enter

Enter 10 different integers

output

Output the array with the largest element removed

Test instruction

The platform will run a test on your code, and if the actual output is the same as the expected output, it will be considered a clearance.

Sample input:

1 2 3 4 5 6 7 8 9 0

Sample output:

1 2 3 4 5 6 7 8 0

Start your quest and good luck!

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

Level 7: Yang Hui's Triangle

100

  • Mission requirements
  • Comments 1359

mission details

Topic description: Do you remember the Yang Hui triangle you learned in middle school? The specific definition is not described here, you can refer to the following graphics: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

Relevant knowledge (omitted)

programming requirements

Please read the code on the right carefully, combine relevant knowledge, and supplement the code in the Begin-End area to complete the small program of Yanghui Triangle.

Test instruction

The platform will run a test on your code, and if the actual output is the same as the expected output, it will be considered a clearance.

output

Print out 10 lines of the Yanghui triangle. See the title description section for the format. Each integer is followed by a space to separate the integers

Start your quest and good luck!

//Realization of Yanghui's Triangle with Two-dimensional Array
#include<stdio.h>
int main(void)
{
    /*********Begin*********/
    int i,j;
    int arr[10][10];
    for (i=0;i<10;i++){
        arr[i][i]=1;
        arr[i][0]=1;
    }
    for (i=2;i<10;i++){
        for (j=1;j<=i-1;j++){
            arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
        }
    }
    for (i=0;i<10;i++){
        for (j=0;j<=i;j++){           
            printf(" %d",arr[i][j]);
        }
        printf("\n");
    }
    /*********End**********/
    return 0;
}

Tags: C++

Posted by mickey9801 on Sat, 15 Oct 2022 06:52:25 +0530