C语言编程. FizzBuzz程序 [英] C programming. The FizzBuzz program
问题描述
我进行了测验,并编写了以下代码:
I had a quiz and I wrote this code:
如果Fizz被3整除,则打印Fizz;如果是,则打印Buzz. 被5整除.如果是,则打印FizzBuss 可被两者整除.否则,它将打印1到100之间的数字.
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;
}
推荐答案
您也可以这样做:
#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.
这篇关于C语言编程. FizzBuzz程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!