C/C++練習7---求某個範圍內的所有素數 (sdut oj)

SwordsMan98發表於2017-01-25


C/C++練習7---求某個範圍內的所有素數

Time Limit: 1000MS Memory Limit: 65536KB



Problem Description

求小於n的所有素數,按照每行10個顯示出來。


Input

輸入整數n(n<10000)。


Output

每行10個依次輸出n以內的所有素數。如果一行有10個素數,每個素數後面都有一個空格,包括每行最後一個素數。


Example Input

100


Example Output

2 3 5 7 11 13 17 19 23 29 
31 37 41 43 47 53 59 61 67 71 
73 79 83 89 97 

Hint

Author








參考程式碼



#include <stdio.h>
int main()
{
    int n;
    int i;
    int temp;
    int a = 0;
    scanf("%d",&n);
    for(i = 2; i <= n; i++)
    {
        if(i == 2)
        {
            printf("%d ",i);
            a++;
        }
        else
        {
            for(temp = 2; temp < i; temp++)
            {
                if(i % temp == 0)
                {
                    break;
                }
            }
            if(i == temp)
            {
                printf("%d ",i);
                a++;
                if(a%10==0)
                    printf("\n");
            }
        }

    }
    return 0;
}


相關文章