zoukankan      html  css  js  c++  java
  • 38:素数表

    38 素数表

    作者: 江宝钏时间限制: 1S章节: 函数

    问题描述 :

    从键盘输入m,n

    在屏幕上按每行10个的格式输出m~n之间的全部素数。

    请用函数判断一个数是否素数。

    输入说明 :

    两个整数m n

    输出说明 :

    [m,n]之间(包含m和n)的素数,每行10个,每个数后跟一个空格。

    输入范例 :
    2 33
    输出范例 :
    2 3 5 7 11 13 17 19 23 29
    31

    代码:

    #include <stdio.h>
    int main()
    {
    	int hash[10000] = { 0 };
    	int m, n;
    	scanf("%d%d", &n, &m);
    	if (n > m)
    	{
    		int temp = n;
    		n = m; m = temp;
    	}
    hash[0]=hash[1]=1;
    	for (int i = 2; i < 10000; i++)
    	{
    		if (hash[i] == 0)
    		{
    			for (int j = i + i; j < 10000; j = j + i)
    			{
    				hash[j] = 1;
    			}
    		}
    	}
    	int num = 0;
    	for (int i = n; i <= m; i++)
    	{
    		if (hash[i] == 0)
    		{
    			num++;
    			printf("%d ", i);
    			if (num == 10)
    			{
    				printf("
    ");
    				num = 0;
    			}
    		}
    	}
    printf("
    ");
    	return 0;
    }
    
    Yesterday is history,tomorrow ismystery,but today is a gift!That why it is called Present!
  • 相关阅读:
    定位小结
    定位知识点
    css属性书写顺序
    清除浮动及清除浮动的方法
    margin合并和塌陷问题
    css特性-层叠性,继承性,优先级
    css属性简写
    css布局之双飞翼布局
    css布局之圣杯布局
    css布局之等高布局
  • 原文地址:https://www.cnblogs.com/VictorierJwr/p/12408792.html
Copyright © 2011-2022 走看看