• 【Educoder作业】C&C++数组实训


    【Educoder作业】C&C++数组实训

    数组是很好用的。作为一个最基本的数据结构,数组是构成高级结构的基础。简单点的比如列表的next和head指针,桶的下标,栈;复杂点的比如说线段树的节点,KD树的平面,我们都需要数组进行实现。

    T1 销售波动统计

    这个题目显然是容易的,注意不要从 i = = 0 i==0 i==0开始即可。

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    T2 最大销售增幅

    这种往函数里面传数组的操作在程序设计竞赛里面是极其愚蠢的,因为常数非常大导致低效;但是在程序实现中带来的方便是不可忽略的,写起来很简单。

    #include 
    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 **********/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    T3 猴子选大王

    这个题是有一点意思的。
    首先,我们的思路先考虑每一次淘汰:只需要从当前的开始往后找到第一个满足条件的猴子即可。这个操作可以用 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++)实现因为最后要淘汰 n − 1 n-1 n1个猴子,也可以用 w h i l e while while来实现,只需要判断当前的猴子数目即可。
    最后就用 w h i l e while while或者 f o r + b r e a k for+break for+break找一下剩下的猴子即可,

    #include 
    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 **********/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    T4 犯二的程度

    有那么一点高精度的味道了。处理当然是容易的,扫一下数组加几个 i f if if就结束了。

    #include 
    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 **********/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    T5 队列变换

    有点奇怪,我做的时候感觉题目描述有点问题?自己对着样例在纸上画一画,然后注意坐标的转换就可以了。

    #include 
    using namespace std;
    
    // 函数rotateLeft:矩阵循环左移
    // 参数:a-100*100的二维数组,用来存储n行n列的数组(n<100),存储在其~n-1行和~n-1列,
    // m-循环左移的位数(0
    // 返回值:无,循环左移的结果写入原二维数组中
    // 说明:传递多维数组时,形式参数中,除了第一维外,其它维的大小必须给出
    // 方便编译器计算每个数组元素的地址
    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 **********/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    T6 朋友圈点赞

    这个题给我们了一个思路,就是记录最大值的同时记录哪个位置是最大值。

    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    软件测试———linux
    华为乾坤区县教育安全云服务解决方案(1)
    Modbus协议
    【React】React全家桶(十)Redux
    【C刷题】day6
    jenkins编译使用nohup部署进程到后台失败,解决方法
    C语言--每日五道选择题--Day4
    5大LOGO免费在线生成器,从此设计不求人!
    Maven学习笔记(一)
    从云计算到函数计算
  • 原文地址:https://blog.csdn.net/JZYshuraK/article/details/127996082