参考:https://blog.csdn.net/ZscDst/article/details/80266639
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 using namespace std; 6 const int N=1005; 7 int main() 8 { 9 int n; 10 int d[N]; 11 // freopen("btext.txt","r",stdin); 12 while (cin>>n,n) 13 { 14 int s[N]; 15 for (int i=0;i<n;i++)//初始数组 16 { 17 s[i]=i+1; 18 } 19 while (cin>>d[0],d[0]) 20 { 21 for(int i=1;i<n;i++)//目的数组 22 { 23 cin>>d[i]; 24 } 25 int j=0; 26 stack<int> st; 27 for (int i=0;i<n;)//i++不能放在这里! 28 { 29 if (d[j]==s[i]) 30 { 31 j++; 32 i++; 33 continue; 34 } 35 else if (!st.empty()&&st.top()==d[j])//要先判断是否为空! 36 { 37 st.pop();//注意栈名字不要与s数组名重复! 38 j++; 39 continue; 40 } 41 else 42 { 43 st.push(s[i]); 44 i++; 45 } 46 } 47 while (!st.empty()&&st.top()==d[j])//还要看栈能否清空! 48 { 49 j++; 50 st.pop(); 51 } 52 if (st.empty()) 53 { 54 cout<<"Yes "; 55 } 56 else 57 { 58 cout<<"No "; 59 } 60 } 61 cout<<endl; 62 } 63 64 return 0; 65 }