Code Monkey home page Code Monkey logo

01's Introduction

0~1

01's People

Contributors

junxnone avatar

Watchers

 avatar

01's Issues

0001

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] 向右看能看到哪些数字

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.