簡體   English   中英

超過時間限制

[英]Time limit exceed

系統說:我的代碼被時間限制超出了。 我的代碼有捷徑可走嗎?
我正在使用矢量將釘子保存到圖形中。

輸入:e,n
輸出:checkcase = 1 =>檢查與i相鄰的u
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

您的代碼中似乎有一個無限的while循環。

您的while(sizeof(n))語句將永遠不會停止重復循環,因為sizeof(n)始終返回整數類型的字節大小,該字節大小始終是一個正數,因此始終為true。

代碼的捷徑是用實際上結束於某個時刻的for或while循環替換while循環。 另外sizeof可能不是您想要的功能。

for(int i=0; i<n; i++){可能就是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM