CSP 201909-2 小明種蘋果(續)
題目鏈接:[http://118.190.20.162/view.page?gpid=T93]小明種蘋果(續)
題目描述:



題目分析:
我們一部分一部分看,他輸入N,代表著有N棵樹
之后N行,每行第一個數字M代表那一行后面有多少個數字
我們要求T果實的總量,D掉落果實的樹的總量,E相鄰樹都掉落果實的組數
先處理T:M后面的第一個數字代表初始樹的數量(我們記為end),之后每出現一個非正數,就需要將end減少,如果出現正數則更新end;輸入完這一行就T+=end;即可
處理D:每次輸入非正數的時候需要判斷,是否和end相等,如果不等則Drop[i]=true;(此陣列記錄當前索引的樹是否為掉落果實的樹),事后回圈判斷Drop陣列中為true的數量即為D的數量
處理E:回圈判斷Drop[i],如果為true,則判斷Drop[i-1]和Drop[i+1]均為true則E++;
若i =1 則判斷Drop[2]和Drop[n];
若i=n 則判斷Drop[1]和Drop[n-1];
滿分代碼:
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
bool Drop[N]={false};
int main()
{
int n,m;
int T=0,D=0,E=0;
cin>>n;
for(int i=1;i<=n;i++)
{
int handle=0;
int end=-1;
cin>>m>>end;
for(int k=1;k<m;k++)
{
cin>>handle;
if(handle<=0)
{
end+=handle;
}
else{
if(end!=handle)
{
Drop[i]=true;
end=handle;
}
}
}
T+=end;
}
for(int i=1;i<=n;i++)
{
if(Drop[i])
{
D++;
if(i==1)
{
if(Drop[2]&&Drop[n])E++;
}
else if(i==n){
if(Drop[1]&&Drop[n-1])E++;
}
else{
if(Drop[i+1]&&Drop[i-1])E++;
}
}
}
cout<<T<<" "<<D<<" "<<E<<endl;
}
/*
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
*/
/*
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
*/
幾乎沒有用到很多記憶體

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/22398.html
標籤:其他
