讀入scanf,%d和%lld問題
来源:互联网 发布:mac os x 10.12镜像 编辑:程序博客网 时间:2024/05/15 17:12
在做一道題目時,用不同的格式化字符讀入在不同的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 0xFFFFFFFtypedef 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問題
- %d, %ld, %lld 区别
- C++的%d、%s、%lld、%
- lld和I64d
- printf格式符%d、%lld、%llx、%u
- %I64d 和%lld 的区别
- %I64d 和%lld 的区别
- while(~scanf("%d %d",&a,&b))和while(scanf("%d %d",&a,&b)!=EOF)
- while(~scanf("%d %d",&a,&b))和while(scanf("%d %d",&a,&b)!=EOF)
- scanf("%*d");
- while(scanf("%d",&n),n)和while(scanf("%d",&n)!=EOF)和while(scanf("%d",&n)==1)和while(~scanf("%d",&n)
- zabbix利用脚本和lld监控web
- 关于%I64d和%lld的问题
- Linuxx下printf格式符%d、%lld、%llx、%u等
- printf 和scanf的%*s, %*d的作用
- scanf("%d",&choose); getchar();
- C++的输出入cin/cout和scanf/printf谁比较快?
- C语言中printf("lld")和printf("I64d")
- memcached 打开以及telnet连接
- 开启新世界,助力新的自己
- 【Unity技巧】四元数(Quaternion)和旋转
- winpcap安装使用教程
- 数据结构和算法经典100题-第20题
- 讀入scanf,%d和%lld問題
- Java中的泛型(1)
- oracle授权另外一个用户访问自己创建的数据对象
- myeclipse中的jdk版本问题
- comet的那些事儿
- Free_modbus源码的解析和modbus协议的一些理解
- 如何将wpa_supplicant ssid字串转换为utf8
- Java基础——继承接口抽象类
- 输入一个整数,判断这个整数各个位数之和是否为奇数