diff --git a/level1/p02_isPrime/p02_isPrime.cpp b/level1/p02_isPrime/p02_isPrime.cpp new file mode 100644 index 00000000..953e3f93 --- /dev/null +++ b/level1/p02_isPrime/p02_isPrime.cpp @@ -0,0 +1,40 @@ +#include +#include +#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; +} + diff --git a/level1/p03_Diophantus/p03_Diophantus.cpp b/level1/p03_Diophantus/p03_Diophantus.cpp new file mode 100644 index 00000000..95f4ae68 --- /dev/null +++ b/level1/p03_Diophantus/p03_Diophantus.cpp @@ -0,0 +1,16 @@ +#include +#include +#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; +} + diff --git a/level1/p04_ narcissus/p04_narcissus.cpp b/level1/p04_ narcissus/p04_narcissus.cpp new file mode 100644 index 00000000..9ebe8c46 --- /dev/null +++ b/level1/p04_ narcissus/p04_narcissus.cpp @@ -0,0 +1,28 @@ +#include +#include +#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; +} + diff --git a/level1/p05_allPrimes/p05_allPrimes.cpp b/level1/p05_allPrimes/p05_allPrimes.cpp new file mode 100644 index 00000000..666a1f49 --- /dev/null +++ b/level1/p05_allPrimes/p05_allPrimes.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#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; +} + diff --git a/level1/p06_Goldbach/p06_Goldbach.cpp b/level1/p06_Goldbach/p06_Goldbach.cpp new file mode 100644 index 00000000..034175c2 --- /dev/null +++ b/level1/p06_Goldbach/p06_Goldbach.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#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; +} + diff --git a/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp b/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp new file mode 100644 index 00000000..9cc1960b --- /dev/null +++ b/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#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>=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 +#include +#include +#include +#include +#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) diff --git a/level1/p09_maze/p09_maze.cpp b/level1/p09_maze/p09_maze.cpp new file mode 100644 index 00000000..b770806a --- /dev/null +++ b/level1/p09_maze/p09_maze.cpp @@ -0,0 +1,256 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define dd double +#define ll long long +#define ull unsigned long long +using namespace std; + +// // 生成迷宫:生成树算法 +// // 我的做法:随机边权然后最小生成树 +int n,m,cte; +struct EDGE{ +int x,y,w; +friend bool operator < (const EDGE &s1,const EDGE &s2) +{ + return s1.w=1 && x=1 && y=1 && x+fx[to]=1 && y+fy[to] +#include +#include +#include +using namespace std; + + +char str[]="sto NamelessOIer orz"; + +int main() +{ + int now=0,len=20,type=1; + while(1) + { + system("cls"); + for(int i=1;i<=now;i++) printf(" "); + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#define FG FOREGROUND_GREEN +#define FR FOREGROUND_RED +#define FB FOREGROUND_BLUE +#define FI FOREGROUND_INTENSITY +#define dd double +#define ll long long +#define ull unsigned long long +using namespace std; + +int n,m,Score,End,step; +HANDLE output[2]; +COORD coord = {0,0}; +DWORD bytes = 0; +int checkx(int x,int y) +{ + if(x>=1 && x=1 && y=1 && x=1 && y +#include +#include +#include +#define dd double +#define ll long long +#define ull unsigned long long +// #include "p11_linkedList.h" +using namespace std; +const int N1=105; + +int n, tot; +struct node{ +int val; node *nxt; +}; +node *head; + +int get_command(); +node* createnode(int x); +void releasenode(node* now); +node* insert(int pos,int x); +void del(); +void DEL(int pos); +void print(int type); +node* inverse(); +int find_first(int w); +int find_nxt(int pos,int w); + +void ins() +{ + system("cls"); + print(0); + puts("Please input new point's position and its value."); + int pos, w; + scanf("%d %d",&pos,&w); + insert(pos,w); +} +void del() +{ + system("cls"); + print(0); + puts("Please input the position where you want to delete."); + int pos; + scanf("%d",&pos); + DEL(pos); +} +void abnor() +{ + puts(""); + puts("Press [0] to continue..."); + int to; + while(1) + { + to=get_command(); + if(to==0) break; + } +} +int find1() +{ + system("cls"); + puts("Please input the value you want to find."); + int pos,w; scanf("%d",&w); + pos = find_first(w); + printf("%d",pos); + abnor(); + return pos; +} +int now_w; +int findn(int st) +{ + system("cls"); + puts("Please input the value you want to find."); + int pos,w; + if(st==-1){ + pos=-1; puts("Please redirect the start position by press [5] in your next command."); + // if(now_w == w) pos=-1; else { scanf("%d",&w); pos = find_nxt(st,w); now_w = w; } + }else{ scanf("%d",&w); pos = find_nxt(st,w); now_w = w; } + printf("%d",pos); + abnor(); + return pos; +} + +int main() +{ + // freopen("a.in","r",stdin); + printf("Plz input the size of your linkedlist:"); + scanf("%d",&n); + puts("Plz input the elements in order in your linkedlist:"); + puts("The last element will be linked with the first element."); + head = new node ((node){0,NULL}); + for(int i=0;i1) now = nxt; + } +} +node* createnode(int x) +{ + node *buf = new node ((node){x,NULL}); + return buf; +} +node* insert(int pos,int x) +{ + node* now=head; + if((*now).nxt==NULL){ (*now).val=x; (*now).nxt=now; tot++; return now; } //原链表没有元素 + pos--; + for(;pos;now=(*now).nxt,pos--); + node *nxt = (*now).nxt, *cur = createnode(x); + (*now).nxt = cur; (*cur).nxt = nxt; + tot++; + return cur; +} +void DEL(int pos) +{ + node* now=head; + if(pos==1) pos=tot; else pos--; + for(;pos>1;now=(*now).nxt,pos--); + node *cur = (*now).nxt; + (*now).nxt = (*cur).nxt; + delete cur; + tot--; +} +void print(int type) +{ + node *now; now=head; int cnt=tot; + for(;cnt;cnt--,now=(*now).nxt) + { + printf("%d ",(*now).val); + } + if(type==0) puts(""); + if(type==1) abnor(); +} +node* inverse() +{ + node *cur = createnode((*head).val), *now, *tmp, *nxt, *st; + (*cur).nxt = cur; + now = head; st = cur; + int cnt = tot; + for(;cnt;cnt--,now=(*now).nxt) + { + nxt = (*now).nxt; + if(cnt!=1) tmp = createnode((*nxt).val); else tmp = st; + (*tmp).nxt = cur; + cur = tmp; + } + releasenode(head); + head = cur; + print(1); + return cur; +} +int find_first(int w) +{ + node* now = head; int cnt=1; + for(;cnt<=tot;cnt++,now=(*now).nxt) + { + if((*now).val==w) break; + } + if(cnt==tot+1) return -1; + return cnt; +} +int find_nxt(int pos,int w) //pos之后的第一个w值 +{ + node* now = head; int cnt=pos; + for(;cnt;now=(*now).nxt,cnt--); cnt=pos+1; + for(;cnt<=tot;cnt++,now=(*now).nxt) + { + if((*now).val==w) break; + } + if(cnt==tot+1) return -1; + return cnt; +} diff --git a/p12_warehouse.cpp b/p12_warehouse.cpp new file mode 100644 index 00000000..7129c06b --- /dev/null +++ b/p12_warehouse.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#define dd double +#define ll long long +#define ull unsigned long long +using namespace std; +const int N1=10005; + +int n,m; +struct Ware{ +char name[40]; +int len,num; +}a[N1]; + +// 1. 实现如下的菜单(按数字选择菜单功能): +// - 显示存货列表 // 1 +// - 入库 // 2 +// - 出库 // 3 +// - 退出程序 // 0 +// 1. 实现菜单对应功能(需记录货物的型号、数量等信息); +// 1. 程序启动时从文件中读取当前库存数据,退出时保存库存数据; + +int get_command() +{ + int input=getch(),to; + switch(input) + { + case '1': to=1; break; + case '2': to=2; break; + case '3': to=3; break; + case '0': to=4; break; + default : to=-1; break; + } + return to; +} + +int idx(char c){ + if(c=='-') return 0; if(c=='.') return 1; + if('A'<=c&&c<='Z') return c-'A'+2; + if('a'<=c&&c<='z') return c-'a'+28; + if('0'<=c&&c<='9') return c-'0'+55; +} //' '之后的 +int id[N1]; +struct Trie{ +int ch[N1*40][64],id[N1],tot; +int findx(char *s,int len) +{ + int x=0,c; + for(int i=0;i