ZeroJudge-i071.風景(Landscape)解題思路 ===================== ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=i071 :::warning - 紀錄小明家的高度,並從小明家往左、往右比較。 - 以變數(lefthighest、righthighest)記錄小明家左邊、右邊最高樓高(初值為小明家樓高)。從小明家往左、往右比較,若比較的樓高>lefthighest、righthighest,則答案sum+1(記得每次執行時將sum重新計算),並調整lefthighest、righthighest。 - <font color=cake>注意小明家高度在陣列的位置,要存在輸入位置-1,即building[location-1]</font>。 ::: ### C++程式碼 ```c= #include<bits/stdc++.h> using namespace std; main(){ ios::sync_with_stdio(0);cin.tie(0); int num,location,height,lefthighest,righthighest; while (cin>>num){ int sum=0; int building[1000]; cin>>location; for(int i=0;i<num;i++){ cin>>building[i]; } lefthighest=righthighest=building[location-1]; for(int l=location-2;l>=0;l--){ if(building[l]>lefthighest){ sum++; lefthighest=building[l]; } } for (int r=location;r<=num-1;r++){ if(building[r]>righthighest){ sum++; righthighest=building[r]; } } cout<<sum<<endl; } return 0; } ```