素數 UVA 406
Prime Cuts
大致題意
從1-1000裡找出所有素數。1-N裡有多少個素數(包括1與N),C是1-N內的素數中從中間那一個到兩邊類似“半徑”。具體看題意第一段
A prime number is a counting number (1, 2, 3, . . .) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the 2C prime numbers from the center of the list if there are an even number of prime numbers or 2C − 1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.
Input
Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 ≤ N ≤ 1000)
is the maximum number in the complete list of prime numbers between 1 and N. The second number
(1 ≤ C ≤ N) defines the 2C prime numbers to be printed from the center of the list if the length of
the list is even; or the 2C − 1 numbers to be printed from the center of the list if the length of the list
is odd.
Output
For each input set, you should print the number N beginning in column 1 followed by a space, then by
the number C, then by a colon (?, and then by the center numbers from the list of prime numbers as
defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1
and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from
the center of the list should be preceded by exactly one blank. Each line of output should be followed
by a blank line. Hence, your output should follow the exact format shown in the sample output.
Sample Input
21 2
18 2
18 18
100 7
Sample Output
21 2: 5 7 11
18 2: 3 5 7 11
18 18: 1 2 3 5 7 11 13 17
100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
#include <stdio.h>
#include <stdlib.h>
#include"string.h"
#include"math.h"
int findprimary(int a[]);
int main()
{
int a[1100]= {1,2,3},i,indexmax,c,limit,limitpos,cposL,cposR,mpos;
indexmax=findprimary(a);
while(scanf("%d %d",&limit,&c)==2)
{
if(limit>=997){limitpos=indexmax;}//N<=1000,而最大素數為997
else
{
for(i=0; i<=indexmax; i++)
{
if(a[i]>limit)
{
limitpos=i-1;
break;
}
}
}
if((limitpos+1) % 2 == 1)
{
if( (2*c-1) >=(limitpos+1))
{
printf("%d %d:",limit,c);
for(i=0; i<=limitpos; i++)
{
printf(" %d",a[i]);
}
}
else
{
mpos=limitpos/2;
cposL=mpos-(c-1);
cposR=mpos+(c-1);
printf("%d %d:",limit,c);
for(i=cposL; i<=cposR; i++)
{
printf(" %d",a[i]);
}
}
}
if((limitpos+1) % 2 == 0)
{
if( (2*c) >=(limitpos+1))
{
printf("%d %d:",limit,c);
for(i=0; i<=limitpos; i++)
{
printf(" %d",a[i]);
}
}
else
{
mpos=limitpos/2;
cposL=mpos-(c-1);
cposR=mpos+1+(c-1);
printf("%d %d:",limit,c);
for(i=cposL; i<=cposR; i++)
{
printf(" %d",a[i]);
}
}
}
printf("\n\n");
}
return 0;
}
int findprimary(int a[])
{
int i,t,j,k=3;
for(i=5; i<=1000; i=i+2)
{
t=sqrt(i);
for(j=3; j<=t; j++)
{
if(i % j ==0)
{
break;
}
}
if(j==t+1)
{
a[k++]=i;
}
}
return k-1;
}
注意:1000內的素數最大為997,也就是確保998 999 1000也要有輸出。
相關文章
- PAT-B 1013 數素數 【素數】
- 素數
- GBJ406-ASEMI無人機專用GBJ406無人機
- leetcode周賽 - 406LeetCode
- PAT-B 1007 素數對猜想【素數】
- 素數篩
- 數論(1):素數
- 判斷素數
- 素數判斷
- 最強素數
- 【數論】素數篩法
- UVA1587
- 美素數 hd 4548
- 素數判定 hd 2012
- 計算素數【Java】Java
- UVa11054 Gergovia的酒交易(數學歸納法)Go
- UVA11624-Fire!
- solution-uva1594
- PAT1013數素數C++C++
- 如何高效尋找素數
- 密碼工程-大素數密碼
- 第六章 數學問題 ----------6.13 素數的篩法(第十萬零二個素數)
- 初等數論——素數,逆元,EXGCD有關GC
- [PAT B] 1013 數素數 -未完成
- Uva232 Crossword AnswersROS
- uva11292-Dragon of LoowaterGo
- 紫書UVa1592
- 紫書UVa297
- UVA11624 Fire!【BFS】
- 分拆素數和 hd 2098
- [PAT B] 1007 素數對猜想
- 素數判定(int sushu(int n))
- [演算法]: 素數篩法演算法
- 1354: 素數判定(C語言)C語言
- 如何修復http代理406錯誤呢?HTTP
- C語言判斷素數,判斷質素演算法C語言演算法
- 素數個數 <埃式篩 && 尤拉篩>
- 1013 數素數 (20分)/c++實現C++