输入两个整数X和Y,输出两者之间的素数个数(包括X和Y)。
#include<cstdio> #include<algorithm> using namespace std; bool f[100005]; int main() { int x, y, ans=0; scanf("%d%d", &x, &y); f[1] = 1; if(x == 1) x=2; if(x > y) swap(x, y); for(int i=2; i<=y; i++) { if(f[i] == 0) { for(int j=2; j*i<=y; j++) { f[j*i] = 1; } } } for(int i=x; i<=y; i++) { if(f[i] == 0) ans++; } printf("%d", ans); return 0; }
#没有考虑y可能比x大,还有时间复杂度。
一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。
#include<cstdio> #include<algorithm> using namespace std; int main() { int a, n, m, ma=32768; scanf("%d", &a); if(a%2 == 0) { for(int i=0; i<=a; i+=2) { n = i/2; if((a-i)%4 == 0) { m=(a-i)/4; ma = min(ma, n+m); } } printf("%d %d", ma, a/2); } else printf("0 0"); return 0; }
没考虑例外情况
原文:https://www.cnblogs.com/orange-233/p/11773487.html