AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870

求n内的素数个数。

 /* ***********************************************
Author : zch
Created Time :2015/5/19 8:46:16
File Name :a.cpp
************************************************ */ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
const int maxn = ;
int prime[maxn];
bool is_prime[maxn]; int solve(int n) {
int p=;
for(int i=;i<=n;i++) is_prime[i]=true;
is_prime[]=is_prime[]=false;
for(int i=;i<=n;i++) {
if(is_prime[i])
{
prime[p++]=i;
for(int j=*i;j<=n;j+=i)
is_prime[j]=false;
}
}
return p;
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
while(~scanf("%d",&n)) {
printf("%d\n",solve(n));
}
return ;
}

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=30390

 /* ***********************************************
Author : zch
Created Time :2015/5/19 9:53:16
File Name :a.cpp
************************************************ */ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) {
return b==?a:gcd(b,a%b);
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
ll x,y;
while(~scanf("%lld%lld",&x,&y)) {
printf("%lld %lld\n",gcd(x,y),x/gcd(x,y)*y);
}
return ;
}
上一篇:12.C#yield return和yield break及实际应用小例(六章6.2-6.4)


下一篇:#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)