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条回答
Melony?
2楼-- · 2020-06-04 06:30

I'm not sure when you'd start calling it unreadable, but there's this.

#include <stdio.h>

int main(void)
{
   int i = 1;
   for (; i<=100; ++i) {
      printf("number= %d %s%s\n", i, i%3?"":"Fizz", i%5?"":"Buzz");
   }
   return 0;
}
查看更多
放我归山
3楼-- · 2020-06-04 06:30
void main()
{
    int i = 0;
    char h[4];

    while (++i <= 100)
    {
        sprintf(h, "%d", i);
        printf("%s%s%s\n", i%3 ? "" : "fizz", i%5 ? "" : "buzz", (i%3 && i%5) ? h: "");
    }
}
查看更多
做个烂人
4楼-- · 2020-06-04 06:35

You could also do:

#include <stdio.h>

int main(void)
{
    int i;
    for(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("number=%d", i);
        printf("\n");
    }

    return 0;
}

A few lines shorter, and a lot easier to read.

查看更多
等我变得足够好
5楼-- · 2020-06-04 06:35

I'd go with a helper function :-)

#include <stdio.h>

int fbindex(int n) {
    int i = 0;
    if (n % 3 == 0) i += 1;
    if (n % 5 == 0) i += 2;
    return i;
}

int main(void) {                                 
    const char *fb[] = {"%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n"};
    for (int i = 1; i <= 100; i++) printf(fb[fbindex(i)], i);                
}
查看更多
萌系小妹纸
6楼-- · 2020-06-04 06:38

i would write something like that

    main(){
  if (i % 3 == 0){
  cout<<"Fizz";
  }
  if (i % 5 == 0){
  cout<<"Buzz";
  }
  // So if both are true, it will print “FizzBuzz” and augment the two strings
    }
查看更多
戒情不戒烟
7楼-- · 2020-06-04 06:40

I would say that modulo is expensive while comparisons are cheap so only perform the modulo once. That would yield something like this.

int i;
for( i = 0; i!=100; ++i ) {
    bool bModThree = !(i % 3);
    bool bModFive = !(i % 5);

    if( bModThree || bModFive ) {
        if( bModThree ) {
            printf( "Fizz" );
        }
        if( bModFive ) {
            printf( "Buzz" );
        }
    } else {
        printf( "%d", i );
    }

    printf( "\n" );
}
查看更多
登录 后发表回答