为什么我会出现访问冲突错误C ++?

时间:2018-10-27 01:05:55

标签: c++11

我收到0xc0000005错误(访问冲突错误),此代码在哪里出错? 我无法调试此错误。请帮助我。

问题是这个

通常,给定一面无限高的墙,最初未上漆。发生N次操作,并且在第一个操作中,墙壁以颜色Ci喷涂到高度Hi。假设在第j次操作(j> i)中,墙壁以颜色Cj喷涂到高度Hj,使得Hj> = Hi,则墙壁上的Cith颜色被隐藏。在N次操作结束时,您必须找到墙上可见的不同颜色(> = 1)的数量。

#include<iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;

int main()
{
   int t;
   cin>>t;
   for(int tt= 0;tt<t;tt++)
   {
       int h,c;
       int temp = 0;
       cin>>h>>c;
       int A[h], B[c];
       vector<int> fc;
       for(int i = 0;i<h;i++)
       {
          cin>>A[i];
       }
       for(int j =0;j<h;j++)
       {
           cin>>B[j];
       }
       if(is_sorted(A,A+h))
       {
           return 1;
       }
       if(count(A,A+h,B[0]) == h)
       {
           return 1;
       }


       for(int i = 0;i<h;i++)
       {

          if(A[i]>=temp)
          {
              temp = A[i];

          }
          else
          {
              if(temp == fc[fc.size()-1])
              {
                  fc[fc.size()-1] = B[i];
              }
              else
              {
                  fc.push_back(B[i]);
              }
           }
         }


     }
  }

1 个答案:

答案 0 :(得分:0)

有几个问题。

将值读入B时,循环检查为j<hB中有多少个元素?

您稍后将查看fc[fc.size()-1]。如果fc为空,这是未定义的行为,并且可能是问题的根源。

其他问题:

  1. Don't use #include <bits/stdc++.h>
  2. Avoid using namespace std;
  3. int A[h]这样的变量声明不是标准C ++,其中h是变量。一些编译器支持它们作为扩展。
相关问题