在做一道題目時,用不同的格式化字符讀入在不同的OJ上會有不同的結果。
poj_2689/ uva_10140 Prime Distance
poj題目鏈接:http://poj.org/problem?id=2689
題目:
Description
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
代碼:
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define range 50000
#define range2 1000010
#define INF 0xFFFFFFF
typedef long long LL;
int prime[range],is_prime[range];
int a[range2],prime2[range2];
int num_p;
void produce_prime()
{
memset(is_prime,0,sizeof(is_prime));
num_p=0;
for(int i=2;i<range;i++)
{
if(is_prime[i]==0)
{
int j=i+i;
prime[num_p++]=i;
while(j<range)
{
is_prime[j]=1;
j+=i;
}
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
LL left,r;
produce_prime();
'''
**// while(scanf("%d%d",&left,&r)==2)//這樣寫在poj上會TLE,在uva上能AC
while(scanf("%lld%lld",&left,&r)==2)//這樣寫在poj和uva上都能AC**
'''
{
memset(a,0,sizeof(a));
if(left==1)
{
a[0]=1;
}
for(int i=0;i<num_p && prime[i]*prime[i]<=r;i++)
{
int k=(int)left/prime[i];
LL m=k*prime[i];
while(m<left || k<=1)
{
m+=(LL)prime[i];
k++;
}
for(LL j=m;j<=r;j+=prime[i])
{
if(j>=left)
a[j-left]=1;
}
}
int minn=INF,maxx=-1;
int minflag=-1,minf=-1,minlast=-1,maxf=-1,maxlast=-1,pre=-1;
for(LL i=left;i<=r;i++)
{
if(a[i-left]==0)
{
if(pre==-1)
{
pre=i;
}
else
{
if(i-pre<minn)
{
minf=pre; minlast=i;
minn=i-pre;
}
if(i-pre>maxx)
{
maxf=pre; maxlast=i;
maxx=i-pre;
}
pre=i;
}
//last=i;
}
}
if(minf==-1)
printf("There are no adjacent primes.\n");
else
printf("%d,%d are closest, %d,%d are most distant.\n",minf,minlast,maxf,maxlast);
}
return 0;
}
博客讨论了在不同在线编程平台上使用scanf读取整数时,使用%d和%lld格式化字符串可能导致不同结果的问题。文章以poj_2689和uva_10140的Prime Distance题目为例,介绍了寻找相邻质数的算法,并给出了输入输出示例。
500

被折叠的 条评论
为什么被折叠?



