2017-08-08 121 views
-1

我試圖解決這個問題in Codility ...
下面是代碼:循環旋轉codility C++解決方案

#include <iostream>  
#include <vector>  
#include <algorithm> 
using namespace std; 

vector<int> solution(vector<int> &A, int k); 
vector<int> A; 
A.push_back(3);  
A.push_back(5); 
A.push_back(7); 
A.push_back(9); 
A.push_back(2); 
int k; 
rotate(A.rbegin(),A.rbegin()+k, A.rend()); 

雖然我的編譯器編譯,並沒有問題跑,codility表明我「的錯誤: 'A'沒有命名一個類型「。 這是用我的編譯器檢查它的代碼:

#include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 

    int main() 
    { 
    vector<int> myVector; 
    myVector.push_back(3); 
    myVector.push_back(5); 
    myVector.push_back(7); 
    myVector.push_back(9); 
    myVector.push_back(2); 
    for(unsigned i=0;i<myVector.size();i++) 
    { 
    cout<<myVector[i]<<" "; 
    } 
    cout<<endl; 
    int k; 
    cout<<"Insert the times of right rotation:"; 
    cin>>k; 
    rotate(myVector.rbegin(),myVector.rbegin()+k, myVector.rend()); 
    for(unsigned i=0;i<myVector.size();i++) 
    { 
    cout<<myVector[i]<<" "; 
    } 
    } 

編譯器輸出:

func.cpp:9:3: error: 'A' does not name a type 
    A.push_back(3); 
^
func.cpp:10:3: error: 'A' does not name a type 
    A.push_back(5); 
^
func.cpp:11:3: error: 'A' does not name a type 
    A.push_back(7); 
^
func.cpp:12:3: error: 'A' does not name a type 
    A.push_back(9); 
^
func.cpp:13:3: error: 'A' does not name a type 
    A.push_back(2); 
^
func.cpp:16:9: error: expected constructor, destructor, or type conversion before '(' token 
    rotate(A.rbegin(),A.rbegin()+k, A.rend()); 
     ^
func.cpp:18:1: error: expected declaration before '}' token 
} 
^ 

Detected some errors. 
+1

你忘了'主()'? –

+0

提示:使用'%'運算符,例如'next_slot =(next_slot + 1)%total_slots;' –

+1

您在'}'令牌之前顯示錯誤消息'func.cpp:18:1:錯誤:預期聲明,你的代碼中沒有這樣的行。怎麼可能? – hyde

回答

0

我想你有很多的問題,並提出了一些假設 這裏是工作的代碼

1.You並不需要創建一個新的向量作爲功能已被引用的矢量& A,使任何變化將直接反映到原來的載體

2.value K是旋轉的點,這已經是輸入功能(因此沒有必要的CIN)

現在弄明白了100%

// you can use includes, for example: 
// #include <algorithm> 

// you can write to stdout for debugging purposes, e.g. 
// cout << "this is a debug message" << endl; 
#include<algorithm> 


vector<int> solution(vector<int> &A, int K) 
{ 
    if (A.empty()) 
    { 
     return A; 
    } 
    K = K % A.size(); 

    if (A.size() == 0 || A.size() == 1 || K == 0) 
    { 
     return A; 

    } 
    else 
    { 
     std::rotate(A.rbegin(), A.rbegin() + K, A.rend()); 
     return A; 

    } 

} 

輸出

enter image description here

+0

@alekastyl上面是我提交的codelity –

+0

的快照,佔用了62%。 – alekastyl

+0

@alekastyl。得到它100%工作 –

1

只能定義或外部函數聲明符號。您的A.push_back(3);等不是定義或聲明聲明。這就是爲什麼你會得到有趣的錯誤信息:定義和聲明以類型開始,並且由於行在函數之外,編譯器期望看到類型,而不是A

所以你需要一個功能,是這樣的:

#include <iostream>  
#include <vector>  
#include <algorithm> 
using namespace std; 

vector<int> solution(vector<int> &A, int k) 
{ //added 
    //vector<int> A; // this is function argument, not local variable 
    A.push_back(3);  
    A.push_back(5); 
    A.push_back(7); 
    A.push_back(9); 
    A.push_back(2); 
    //int k; // this is function argument, not local variable 
    rotate(A.rbegin(),A.rbegin()+k, A.rend()); 
    return A; // Added since the function needs to return a vector of int... 
} //added 

應該編譯,做一些事情。所以我所做的是你可能打算做的:代碼現在在solution函數中。

+0

我想他錯誤地在'solution'後面加了一個分號,而不是定義它。因此,編譯失敗的代碼應該在函數的大括號內。 – 0x499602D2

+0

感謝您的幫助,但是這些代碼的可信度爲0%。 – alekastyl

+0

@alekastyl您是否嘗試過我的解決方案 –