LeetCode 412. Fizz Buzz

来源:互联网 发布:qq邮箱stmp服务器端口 编辑:程序博客网 时间:2024/05/20 11:49

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

这个题跟简单,就是在to_string()函数中有点磕绊,按照C++11的标准,to_string函数对每个基础算术类型均有重载函数。但是VC2010的C++库中没有实现所有的重载函数,而是只实现了其中的几个。所以不能写成to_string(i)的形式,微软给出的解决方案是
to_string(static_cast < long long> (i))

解决方案参考 VS2010 / VC2010 BUG应对:to_string 重载函数不完整导致编译错误

class Solution {public:    vector<string> fizzBuzz(int n) {        vector<string> str;        string s;        for (int i=1;i<=n;i++)        {            if (i % 3 == 0 && i % 5 == 0)                str.push_back("FizzBuzz");            else                if (i % 3 == 0)                    str.push_back("Fizz");                else                    if (i % 5 == 0)                        str.push_back("Buzz");                    else                       str.push_back(to_string(static_cast<long long>(i)));        }        return str;    }};
0 0