我試圖解決這個問題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.
你忘了'主()'? –
提示:使用'%'運算符,例如'next_slot =(next_slot + 1)%total_slots;' –
您在'}'令牌之前顯示錯誤消息'func.cpp:18:1:錯誤:預期聲明,你的代碼中沒有這樣的行。怎麼可能? – hyde