两个代码有什么区别

What is the difference between the two codes

最近一直在做一道题,我用了类似的方法来做社论。 Link 问题如下:https://www.spoj.com/problems/STPAR/

编辑代码(虽然来自第三方网站,但我已经测试过了,它给了AC)-

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

int a[1111], n;

int main() {
  while (scanf("%d", &n) && n != 0) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    int k = 0, i = 0;
    stack<int> st;
    while (i < n) {
      while (st.size() && st.top() == k + 1) k++, st.pop();
      if (a[i] != k + 1) st.push(a[i]);
      else k++;
      i++;
    }
    while (st.size() && st.top() == k + 1) k++, st.pop();
    puts(k == n ? "yes" : "no");
  }
}

我的代码 -

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    while(t)
    {
        int n;
        cin>>n;

        if(n==0)
        {
            break;
        }

        int arr[n];
        for(int i=0; i<n; i++)
        {
            cin>>arr[i];
        }

        int p=1;
        stack<int> s;
        int flag=0;
        for(int i=0; i<n; i++)
        {
            while(!s.empty() && s.top()==p)
            {
                s.pop();
                p++;
            }
            if(p==arr[i])
            {
                p++;
            }else if(!s.empty() && s.top()<arr[i])
            {
                flag=1;
                break;
            }else{
                s.push(arr[i]);
            }

        }

        if(flag==0)
        {
            cout<<"yes"<<endl;
        }else{
        cout<<"no"<<endl;}
    }
}

我只是无法理解这两个代码之间的区别,因为它们使用完全相似的方法并且代码也几乎相似。帮助将不胜感激:)

您尚未初始化 t 值。而且你也没有减少它。

根据问题,没有输入测试用例。所以删除变量 t 和 while 循环(我的意思是删除 while 循环而不是其中的代码)。 运行 再来一次。