zoukankan      html  css  js  c++  java
  • 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 package main
     2 
     3 import (
     4     "fmt"
     5     "strconv"
     6 )
     7 
     8 func main() {
     9     var n int = 100
    10     res := fizz(n)
    11     fmt.Println("Fizee Buzz: ", res)
    12 }
    13 
    14 func fizz(n int) []string {
    15     var res = []string{}
    16     if n <= 0 {
    17         return res
    18     }
    19 
    20     for i:=1;i<=100;i++ {
    21         if i%3==0 && i%5==0 {//i%15
    22             res = append(res, "fizz buzz")
    23         } else if i%3==0 {
    24             res = append(res, "fizz")
    25         } else if i%5==0 {
    26             res = append(res, "buzz")
    27         } else {
    28             res = append(res, strconv.FormatInt(int64(i), 10))
    29         }
    30     }
    31     return res
    32 }
  • 相关阅读:
    Expectation Maximization Algorithm
    Cramer-Rao Bounds (CRB)
    宽带DOA估计方法
    Statistical Methods for Machine Learning
    Bootstrap Method
    算法学习————猫树
    扩展KMP详解
    网络流的模型和应用
    [CQOI2011]动态逆序对
    CF1278F Cards
  • 原文地址:https://www.cnblogs.com/573583868wuy/p/8444940.html
Copyright © 2011-2022 走看看