使用函数进行模块化设计,可以简化程序的逻辑结构,并达到代码重用的效果。
简单的数列总是具有一些奇妙的性质,W教授正在研究这样一个数列A,它是以递增顺序排列的,并且其中所有的数的质因子只有可能是2,3和5。
请你编写程序输出这个数列中前N个数字。
14属于这个数列吗?显然不属于,因为14包含质因子7
如果输入6,输出结果就是:2 3 4 5 6 8。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> int is_qms(int n) { while (n%2==0) n = n/2; while (n%3==0) n = n/3; while (n%5==0) n = n/5; if (n==1) return 1; return 0; } int main(int argc, char *argv[]) { int i, n, total=0; scanf("%d", &n); for(i=2; ; i++) { if (is_qms(i)) { printf("%d\n", i); total++; if (total==n) break; // 当找到n个数后,就跳出循环 } } return 0; } |