第十次总结
今天依旧不废话 开始吧
T1:
题意:
给一个nm的方阵,要你求2 2总和最大的方阵,输出最大的总和,还要输出这个方阵的左上角
思路:
先输入,然后拿个maxx(打擂台),再然后拿到这个方阵的和,跟maxx比,如果大就把maxx=这个方阵的和,存下这个坐标
错因:
我在做的时候脑子乱了,没想过这么简单的思路,代码就跟没了
AC代码:
#include<bits/stdc++.h>
using namespace std;
int a[1005][1005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,s=0;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int maxx=0,max1,max2;
for(int i=1;i<n;i++){//1~n-1!
for(int j=1;j<m;j++){//1~m-1!
int x=i,y=j;
s=(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]);//记下和
if(maxx<s){//打擂台
maxx=s;//记录
max1=x;
max2=y;
}
}
}
cout<<maxx<<endl<<max1<<" "<<max2;//输出
return 0;
}
T2:
题意:
给你一个用后缀表达式的式子,让你算出这个式子的答案
思路:
拿个栈,输入,如果发现是数字就存到栈,是运算符就把栈的前两个数给存到两个变量里,然后根据运算符去运算,把结果存进栈,最后输出栈顶
代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
char a;
int dq,zd;
while(cin>>a){
if(a>='0'&&a<='9'){
s.push(int(a-'0'));
}
if(a=='+'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd+dq);
}
else if(a=='-'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd-dq);
}
else if(a=='*'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd*dq);
}
else if(a=='/'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd/dq);
}
}
cout<<s.top();
return 0;
}
T3:
题意:
给你一个用后缀表达式的式子,让你算出这个式子的答案(可能会有多位数)
思路:
在之前的代码上的存数字多加点东西就行:拿个num,while如果下一个不是空的就把num*10加上这位数,最后存进去,其他的还是一样
错因:
没想到多位数咋处理(本来发现了)
代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
char a;
int dq,zd;
while(cin>>a){
if(a>='0'&&a<='9'){
s.push(int(a-'0'));
}
if(a=='+'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd+dq);
}
else if(a=='-'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd-dq);
}
else if(a=='*'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd*dq);
}
else if(a=='/'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd/dq);
}
}
cout<<s.top();
return 0;
}
(直接把上一题的复制过来了)
AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string a;
getline(cin,a);
int dq,zd;
for(int i=0;i<a.size();i++){
if(a[i]>='0'&&a[i]<='9'){
int num=0;
while(a[i]!=' '){
num=num*10+a[i]-'0';
i++;
}
s.push(num);
}
if(a[i]=='+'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd+dq);
}
else if(a[i]=='-'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd-dq);
}
else if(a[i]=='*'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd*dq);
}
else if(a[i]=='/'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd/dq);
}
}
cout<<s.top();
return 0;
}
T4:
题意:
给你一个用后缀表达式的式子,让你算出这个式子的答案(可能会有多位数和负数)
思路:
继续在上一题的基础上改:在外面放一个f,代表是不是负数,在存数字的时候把num和f相乘,f=1,在看-号时发现后面还有数字,就把f为-1并直接进下个循环
错因:
上一题都没做出来这题就跟别说了
AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string a;
getline(cin,a);
int dq,zd,f=1;
for(int i=0;i<a.size();i++){
if(a[i]>='0'&&a[i]<='9'){
int num=0;
while(a[i]!=' '){
num=num*10+a[i]-'0';
i++;
}
s.push(num*f);
f=1;
}
if(a[i]=='+'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd+dq);
}
else if(a[i]=='-'){
if(a[i+1]>='0'&&a[i+1]<='9'){
f=-1;
continue;
}
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd-dq);
}
else if(a[i]=='*'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd*dq);
}
else if(a[i]=='/'){
dq=s.top();
s.pop();
zd=s.top();
s.pop();
s.push(zd/dq);
}
}
cout<<s.top();
return 0;
}
总结:
一句话,多刷题
乾坤未定,你我皆是黑马