洛谷训练新手村之“数组”题解

P1046 陶陶摘苹果

题目链接:https://www.luogu.com.cn/problem/P1046
题目大意:求有多少苹果树的高度\(\le\) 陶陶的高度+30。
解题思路:开数组存数据,然后循环遍历一遍。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int a[11], cnt;
int main() {
    for (int i = 0; i < 11; i ++) cin >> a[i];
    for (int i = 0; i < 10; i ++) if (a[i] <= a[10]+30) cnt ++;
    cout << cnt << endl;
    return 0;
}

P1047 校门外的树

题目链接:https://www.luogu.com.cn/problem/P1047
题目大意:给你若干区间,求有多少点没有被这些区间覆盖。
解题思路:开一个 cut 数组, cut[i] 表示第i个点有没有被覆盖,对于每个区间 [l,r] ,从 l 到 r 置每个 cut[i]true ,然后从 1 到 n 统计有多少 cut[i] == false 的点。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
bool cut[10001];
int L, M, l, r, cnt;
int main() {
    cin >> L >> M;
    while (M --) {
        cin >> l >> r;
        for (int i = l; i <= r; i ++) cut[i] = true;
    }
    for (int i = 0; i <= L; i ++) cnt += !cut[i];
    cout << cnt << endl;
    return 0;
}

P1427 小鱼的数字游戏

题目链接:https://www.luogu.com.cn/problem/P1427
题目大意:
小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了)。这对小鱼的那点记忆力来说实在是太难了,你也不想想小鱼的整个脑袋才多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。

解题思路:开一个数组存数据,然后倒着输出。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int a[101], n;
int main() {
    while (~scanf("%d", a+n) && a[n]) n ++;
    for (int i = n-1; i >= 0; i --) {
        cout << a[i];
        if (i > 0) cout << " ";
    }
    cout << endl;
    return 0;
}

不过最近学到一种 递归 写法,都不用开数组。实现代码如下:

#include <bits/stdc++.h>
using namespace std;
void f() {
    int a; cin >> a;
    if (a == 0) return;
    f();
    cout << a << " ";
}
int main() {
    f();
    return 0;
}

P1428 小鱼比可爱

题目链接:https://www.luogu.com.cn/problem/P1428
题目大意:对于数组中每个数,求它左边有多少数比它小。
解题思路:
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int n, a[101], c[101];  // c[i]用于统计数量
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) for (int j = 0; j < i; j ++) if (a[j] < a[i]) c[i] ++;
    for (int i = 0; i < n; i ++) {
        if (i) putchar(' ');
        cout << c[i];
    }
    cout << endl;
    return 0;
}

P2141 珠心算测验

题目链接:https://www.luogu.com.cn/problem/P2141
题目大意:寻找数组中有多少个数,恰好等于集合中另外两个(不同的)数之和。
解题思路:因为数据量不是很大(\(\le 100\)),所以可以开 \(O(n^3)\) 枚举。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int n, a[101], cnt;
bool check(int num) {
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < i; j ++)
            if (a[i] + a[j] == num) return true;
    return false;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) if (check(a[i])) cnt ++;
    cout << cnt << endl;
    return 0;
}

P1567 统计天数

题目链接:https://www.luogu.com.cn/problem/P1567
题目大意:统计数组的最长上升子串的长度。
解题思路:开一个变量 c,如果 \(a[i-1] \lt a[i]\) ,则 \(c = c + 1, ans = max(ans, c)\) ,否则 \(c = 1\)
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int n, a[1001000], c = 1, ans = 1;
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 1; i < n; i ++) {
        if (a[i] > a[i-1]) c ++;
        else c = 1;
        ans = max(ans, c);
    }
    cout << ans << endl;
    return 0;
}
posted @ 2019-11-24 22:10  quanjun  阅读(292)  评论(0编辑  收藏  举报