首页 > 代码库 > UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)


两个暴力题。。


题目传送:11827 Maximum GCD


AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int a[105];

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	int T;
	scanf("%d", &T);
	getchar();
	while(T --) {
		int cnt = 0;
		char s[100005];
		gets(s);
		int len = strlen(s);
		for(int i = 0; i < len; i ++) {
			if(s[i] != ‘ ‘) {
				int tmp = 0; 
				for(; isdigit(s[i]); i ++) {
					tmp = tmp * 10 + s[i] - ‘0‘;
				}
				a[cnt ++] = tmp;
			}
		}
		
		int ans = 0;
		for(int i = 0; i < cnt; i ++) {
			for(int j = i + 1; j < cnt; j ++) {
				ans = max(ans, gcd(a[i], a[j]));
			}
		}
		
		printf("%d\n", ans);
	}
	return 0;
}





题目传送:10200 Prime Time


AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int biao[10005];

bool isPrime(int x) {
	if(x == 2 || x == 3) return true;
	int m = (int)sqrt(x + 0.5);
	for(int i = 2; i <= m; i ++) {
		if(x % i == 0) return false;
	}
	return true;
}

void init() {
	for(int i = 0; i < 40; i ++) {
		biao[i] = 1;
	}
	for(int i = 40; i < 10005; i ++) {
		int t = i * i + i + 41;
		if(isPrime(t)) {
			biao[i] = 1;
		}
		else biao[i] = 0;
	}
}

int main() {
	init();
	int a, b;
	while(scanf("%d %d", &a, &b) != EOF) {
		int cnt = 0;
		for(int i = a; i <= b; i ++) {
			cnt += biao[i];
		}
		printf("%.2lf\n", (cnt * 1.0) / (b - a + 1) * 100.0 + 1e-6);//注意浮点数误差 
	}
	return 0;
}














UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)