Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0001 #1

Open
junxnone opened this issue Jun 1, 2024 · 0 comments
Open

0001 #1

junxnone opened this issue Jun 1, 2024 · 0 comments

Comments

@junxnone
Copy link
Owner

junxnone commented Jun 1, 2024

0001

题目

(统计个数)给出n个数,统计每个数能看到的数字个数。

例如: 2 5 1 3 4

第一个数字2可以看到:5,因为51,3,4 挡住了,一共1
第二个数字5可以看到:2,1,3,4一共4
第三个数字1可以看到:5,3,4,因为52挡住了,一共3
第四个数字3可以看到:1,5,4,因为52挡住了,一共3
第五个数字4可以看到:3,5,因为31挡住了,52挡住了,一共2

#include <iostream>
using namespace std;

int a[100];
int b[100];
int now;
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = 0; i < n; i++)
    {
        now = ①;

        if (i == 0 || i == n - 1)
            b[i] = 1;
        else
            b[i] = ②;

        for (int j = ③; j >= 0; j--)
        {
            if (a[j] > now)
            {
                now = a[j];
                b[i]++;
            }
        }

        now = ④;

        for (int j = ⑤; j < n; j++)
        {
            if (a[j] > now)
            {
                now = a[j];
                b[i]++;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << b[i] << " ";
    }
    return 0;
}

问题: 题目中的 ①②③④⑤ 分别应该是什么?

解题

分析问题

  1. 题目定义了什么? 定义了数字序列中 看到挡住
  2. 题目提出的问题是什么? 序列中的数字a能看到序列中的其他哪些数字,一共可以看到几个?

代码补全思路

过一遍代码

  • 把能看懂的先看懂,不能看懂的理清思路再重点去看

  • 以下代码比较简单,定义变量,输入,循环遍历数字序列
<iframe frameborder="0" scrolling="no" style="width:100%; height:394px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L1-L15&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>
  • 中间代码初看不太懂,先跳过
<iframe frameborder="0" scrolling="no" style="width:100%; height:625px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L16-L41&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>
  • 后面的代码是输出,退出
<iframe frameborder="0" scrolling="no" style="width:100%; height:163px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L43-L46&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>

看不懂的代码

  • 特殊情况下的初始值
<iframe frameborder="0" scrolling="no" style="width:100%; height:163px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L18-L21&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>
  • j-- 表示从大往小遍历,结束条件为 0 , 即数字序列从右往左遍历
<iframe frameborder="0" scrolling="no" style="width:100%; height:247px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L23-L30&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>
  • j++ 表示从小往大遍历, 结束条件为 n, 即数字序列从左往右遍历
<iframe frameborder="0" scrolling="no" style="width:100%; height:247px;" allow="clipboard-write" src="https://junxnone.github.io/emgithub/iframe.html?target=https%3A%2F%2Fgithub.com%2Fjunxnone%2F01%2Fblob%2Ff97f62c8414a11e89646275f9316a2660ca27f76%2Fdocs%2Fcode%2F0001.cpp%23L34-L41&style=agate&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></iframe>

本题为补全代码的题,如果是编写代码的题应该怎么做?

整理问题解决的步骤

  • ① 向左看能看到哪些数字
  • ② 向右看能看到哪些数字
  • ③ 加起来计数

可以尝试用文字描述下每个步骤

遍历序列中的数字 `a[0] a[1] ... a[n]`
   a[i] 向左看能看到哪些数字
   a[i] 向右看能看到哪些数字
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant