国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode題解:FizzBuzz

2019-11-14 09:52:58
字體:
來源:轉載
供稿:網友

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"]

思路:

太簡單。

題解:

std::vector<std::string> fizzBuzz(int n) {    std::vector<std::string> result(n);    for(int i = 1; i <= n; ++i) {        if (i % 3 && i % 5)            result[i - 1] = std::to_string(i);        else if (i % 3)            result[i - 1] = "Buzz";        else if (i % 5)            result[i - 1] = "Fizz";        else            result[i - 1] = "FizzBuzz";    }    return result;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇州市| 饶平县| 资阳市| 揭阳市| 沽源县| 馆陶县| 禄劝| 江山市| 周至县| 乌鲁木齐市| 天台县| 合山市| 洛扎县| 土默特右旗| 晋城| 武宣县| 桐柏县| 武安市| 郧西县| 阜平县| 昭平县| 赣州市| 尖扎县| 扎赉特旗| 仁寿县| 沐川县| 墨竹工卡县| 上林县| 华坪县| 江津市| 临清市| 潼南县| 凤台县| 开远市| 沙洋县| 庆城县| 夏津县| 临朐县| 玉环县| 乌兰察布市| 双鸭山市|