View Code of Problem 120

#include<stdio.h>
#include<math.h>
//二刷 120th 
int IsPrime(int x){
	if(x<2) return 0;
	int bound = sqrt(x);
	for(int i=2; i<=bound; i++){ //i是从2开始的! 
		if(x % i == 0){
			return 0;
		} 
	}
	return 1;
} 

int main(){
	int h1, m1, h2, m2; 
	while(scanf("%d:%d", &h1, &m1) != EOF) { //有许多组测试数据
		scanf("%d:%d", &h2, &m2);
		
		int start = h1*60 + m1; //思路:把时间全部化成分钟,然后for循环判断比较
		int end = h2*60 + m2;
		int sum = 0, cnt = 0;
		for(int i=start; i<=end; i++){
			int h = i/60;
			int m = i%60; 
			sum = h*2500 + m;
			if(IsPrime(sum)){
				cnt++;
			}
		}
		printf("%d\n", cnt); 
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 120