[Educoder homework] C&C++ array training

[Educoder homework] C&C++ array training

Arrays are very useful. As one of the most basic data structures, arrays are the basis for advanced structures. Simple ones such as the next and head pointers of the list, bucket subscripts, and stacks; complex ones such as the nodes of the line segment tree and the plane of the KD tree, we all need arrays to implement them.

T1 sales fluctuation statistics

This question is obviously easy, be careful not to start from i = = 0 i==0 i==0Can start immediately.

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    
    
    int n, a[30], i;     // 定义变量及数组,n-销售额个数,a-销售额
    cin >> n;     // 输入销售额数量,n <= 30
    // 输入n个销售额,分别存入a[0]到a[n-1]
    for(i = 0; i < n; i++)
        cin >> a[i];
    // 请在此添加代码,计算并输出销售额的波动情况
    /********** Begin *********/
	for (i = 1; i < n; i ++ ) printf("%d ", a[i] - a[i - 1]);
	puts("");
    
    
    /********** End **********/
    return 0;
}

T2 maximum sales increase

This kind of operation of passing an array into a function is extremely stupid in programming competitions, because the constants are very large and lead to inefficiency; however, the convenience it brings in program implementation cannot be ignored, and it is very simple to write.

#include <iostream>
using namespace std;

// 函数maxIncrease:计算销售额增幅
// 参数:s-销售额数组,n-销售额数组长度,n>1
// 返回值:销售额最大增幅
int maxIncrease(int s[], int n);

int main()
{
    
    
    int n, a[30], i;     // 定义变量及数组,n-销售额个数,a-销售额数组
    cin >> n;      // 输入销售额数量,n>1
    // 输入n个销售额,分别存入a[0]到a[n-1]
    for(i = 0; i < n; i++)
        cin >> a[i];
    i = maxIncrease(a,n);
    cout << "最大销售增幅为:" << i << endl;
    return 0;
}

int maxIncrease(int s[], int n)
{
    
    
    //请在此添加代码,实现函数maxIncrease
    /********** Begin *********/
	int ans = 0xefefefef;
	for (int i = 0; i < n; i ++ ) {
    
    
		for (int j = i; j < n; j ++ ) {
    
    
			ans = max(ans, s[j] - s[i]);
		}
	}
	return ans;
    /********** End **********/
}

T3 Monkey chooses the king

This question is a bit interesting.
First of all, our idea is to consider each elimination: we only need to find the first monkey that meets the conditions from the current beginning. This operation can be used f o r ( i n t i = 1 ; i < n ; i + + ) for(int\ i = 1; i < n; i ++) for(int i=1;i<n;i++)actually present due to last selection n − 1 n-1 n1个猴子,也可用用 w h i l e while while When it comes to reality, it's just a matter of determining the demand and the number of 猌子 will be available immediately.
Last employed w h i l e while while或者 f o r + b r e a k for+break for+break Immediately available for the first time,

#include <iostream>
using namespace std;

// 函数king:猴子选大王
// 参数:a-猴子数组n-1个猴子分别占据下标为~n-1的位置,n-数组长度
// 返回值:新猴王的下标序号
int king(int a[], int n);

int main()
{
    
    
    int n, a[1000], i;     // 定义变量及数组,n-猴子数量,a-猴子数组
    cin >> n;     // 输入猴子数量,n>0

    // 初始化猴子数组,n 个猴子分别占据 n 个位置
    a[0] = 0; // 0号位置没有猴子
    for(i = 1;i <= n; i++)
        a[i] = i;

    // 选大王啦
    i = king(a, n);
    cout << i << "号猴子是大王。" << endl;
    return 0;
}

int king(int a[], int n)
{
    
    
    // 请在此添加代码,实现函数king
    /********** Begin *********/
    int num = n, now = 1, tmp = 1;
    while (num > 1) {
    
    
        while (!a[now]) {
    
    
            if (now == n) now = 1;
            else now ++ ;
        }
        if (tmp == 3) a[now] = 0, num -- , tmp = 1;
        else tmp ++ ;
        now ++ ;
        if (now == n + 1) now = 1;
    }
    while (!a[now]) {
    
    
		if (now == n) now = 1;
		else now ++ ;
	}
    return now;
    /********** End **********/
}

