C programming. The FizzBuzz program [closed]

2020-06-04 06:12发布

I had a quiz and I wrote this code:

Print Fizz if it is divisible by 3 and it prints Buzz if it is divisible by 5. It prints FizzBuss if it is divisible by both. Otherwise, it will print the numbers between 1 and 100.

But after I arrived home, I wondered if could have writen it with less code. However, I could not come out with a shorter code. Can I do it with a shorter code? Thanks.

This is what I wrote and I think it works well. But can I have done it with less code.

#include <stdio.h>

int main(void)
{
    int i;
    for(i=1; i<=100; i++)
    {
        if(((i%3)||(i%5))== 0)
            printf("number= %d FizzBuzz\n", i);
        else if((i%3)==0)
            printf("number= %d Fizz\n", i);
        else if((i%5)==0)
            printf("number= %d Buzz\n", i);
        else
            printf("number= %d\n",i);

    }

    return 0;
}

标签: c fizzbuzz
11条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-04 06:44

You can do it using a String:

String s="";
if(num%3==0)
   s+="fizz";
if(num%5==0)
   s+="buzz";
if(s.length()==0)
   s+=num+"";
查看更多
相关推荐>>
3楼-- · 2020-06-04 06:45
#include <stdio.h>

char const * template[] = {
  "%i",
  "Buzz",
  "Fizz",
  "FizzBuzz"
};
const int __donotuseme3[] = { 2, 0, 0 };
const int __donotuseme5[] = { 1, 0, 0, 0, 0 };
#define TEMPLATE(x) (template[__donotuseme3[(x) % 3] | __donotuseme5[(x) % 5]])

int
main(void) {
  int i;
  for (i = 1; i <= 100; i++) {
    printf(TEMPLATE(i), i);
    putchar('\n');
  }
  return 0;
}
查看更多
We Are One
4楼-- · 2020-06-04 06:45

Obfuscated form of Mr Lister's answer

main(int i){while(i++<100){printf("number= %d %s%s",i,i%3?"":"Fizz",i%5?"":"Buzz");}}

查看更多
Anthone
5楼-- · 2020-06-04 06:46

This one avoids some code repetition but requires a temporary variable char t

void FizzBuzz( ) {
    char t = 0;
    for (unsigned char i = 1; i <= 100; ++i, t = 2) {
        (i % 3) ? --t : printf("Fizz");
        (i % 5) ? --t : printf("Buzz");
        if (!t) printf("%d", i);
        printf("\n");   
    }
}
查看更多
Ridiculous、
6楼-- · 2020-06-04 06:52

If a number is divisible by both 3 and 5, then it's divisible by 15, so:

for each number 1 to 100:
    if number % 15 == 0:
        print number, "fizzbuzz"
    else if number % 5 == 0:
        print number, "buzz"
    else if number % 3 == 0:
        print number, "fizz"
    else:
        print number

Other than that, you probably won't get it much shorter, at least in a conventional language like C (and I'm assuming you don't want the normal code-golf style modifications that make your code unreadable).

You could also get the whole thing into two lines if you packed the entire main function onto a single large line, but I would hope you wouldn't be after that sort of trickery either.

You can possibly get it faster (though you should check all performance claims for yourself) with something like:

static const char *xyzzy[] = {
    "",     "",     "fizz", "",     "buzz",
    "fizz", "",     "",     "fizz", "buzz",
    "",     "fizz", "",     "buzz", "fizzbuzz",
    // Duplicate those last three lines to have seven copies (7x15=105).
};
for (int i = 1; i <= 100; i++)
    printf ("%d %s\n", i, xyzzy[i-1]);

As an aside, that array of char pointers is likely to be less space-expensive than you think, thanks to constant amalgamation - in other words, it will be likely that there will only be one of each C string.

As I say, whether it's faster should be tested. In addition, your original specs only called for the shortest code so it may be irrelevant.

查看更多
登录 后发表回答