Extra chapter: printing graphics, matrix transposition, reverse order output, and ordered sequence merging.

Foreword:
1 The main content of the first question is to review the knowledge of cycles, and the second is to hope that everyone can master the rules of printing graphics.

2 The next three questions are mainly related to binary arrays, and there are certain methods and techniques. You should understand them after reading the detailed explanations slowly.

Please read the question below:

//根据规律做题,不知道大家发现没有,先看正对角线,发现行列相等,副对角线行加列等于4
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//打印图形X
int main()
{
	int i = 0;//行
	int j = 0;//列
	int n = 0;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (i == j || (i + j) == n - 1)
			{
				printf("*");
			}
			else
				printf(" ");
		}
		printf("\n");
	}
	return 0;
}

Take a look at the running results:

Look at the code below:

//还是找规律
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//打印图形X
int main()
{
	int i = 0;//行
	int j = 0;//列
	int n = 0;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (i==0||j==0||j==n-1||i==n-1)//这个是规律的核心
			{
				printf("* ");
			}
			else
				printf("  ");
		}
		printf("\n");
	}
	return 0;
}

2 Matrix transpose

int main()
{
	int n = 0;//行
	int m = 0;//列
	scanf("%d%d", &n, &m);
	int i = 0;
	int j = 0;
	int arr[10][10] = { 0 };
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		scanf("%d", &arr[i][j]);
	}
	for (i = 0; i< m; i++)
	{
		for (j = 0; j < n; j++)
		{
			printf("%d ",arr[j][i]);
		}
		printf("\n");
	}

	return 0;
}

3 Reverse order output

int main()
{
	int arr[10] = { 0 };
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (i = 9; i >= 0; i--)
	printf("%d ", arr[i]);
	return 0;
}

3 Exchange arrays

#include <stdio.h>
int main()
{
	int arr1[10] = {0};
	int arr2[10] = {0};
	int i = 0;


	printf("请输入10个数字:>");
	for(i=0; i<10; i++)
	{
		scanf("%d", &arr1[i]);
	}
	printf("请输入10个数字:>");
	for(i=0; i<10; i++)
	{
		scanf("%d", &arr2[i]);
	}
	//交换
	for(i=0; i<10; i++)
	{
		int tmp = arr1[i];
		arr1[i] = arr2[i];
		arr2[i] = tmp;
	}
	
	return 0;
}

4 Ordered sequence merging

Method 1: Bubble sorting (it is not recommended to use it here, although it can be done, but it is a bit like applying a formula, a bit old-fashioned, personal feeling)

#include <stdio.h>
int main()
{
    int n = 0;
    int m = 0;
    scanf("%d%d", &n, &m);
    int i = 0;
    int j = 0;
    int arr[10000] = { 0 };
    for (; i < n+m; i++)
    {
        scanf("%d", &j);
        arr[i] = j;
    }
    for (i = 0; i < n + m; i++)
    {
        for (j = 0; j < n + m -1 - i; j++)
        {
            if (arr[j] > arr[j+ 1])
            {
                int tmp = arr[j+1];
                arr[j+1] = arr[j];
                arr[j] = tmp;
            }
        }
    }
    for (i = 0; i < n + m; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

Method 2 (harder to think of, but a good method, few people should be able to think of it):

#include <stdio.h>


int main()
{
    int n = 0;
    int m = 0;
    int arr1[1000] = {0};
    int arr2[1000] = {0};
    //输入
    scanf("%d %d", &n, &m);
    int i = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr1[i]);
    }
    for(i=0; i<m; i++)
    {
        scanf("%d", &arr2[i]);
    }
    //处理
    int j = 0;
    i = 0;
    while(i<n && j<m)
    {
        if(arr1[i] < arr2[j])
        {
            printf("%d ", arr1[i]);
            i++;
        }
        else
        {
            printf("%d ", arr2[j]);    
            j++;
        }
    }
    if(i == n)
    {
        for(; j<m; j++)
        {
            printf("%d ", arr2[j]);
        }
    }
    else
    {
        for(; i<n; i++)
        {
            printf("%d ", arr1[i]);
        }
    }
    return 0;
}

Today’s content has been explained, leaving a more interesting question. You can take a look. The answer is attached at the end (the knowledge points used are all learned previously, excluding the following knowledge points. At this point, I believe everyone You must be able to understand the answer. There is still a lot of room for optimization in the code of this answer. You can try it yourself first, then look at the ideas of the answer, and finally optimize the answer code and your own code)

#include<stdio.h>
int main()
{
	int l = 0;
	int r = 0;
	scanf("%d%d", &l, &r);
	int i = 0;
	int x = 1;
	int y = 0;
	int m = 0;
	int count0 = 0;
	int count1 = 0;
	int count2 = 0;
	int count3 = 0;
	int count4 = 0;
	int count5 = 0;
	int count6 = 0;
	int count7 = 0;
	int count8 = 0;
	int count9 = 0;
	int max = 0;
	int n = 0;
	for (i = l; i <= r; i++)
	{
		m = i;
		if (i == 0)
		{
			count0++;
		}
		if (m % 10 == 0&&m!=0)
		{
			count1++;
			count0++;
			while (m /= 10)
			{
				if (m != 1)
				count0++;
			}
		}
		m = i;
		while (m!= 0&&i!=0&&m%10!=0)
		{
			x = m % 10;
			m /= 10;
			switch (x)
			{
			case 0:
				count0++;
				break;
			case 1:
				count1++;
				break;
			case 2:
				count2++;
				break;
			case 3:
				count3++;
				break;
			case 4:
				count4++;
				break;
			case 5:
				count5++;
				break;
			case 6:
				count6++;
				break;
			case 7:
				count7++;
				break;
			case 8:
				count8++;
				break;
			case 9:
				count9++;
				break;
			}
		}
	}
	int arr[10] = { count0, count1, count2, count3, count4, count5, count6, count7, count8, count9 };
	max = arr[0];
	for (y = 0; y <= 9; y++)
	{
		if (max < arr[y])
			max = arr[y];
	}
		if (max == count0)
			n = 0;
		if (max == count1)
			n = 1;
		if (max == count2)
			n = 2;
		if (max == count3)
			n = 3;
		if (max == count4)
			n = 4;
		if (max == count5)
			n = 5;
		if (max == count6)
			n = 6;
		if (max == count7)
			n = 7;
		if (max == count8)
			n = 8;
		if (max == count9)
			n = 9;
		printf("%d %d", n, max);
	    return 0;
}

Guess you like

Origin blog.csdn.net/2301_79811170/article/details/134168877