当前位置:网站首页>P1567 统计天数【入门】
P1567 统计天数【入门】
2022-07-16 02:32:00 【会编程的露娜】
统计天数<------题目传送门
这个题说白了就是:寻找一个数组中连续递增的最长子序列的元素个数。
输入格式
第 1 行:一个整数 N N N 。 1 ≤ N ≤ 1 0 6 1 \leq N \leq 10^6 1≤N≤106
第 2 行: N N N个空格隔开的整数 x,表示每天的气温。 0 ≤ 0 \leq 0≤ x ≤ 1 0 9 \leq 10^9 ≤109 。
输出格式
1 行:一个整数,表示最高气温一直上升的最长连续天数。
样例输入 #1
10
1 2 3 2 4 5 6 8 5 9
样例输出 #1
5
思路:
需要2个计数器就行了。
x 记录连续递增天数的个数,如果升温,++x,如果降温,x 从1开始重新计数。
num 用来记录最长的天数。每次循环我们对比 x 和 num 的值,如果当前的持续天数已经超过了最大值,那么更新 num 保存最大值。
last 记录当天的温度
now 记录下一天的温度
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<fstream>
#include<iomanip>
#include<utility>
#include<unordered_map>
using namespace std;
int main(){
int n,x=1,num=1;
int last,now;
cin>>n;//输入总共的天数
cin>>last;//先记录下第一天的温度
for(int i=1;i<n;++i){
cin>>now;//输入剩下的温度
if(now>last){
++x;//如果是递增的温度,就连续天数加1,将num和x的最大值赋给num
num=max(num,x);
}
else
x=1;
last=now;//因为每次都是后一个和前一个比较,所以都要把输入的值赋给前一个
}
cout<<num<<endl;
return 0;
}
边栏推荐
- 从应用到底层:36张图带你进入Redis世界(上)
- [Vulnhub] Raven-1
- PostgreSQL每周人物采访
- Sorting of two fields in redis
- FreeRTOS个人笔记-列表与列表项
- [idea] add VM options to idea
- C# 使用JObject解析嵌套json
- 021.多态详解 续2
- ["plaything determination" scratch children programming] tank turbulence - "implementation of" through wall control "and" bullet rebound "- including complete code
- 模拟实现C语言中常用函数
猜你喜欢

PostgreSQL每周人物采访

Redis的安装(Windows)及常用的使用方法

Introduction to Chang'an chain-01

【C】 Dynamic memory management (related functions, common errors, written test questions)

What is the function of GIS service platform for pipe network management?

In three steps, I finished MySQL in one day, which made me win tmall offer smoothly

判等问题:程序里如何确定你就是你?

今晚8点! LightDB PG分布式数据库技术创新与实践”

数据库索引:索引并不是万能药

从应用到底层:36张图带你进入Redis世界(上)
随机推荐
MySQL死锁分析与解决之路
数据库索引:索引并不是万能药
Gson解析生成json数据工具类
Sorting of two fields in redis
Detailed explanation and examples of anti shake throttling
PostgreSQL weekly person interview
023.static与final使用陷阱
0x22, 0x2e services of UDS
Top100国内NFT平台 联盟链、公链使用情况统计
单目标追踪——【相关滤波】ATOM:Accurate Tracking by Overlap Maximization
Web programming interview question (2022)
汉字风格迁移篇--基于生成对抗网络的无监督字体风格转换模型
Appium automation test foundation - WebView operation (key)
C # use jobject to resolve nested JSON
[wechat applet] slide effect realization
Detailed explanation of assembly language programming skills (with examples)
AcWing 368. 银河 题解(强连通分量做差分约束问题)
【C】 Dynamic memory management (related functions, common errors, written test questions)
MYSQL一站式学习,看完即学完
谷歌 | 图神经网络预训练帮助了分子表征吗