01's Introduction
01's People
01's Issues
0001
0001
题目
(统计个数)给出n个数,统计每个数能看到的数字个数。
例如: 2 5 1 3 4
第一个数字2
可以看到:5
,因为5
将 1,3,4
挡住了,一共1
个
第二个数字5
可以看到:2,1,3,4
一共4
个
第三个数字1
可以看到:5,3,4
,因为5
将2
挡住了,一共3
个
第四个数字3
可以看到:1,5,4
,因为5
将2
挡住了,一共3
个
第五个数字4
可以看到:3,5
,因为3
将1
挡住了,5
将2
挡住了,一共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;
}
问题: 题目中的 ①②③④⑤ 分别应该是什么?
解题
分析问题
- 题目定义了什么? 定义了数字序列中
看到
和挡住
- 题目提出的问题是什么? 序列中的数字
a
能看到序列中的其他哪些数字,一共可以看到几个?
代码补全思路
过一遍代码
- 把能看懂的先看懂,不能看懂的理清思路再重点去看
- 以下代码比较简单,定义变量,输入,循环遍历数字序列
- 中间代码初看不太懂,先跳过
- 后面的代码是输出,退出
看不懂的代码
- 特殊情况下的初始值
j--
表示从大往小遍历,结束条件为0
, 即数字序列从右往左遍历
j++
表示从小往大遍历, 结束条件为n
, 即数字序列从左往右遍历
本题为补全代码的题,如果是编写代码的题应该怎么做?
整理问题解决的步骤
- ① 向左看能看到哪些数字
- ② 向右看能看到哪些数字
- ③ 加起来计数
可以尝试用文字描述下每个步骤
遍历序列中的数字 `a[0] a[1] ... a[n]`
a[i] 向左看能看到哪些数字
a[i] 向右看能看到哪些数字
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.