时限超过

时间:2017-03-06 11:16:55

标签: c++ vector graph

系统说:我的代码超出时限。是否有我的代码快捷方式?
  我使用矢量将指甲保存到图表中。

输入:e,n
输出:checkcase = 1 =>用i确认邻近         checkcase = 2 =>找到你周围的指甲

#include <iostream>
#include <vector>
#include <list>
#include <string>

using namespace std;

int main()
{
  int e, n;
  string u, i;
  //using vectors
  vector<string> graph;
  //use list
  list<string>listElementsInCase2;

  cin >> e;
  cin >> n;
  //loop for e
  for (long index = 0; index < e; index++)
  {
    cin>>u>> i;
    //add u to graph 
    graph.push_back(u);
    //add i to graph 
    graph.push_back(i);
  }
  //Option
  int checkCase;
  long index;
   //loop for n
    while(sizeof(n))
  {
    cin >> checkCase;

    if (checkCase == 1)
    {
        cin >> u >> i;

        for (index = 0; index < 2 * e; index += 2)
        {   //Check u adjacent ? i
            if ((graph.at(index) == u) && (graph.at(index + 1) == i))
            {
                cout << "TRUE" << endl;
                break;
            }
        }

        if (index == 2 * e)
            cout << "FALSE" << endl;
    }
    //checkCase=2
    if (checkCase == 2)
    {
        cin >> u;

        for (long index = 0; index < 2 * e; index += 2)
        {   
            if (graph.at(index) == u)
                listElementsInCase2.push_back(graph.at(index + 1));
        }
          // return 0
        if (listElementsInCase2.empty() == true)
            cout << "0";
        else
        {
            while (0 < listElementsInCase2.size())
            {   //find nails that around u
                cout << listElementsInCase2.front();
                listElementsInCase2.pop_front();
                cout << " ";
            }
        }

        cout << endl;
    }

    n--;
   }
 }
//end

1 个答案:

答案 0 :(得分:2)

你的代码似乎有一个无限的while循环。

你的语句while(sizeof(n))永远不会停止重复循环,因为sizeof(n)总是返回整数类型的字节大小,它总是一个正数,因此总是计算为真。

代码的快捷方式是使用for或while循环替换while循环,该循环实际上在某个时刻结束。 sizeof也可能不是你想要的功能。

for(int i=0; i<n; i++){可能就是你要找的东西。