整除
题目描述:输出100到1000之间能被5和6同时整除的数,输出格式为每10个数为一行。
输入描述:无输入
输出描述:按题目意思输出,相邻两个数之间用空格隔开(注意每一行末尾没有空格)。
C语言的代码
#include <stdio.h>
int main()
{
int count=0; //记录每行的个数达到几个
for(int i=100;i<=1000;i++)
{
if(i%5==0&&i%6==0)
{
count++;
if(count%10!=0) printf("%d ",i); //判断是否需要换行
else printf("%d\n",i);
}
}
return 0;
}
python语言的代码
count=0
i=100
while i<1001:
if i%5==0 and i%6==0:
count=count+1
if count%10!=0:
print(i,end=' ')
else:
print(i)
i=i+1
由于对python语法不是很熟,出现了逻辑与的错误,使用&运行的结果没有判断是否能被6整除,换成and运行结果才正确。且python的print()输出函数是自带换行的。