Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ljj's level1 #26

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
40 changes: 40 additions & 0 deletions level1/p02_isPrime/p02_isPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <cmath>
#include <cstdio>
#define ll long long
using namespace std;
const int maxn=100000,N1=maxn+5;

int pr[N1],cnt;
bool vis[N1];
void get_prime()
{
for(int i=2;i<=maxn;i++)
{
if(!vis[i]) pr[++cnt]=i;
for(int j=1;j<=cnt&&i*pr[j]<=maxn;j++)
{
vis[i*pr[j]]=1;
if(i%pr[j]==0) break;
}
}
}
int check(ll x)
{
ll m=sqrt(x);
for(int j=1;pr[j]<=m&&j<=cnt;j++)
if(x%pr[j]==0) return 0;
return 1;
}

ll n;

int main()
{
scanf("%lld",&n);
if(n<=1){ puts("bad input"); return 0; }
get_prime();
int flag=check(n);
if(!flag) puts("NO"); else puts("YES");
return 0;
}

16 changes: 16 additions & 0 deletions level1/p03_Diophantus/p03_Diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <cmath>
#include <cstdio>
#define dd double
#define ll long long
using namespace std;
const int maxn=100000,N1=maxn+5;


int main()
{
//(1/6+1/12+1/7)x+5+1/2x+4=x
dd x=(5.0+4.0)/(1.0-1.0/2-1.0/6-1.0/7-1.0/12);
printf("%.0lf\n",x);
return 0;
}

28 changes: 28 additions & 0 deletions level1/p04_ narcissus/p04_narcissus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cmath>
#include <cstdio>
#define dd double
#define ll long long
using namespace std;
const int maxn=100000,N1=maxn+5;

int a[2];
void check(int x)
{
int tx=x,tot=0;
for(int i=0;i<=2;i++)
{
a[i]=tx%10, tx/=10;
tot+=a[i]*a[i]*a[i];
}
if(tot==x) printf("%d\n",x);
}

int main()
{
for(int i=100;i<=999;i++)
{
check(i);
}
return 0;
}

35 changes: 35 additions & 0 deletions level1/p05_allPrimes/p05_allPrimes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cmath>
#include <ctime>
#include <cstdio>
#define dd double
#define ll long long
using namespace std;
const int maxn=1000,N1=maxn+5;

clock_t st,ed;
int pr[N1],cnt;
bool vis[N1];
void get_prime()
{
for(int i=2;i<=maxn;i++)
{
if(!vis[i]) pr[++cnt]=i, printf("%d ",i);
for(int j=1;j<=cnt&&i*pr[j]<=maxn;j++)
{
vis[i*pr[j]]=1;
if(i%pr[j]==0) break;
}
}
puts("");
}

int main()
{
st=clock();
get_prime();
ed=clock();
printf("%.7lfs\n",(double)(ed-st)/CLOCKS_PER_SEC);
//Time complexity:O(n)
return 0;
}

41 changes: 41 additions & 0 deletions level1/p06_Goldbach/p06_Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cmath>
#include <ctime>
#include <cstdio>
#define dd double
#define ll long long
using namespace std;
const int maxn=100,N1=maxn+5;

clock_t st,ed;
int pr[N1],cnt;
bool vis[N1];
void get_prime()
{
for(int i=2;i<=maxn;i++)
{
if(!vis[i]) pr[++cnt]=i; //printf("%d ",i);
for(int j=1;j<=cnt&&i*pr[j]<=maxn;j++)
{
vis[i*pr[j]]=1;
if(i%pr[j]==0) break;
}
}
// puts("");
}
void check(int x)
{
for(int i=2;i+2<=x;i++) if(!vis[i]&&!vis[x-i])
{ printf("%d:%d %d\n",x,i,x-i); return; }
printf("%d:failed\n",x);
}

int main()
{
// Goldbach conjecture:
// every even number greater than 2 is able to be represented as the sum of two primes
get_prime();
for(int i=4;i<=maxn;i+=2) check(i);
// Time complexity : O(n^2)
return 0;
}

68 changes: 68 additions & 0 deletions level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#define dd double
#define ll long long
using namespace std;
const int maxn=100,N1=maxn+5,base=63;

// base64:
char encx(int x)
{
x=x&base;
if(0<=x&&x<=25) return x+'A';
if(26<=x&&x<=51) return x-26+'a';
if(52<=x&&x<=61) return x-52+'0';
if(x==62) return '+'; else return '/';
}
int decx(char x)
{
if('A'<=x&&x<='Z') return x-'A';
if('a'<=x&&x<='z') return x-'a'+26;
if('0'<=x&&x<='9') return x-'0'+52;
if(x=='+') return 62; else return 63;
}
int encrypt(char *s,char *enc,int len)
{
int n=0,tmp,i;
for(i=0;i<len;i+=3)
{
tmp=(*(int*)(s+i))&0xffffff;
enc[n++]=encx(tmp); tmp>>=6;
enc[n++]=encx(tmp); tmp>>=6;
enc[n++]=encx(tmp); tmp>>=6;
enc[n++]=encx(tmp);
}
if(len%3==1) enc[n-1]='=', enc[n-2]='=';
else if(len%3==2) enc[n-1]='=';
return n;
}
void decrypt(char *enc,char *s)
{
int tmp,y,len=strlen(enc);
if(enc[len-1]=='=') enc[len-1]='A'; if(enc[len-2]=='=') enc[len-2]='A';
for(int i=0,j;i<len;i+=4)
{
j=i/4; tmp=0;
y=decx(enc[i+3]); tmp|=y; tmp<<=6;
y=decx(enc[i+2]); tmp|=y; tmp<<=6;
y=decx(enc[i+1]); tmp|=y; tmp<<=6;
y=decx(enc[i]); tmp|=y;
*(int*)(s+j*3)=tmp;
}
}

char origin[N1],ency[N1],decy[N1];

int main()
{
freopen("a.in","r",stdin);
scanf("%s",origin); int len=strlen(origin);
int lene=encrypt(origin,ency,len);
for(int i=0;i<lene;i++) printf("%c",ency[i]); puts("");
decrypt(ency,decy);
for(int i=0;i<lene;i++) printf("%c",decy[i]); puts("");
return 0;
}

39 changes: 39 additions & 0 deletions level1/p08_hanoi/p08_hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define dd double
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn=100,N1=maxn+5,base=63;

int n;
int tot=0;
void mov(int dep,int st,int via,int ed)
{
if(dep>1) mov(dep-1,st,ed,via);
printf("step %d: %d %d -> %d\n",++tot,dep,st,ed);
if(dep>1) mov(dep-1,via,st,ed);
}

int main()
{
freopen("a.out","w",stdout);
scanf("%d",&n);
int A=1,B=2,C=3;
mov(n,A,B,C);
return 0;
}


// ull f[N1];

// 题解:
// 1. 考虑递推(或归纳), 设A柱子为起始柱,B柱子为辅助柱,C柱子为目标柱
// 假设我们已经求解出A柱子上有i个圆盘时的答案,现在需要求解有i+1个圆盘时的答案;
// 第i+1个圆盘在移动前必须保证如下条件:A中最上面的圆盘是i+1,B应该放置1~i的所有圆盘,C是空柱子
// 然后再把B当做起始柱,A当做辅助柱,把放置在B上的1~i的所有圆盘依次挪到C
// 可得递推式:f[i]=f[i-1]*2+1, f[1]=1
// n=64可以用递推轻松求解(注意要使用unsigned long long)
Loading