# Jserv’s Solution to FizzBuzz 實做分析

Feb 7 · 9 min read

`for(int i = 1; i <= n; i++){     String result = "";     if(i%3 == 0) result += "Fizz";     if(i%5 == 0) result += "Buzz";     if(result.length() > 0)       System.out.println(result);     else       System.out.println(i);}`

`能不要用模數運算(modulo operation)嗎？`

`分支判斷是否過多？`

`每次重新記憶體分配同樣的 string literal 是否過於浪費記憶體空間？`

`#include <stdio.h> #include <string.h>/*  * Based on the way printf manipulating format strings, we can specify * a single string for each kind of thing we want to print, in power of * two lengths. * * For a single integer, “%u” length == 2 * For fizz or buzz, “fizz” or “buzz” length == 4 * For fizzbuzz, length == 8 * * Even better, we can set up a string so the starts of our format are at * powers of 2 * * “fizzbuzz%u” * 0 4 8 */#define MSG_LEN 8#define DIV5(x) ((x & 2) >> 1)#define DIV3(x) (x & 1)int main(int argc, char **argv){    for (size_t i = 1; i <= 100; i++) {        /* Define mask/flags for storing divisibility */        unsigned int mask = 0;        /* Create bitmask of the form 000000ab           a — divisible by 5           b — divisible by 3 */        mask |= (!(i % 5) << 1) | !(i % 3);        unsigned int start =            // Shift down 1 if div5            (MSG_LEN >> DIV5(mask))            // Shift down another 4 if div3 */            >> (DIV3(mask) << 2);        // Default length is 2        // Shift up by one for each bit set        unsigned int length = (2 << (DIV3(mask))) << DIV5(mask);        // Shift up by 1 for each bit set        char fmt[MSG_LEN + 1];        strncpy(fmt, &”FizzBuzz%u”[start], length);        fmt[length] = ‘\0’;        printf(fmt, i);        printf(“\n”);    }    return 0;}`

`#include <stdio.h>int main () {    for (unsigned int i = 1; i < 100; i++) {       if (i % 3 == 0) printf(“fizz”);       if (i % 5 == 0) printf(“buzz”);       if ((i % 3 != 0) && (i % 5 != 0)) printf(“%u”, i);       printf(“\n”);    }}`

Written by

## 陳奕熹

#### 應該要是一個工程師的……

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just \$5/month. Upgrade