請判斷某數是否為質數 輸入說明 : 一個整數x, x= 2 且 x=2147483647
輸出說明:
質數 或 非質數
範例輸入:
13
14
範例輸出:
質數
非質數
程式碼:
#include<stdio.h>
#include<math.h>
int main()
{
int a,i,check,temp;
while(scanf(" %d",&a)==1)
{
check=1;
if(a%2==0 && a!=2)
check=0;
else
{
temp = sqrt(a)+1;
for(i=3;i<temp;i+=2)
{
if(a%i==0)
{
check=0;
break;
}
}
}
if(check)
printf("質數\n");
else
printf("非質數\n");
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=a007
沒有留言:
張貼留言