A.最大的数
筛质数。
#include <cstdio> #include <iostream> #include <algorithm> #define N 20001 int prime[N]; bool not_prime[N]; int n, cnt, ans, num; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } inline void init() { int i, j; for(i = 2; i < N; i++) if(!not_prime[i]) { prime[++cnt] = i; for(j = i + i; j < N; j += i) not_prime[j] = 1; } } int main() { int i, j, a, x; init(); n = read(); for(i = 1; i <= n; i++) { a = read(); x = std::upper_bound(prime + 1, prime + cnt + 1, a) - prime; for(j = x - 1; j >= 1; j--) if(a % prime[j] == 0) { if(prime[j] > num || (prime[j] == num && a > ans)) { num = prime[j]; ans = a; } break; } } printf("%d\n", ans); return 0; }
B.小红数列
找找规律。
#include <cstdio> #include <iostream> int n; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } int main() { n = read(); printf("%d\n", n / 3 * 2 + (n % 3 == 2)); return 0; }
时间: 2024-10-03 23:04:20