T4 Degree of second offense

It has a bit of high-precision flavor. Of course the processing is easy, just scan the array and add a few i f if ifConcluded.

#include <iostream>
using namespace std;

// 函数silly:计算数值有多二
// 参数:a-存储数值的字符数组,以'\0'结束,所以不需要另一个参数告诉函数数组有多长
// 返回值:数值犯二的程度
double silly(char a[]);

int main()
{
    
    
    char s[102];     // 定义存储数值的数组
    cin >> s;     // 输入不超过位的整数
    double sy = silly(s);     // 计算犯二的程度
    cout << sy << endl;     // 输出犯二的程度
    return 0;
}

double silly(char a[])
{
    
    
    // 请在此添加代码,实现函数silly
    /********** Begin *********/
	bool flag = false;
	if (a[0] == '-') flag = true;
	int i = 0, num = 0;
	while (a[i] != '\0') {
    
    
		if (a[i] == '2') num ++ ;
		i ++ ;
	}
	int tmp = 1;
	if ((a[i - 1] - '0') % 2 == 0) tmp = 2;
	if (flag) return (double)num / (i - 1) * 1.5 * tmp;
	return (double)num / i * tmp;
    
    /********** End **********/
}

T5 queue transformation

It's a bit strange. When I was doing it, I felt that there was something wrong with the title description? Just draw a picture on paper against the sample, and then pay attention to the coordinate conversion.

#include <iostream>
using namespace std;

// 函数rotateLeft:矩阵循环左移
// 参数:a-100*100的二维数组,用来存储n行n列的数组(n<100),存储在其~n-1行和~n-1列,
// m-循环左移的位数(0<m<n)
// 返回值:无,循环左移的结果写入原二维数组中
// 说明:传递多维数组时,形式参数中,除了第一维外,其它维的大小必须给出
// 方便编译器计算每个数组元素的地址
void rotateLeft(int a[][100],int n,int m);

int main()
{
    
    
    int a[100][100];     // 定义存储二维数组的空间
    int n, m;
    cin >> n >> m;     // 输入n和m

    // 输入n*n的矩阵,存储在数组a的~n-1行和~n-1列
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0; j < n; j++)
            cin >> a[i][j];

    // 循环左移
    // 说明:传递多维数组时,实在参数只需要给出数组名就可以了
    rotateLeft(a,n,m);

    // 输出循环右移的结果
    for(i = 0; i < n; i++)
    {
    
    
        for(j = 0; j < n; j++)
            cout << " " << a[i][j];
        cout << endl;
    }
    return 0;
}
int b[110][110];
void rotateLeft(int a[][100],int n,int m)
{
    
    
    // 请在此添加代码,实现函数rotateLeft
    /********** Begin *********/
    for (int i = 0; i < n; i ++ ) for (int j = 0; j < n; j ++ ) b[i][j] = a[i]
[j];
	for (int i = 0; i < n; i ++ ) {
    
    
		for (int j = 1; j <= m; j ++ ) a[i][n - m + j - 1] = b[i][j - 1];
		for (int j = 1; j <= n - m; j ++ ) a[i][j - 1] = b[i][m + j - 1];
	}
    /********** End **********/
}

T6 Moments likes

This question gives us an idea, which is to record the maximum value and record which position is the maximum value.

#include <bits/stdc++.h>

using namespace std;

int v[1010];

int main() {
    
    
    int n; cin >> n ;
    for (int i = 1; i <= n; i ++ ) {
    
    
        int k; scanf("%d", &k);
        while (k -- ) {
    
    
            int x; scanf("%d", &x); v[x] ++ ;
        }
    }
    int id = 1000, mx = v[1000];
    for (int i = 1000; i; i -- ) {
    
    
        if (v[i] > mx) mx = v[i], id = i;
    }
    cout << id << ' ' << mx << endl ;
    return 0;
}

Guess you like

Origin blog.csdn.net/JZYshuraK/article/details/127996082