题目:
第一道:P1567 统计天数
题目描述
炎热的夏日,KC 非常的不爽。他宁可忍受北极的寒冷,也不愿忍受厦门的夏天。最近,他开始研究天气的变化。他希望用研究的结果预测未来的天气。
经历千辛万苦,他收集了连续 N(1≤N≤10^6) 天的最高气温数据。
现在,他想知道最高气温一直上升的最长连续天数。
输入格式
第 1 行:一个整数 N 。1≤N≤10^6
第 2 行:N个空格隔开的整数,表示连续 N 天的最高气温。0≤ 最高气温 ≤10^9 。
输出格式
1 行:一个整数,表示最高气温一直上升的最长连续天数。
输入输出样例
输入 #1
10 1 2 3 2 4 5 6 8 5 9
输出 #1复制
5
在讲思路前,我想吐槽一下,题目这两个数据太不真实了!
1.注意到天数是≤10^6的(即≤1000000),那么我们把这个数换算成年得到......2739.72602739!(一年按365天计算)这KC也太牛了吧,收集将近3000年!
2.气温≤10^9!40°C就热死了,你跟我说10亿度?!
吐槽完毕,下面——上思路!
思路1(WA):
这题好简单!不就是遍历一下,取个最大值吗?轻轻松松拿下90分
WA代码:
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
int mx=-1,cnt=0;
for(int i=1;i<n;i++)
{
if(a[i]>a[i-1])
{
cnt++;
}
else
{
mx=max(mx,cnt);
cnt=0;
}
}
cout<<mx+1;
return 0;
}
一气之下差点下载数据!后来发现了一组错误样例,如下:
10
1 2 3 4 5 6 7 8 9 10
思路2(AC):
上面样例输出是0!!!后面发现,最后没有更新mx,要是他最后温度一直上升,还超过了历史最长天数,那不就完犊子了?最后更新一下mx,应该对了吧!
AC代码:
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
int mx=-1,cnt=0;
for(int i=1;i<n;i++)
{
if(a[i]>a[i-1])
{
cnt++;
}
else
{
mx=max(mx,cnt);
cnt=0;
}
}
if(cnt>mx)
{
mx=cnt;
}
cout<<mx+1;
return 0;
}
在这里再给大家另一种AC代码,思路和上面差不多,就不细讲了
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main()
{
int n,cnt=0,ans=0;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> a[i];
if(a[i]>a[i-1])
{
cnt++;
if(cnt>ans)
{
ans=cnt;
}
}
else
{
cnt=0;
}
}
if(cnt==n)
{
cout<<cnt;
}
else if(cnt>ans)
{
cout<<cnt;
}
else
{
cout<<ans+1;
}
return 0;
}
第二道:P1597 语句解析
题目背景
木有背景……
题目描述
一串长度不超过 255 的 PASCAL 语言代码,只有 a,b,c 三个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变量,每条赋值语句的格式是 [变量]:=[变量或一位整数];
未赋值的变量值为 0 输出 a,b,c 的值。
输入格式
一串符合语法的 PASCAL 语言,只有 a,b,c 三个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变量,未赋值的变量值为 0。
输出格式
输出 a,b,c 最终的值。
输入输出样例
输入 #1
a:=3;b:=4;c:=5;
输出 #1
3 4 5
说明/提示
输入的 PASCAL 语言长度不超过 255。
首先,我们要知道PASCAL语言的变量赋值方式:
将b赋值给a(和c++有点像,只是多了一个:)
a:=b;
将a赋值为1
a:=1;
知道了这个之后,我们就可以开始做题了
思路:
首先定义一堆变量......然后while循环里面处理,具体请见代码(WA的,别抄)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
char d,e,f,g,h;
while(cin >> d)
{
cin >> e >> f >> g >> h;
if(g>='0'&&g<='9')
{
g-=48;
if(d=='a')
{
a=g;
}
else if(d=='b')
{
b=g;
}
else if(d=='c')
{
c=g;
}
}
else if(g=='a')
{
if(d=='a')
{
a=a;
}
else if(d=='b')
{
b=a;
}
else if(d=='c')
{
c=a;
}
}
else if(g=='b')
{
if(d=='a')
{
a=b;
}
else if(d=='b')
{
b=b;
}
else if(d=='c')
{
c=b;
}
}
else if(g=='c')
{
if(d=='a')
{
a=c;
}
else if(d=='b')
{
b=c;
}
else if(d=='c')
{
c=c;
}
}
}
cout<<a<<" "<<b<<" "<<c;
return 0;
}
发现问题了吗?对,没错,abc没有赋值!
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a=0,b=0,c=0;
char d,e,f,g,h;
while(cin >> d)
{
cin >> e >> f >> g >> h;
if(g>='0'&&g<='9')
{
g-=48;
if(d=='a')
{
a=g;
}
else if(d=='b')
{
b=g;
}
else if(d=='c')
{
c=g;
}
}
else if(g=='a')
{
if(d=='a')
{
a=a;
}
else if(d=='b')
{
b=a;
}
else if(d=='c')
{
c=a;
}
}
else if(g=='b')
{
if(d=='a')
{
a=b;
}
else if(d=='b')
{
b=b;
}
else if(d=='c')
{
c=b;
}
}
else if(g=='c')
{
if(d=='a')
{
a=c;
}
else if(d=='b')
{
b=c;
}
else if(d=='c')
{
c=c;
}
}
}
cout<<a<<" "<<b<<" "<<c;
return 0;
}
这篇博客就到这里啦,我们下篇博客见!
标签:语句,10,cnt,洛谷,int,题解,cin,mx,赋值 From: https://blog.csdn.net/woshishabi0716/article/details/140894791