码迷,mamicode.com
首页 > 其他好文 > 详细

412. Fizz Buzz

时间:2018-08-17 21:33:00      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:class   tput   bsp   push   ram   color   and   continue   desc   

题目描述:


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”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

解题思路:

直接代码。

代码:

 1 class Solution {
 2 public:
 3     vector<string> fizzBuzz(int n) {
 4         vector<string> ret;
 5         for (int num = 1; num <= n; ++num) {
 6             string tmp;
 7             if (num % 3 == 0)
 8                 tmp += "Fizz";
 9             if (num % 5 == 0)
10                 tmp += "Buzz";
11             if (tmp.size() > 0) {
12                 ret.push_back(tmp);            
13                 continue;
14             }
15             else 
16                 tmp += to_string(num);
17             ret.push_back(tmp);
18         }
19         return ret;
20     }
21 };

 

412. Fizz Buzz

标签:class   tput   bsp   push   ram   color   and   continue   desc   

原文地址:https://www.cnblogs.com/gsz-/p/9495278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!