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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

LeetCode題解:FizzBuzz

2019-11-14 11:15:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

思路:

太簡(jiǎn)單。

題解:

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;}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 滨海县| 靖安县| 琼海市| 进贤县| 巴马| 凤城市| 修水县| 贡山| 黎川县| 三原县| 新河县| 乌恰县| 陈巴尔虎旗| 夏河县| 磐安县| 安徽省| 巨鹿县| 贡嘎县| 平湖市| 彰化县| 会同县| 高碑店市| 紫云| 伊川县| 永兴县| 赫章县| 文成县| 石门县| 正安县| 岳普湖县| 南城县| 双鸭山市| 张家港市| 焉耆| 兴和县| 台湾省| 怀来县| 湘乡市| 于田县| 吉木萨尔县| 思南县|