Fizz Buzz 问题

Fizz Buzz 问题

给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:

  • 如果这个数被3整除,打印fizz.
  • 如果这个数被5整除,打印buzz.
  • 如果这个数能同时被35整除,打印fizz buzz.

样例

比如 n = 15, 返回一个字符串数组:

[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"
]

标签

枚举法 基本实现

 1 class Solution {
 2     public:
 3     vector<string> fizzBuzz(int n)  {
 4         vector<string> result;
 5         for (int i = 1; i <= n; i++)  {
 6             if (i % 15 == 0)  {
 7                 result.push_back("fizz buzz");
 8             } else if (i % 3 == 0) {
 9                 result.push_back("fizz");
10             } else if (i % 5 == 0) {
11                 result.push_back("buzz");
12             } else {
13                 result.push_back(to_string(i));
14             }
15         }
16         return result;
17     }
18 };
时间: 2024-10-01 07:21:30

Fizz Buzz 问题的相关文章

412. Fizz Buzz

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 fiv

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

LeetCode 412. Fizz Buzz

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 bot

LintCode (9)Fizz Buzz

下面是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_bac

Lintcode: Fizz Buzz

Given number n. Print number from 1 to n. But: when number is divided by 3, print fizz; when number is divided by 5, print buzz; when number is divided by both 3 and 5, print fizz buzz. Example If n=15, you should return ["1", "2", &qu

Lintcode9 Fizz Buzz solution 题解

[题目描述] Given number n. Print number from 1 to n. But: when number is divided by 3, print "fizz". when number is divided by 5, print "buzz". when number is divided by both 3 and 5, print "fizz buzz". 给你一个整数n. 从 1 到 n 按照下面的规则打印

LintCode 第一题fizz buzz

[题目] 给你一个整数n. 从1到n按照下面的规则打印每个数: 如果这个数被3整除,打印fizz 如果这个数被5整除,打印buzz 如果这个数能同时被3和5整除,打印fizz buzz 样例 比如n=15, 返回一个字符串数组: ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz

[容易]Fizz Buzz 问题

题目来源:http://www.lintcode.com/zh-cn/problem/fizz-buzz/ 可以accept的程序如下: 1 class Solution { 2 public: 3 /** 4 * param n: As description. 5 * return: A list of strings. 6 */ 7 vector<string> fizzBuzz(int n) { 8 vector<string> results; 9 for (int i

Leetcode: Fizz Buzz

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 bot