下面是AC代碼,C++風格:
1 class Solution { 2 public: 3 vector<string> fizzBuzz(int N) { 4 vector<string> Answer; 5 for(int i = 1;i <= N;i++) { 6 if(i % 15 == 0) { 7 Answer.push_back("fizz buzz"); 8 } else if(i % 3 == 0) { 9 Answer.push_back("fizz"); 10 } else if(i % 5 == 0) { 11 Answer.push_back("buzz"); 12 } else{ 13 Answer.push_back(to_string(i)); 14 } 15 } 16 return Answer; 17 } 18 };
此處很奇怪,為何將成員函數名fizzBuzz的首字母改成大寫,編譯返回不通過?
編譯警告Main.cpp: In function ‘int main()’:
Main.cpp:26:39: error: ‘class Solution’ has no member named ‘fizzBuzz’
vector results = solution.fizzBuzz(n);
^
EXITCODE=1