to hop 2
thuat toan 1.3
#include<conio.h> #include<stdio.h> #include<math.h> int n,p[50]; int b(int m, int i) { if(m%2==0&&m>2) { if(i<m-2) return i; else if(i==m-1) return m-2; } if(m%2!=0||m==2) return m-1; }
void in() { for(int i=1;i<=n;i++) { printf("%d ",p[i]); } printf("
"); }
void thuattoan(int m) { int t,tg; if(m==1) {in();} else { for(int i=1;i<=m;i++) { thuattoan(m-1); if(i<m) { int t=b(m,i); tg=p[t]; p[t]=p[m]; p[m]=tg; } } } }
int main() { printf("nhap vao n="); scanf("%d",&n); for(int i=1;i<=n;i++) { p[i]=i; } thuattoan(n); getch(); return 0; }
//thuat toan 1.4
#include<conio.h> #include<stdio.h> #include<math.h> int n,p[50];
void khoi_tao() { for(int i=1;i<=n;i++) { p[i]=0; } } int thuattoan() { int i=n; while(p[i]==1) { p[i]=0; i--; } p[i]=1; return i; }
void in() { for(int i=1;i<=n;i++) { printf("%d ",p[i]); } printf("
"); }
int main() { printf("nhap vao n=");scanf("%d",&n); khoi_tao(); in(); while(thuattoan()){in();} getch(); return 0; }
thuat toan2.1
#include<conio.h> #include<stdio.h> #include<math.h> int n,p[50];
void khoi_tao() { for(int i=1;i<=n;i++) { p[i]=0; } }
int thuattoan() { int i=n; while(p[i]==1) { p[i]=0; i--; } p[i]=1; return i; }
void in() { for(int i=1;i<=n;i++) { printf("%d ",p[i]); } printf("
"); }
int main() { printf("nhap vao n=");scanf("%d",&n); khoi_tao(); in(); while(thuattoan()){in();} getch(); return 0; }
thuat toan2.3
#include<conio.h> #include<stdio.h> #include<math.h>
int n,p[50],b[50];
oid khoi_tao() { printf("nhap vao boi cua cac phan tu:"); for(int i=1;i<=n;i++) { scanf("%d",&b[i]); p[i]=0; } }
void in() { for(int i=1;i<=n;i++) { printf("%d ",p[i]); } printf("
"); }
int thuattoan() { int i=n; while(p[i]==b[i]&&i!=0) { p[i]=0; i--; } p[i]++; return i; }
int main() { printf("nhap vao n=");scanf("%d",&n); khoi_tao(); in(); while(thuattoan()){in();} getch(); return 0; }
thuat toan them bot lap
#include<conio.h> #include<stdio.h> #include<math.h>
int n,b[50]; void khoi_tao() { for(int i=1;i<=n;i++) { b[i]=0; } }
void in() { for(int i=1;i<=n;i++) { printf("%d ",b[i]); } printf("
"); }
int tim(int k) { int i=0; while(k%2==0) { i=i+1; k=k/2; } return i; }
void thuattoan() { int i=0,k=0; while(i<n) { in(); k=k+1; i=1+tim(k); //printf(" i= %d",i); b[i]=1-b[i]; } }
int main() { printf("nhap vao n=");scanf("%d",&n); khoi_tao(); thuattoan(); getch(); return 0; }
//cong 1
#include<conio.h> #include<stdio.h> #include<math.h>
int n; int t[50];
void doi(int r) { int i = 1; while (r!=0) { t[i] = r%2; r = r/2; i++; } }
int main() { int r, n; printf("
Nhap n: "); scanf("%d", &n); for(int i = 1; i<=n; i++) t[i]=0; r = 0; while(r < pow(2, n)) { doi(r); printf("
"); for(int i=n; i>=1; i--) { printf("%d ", t[i]); }
r ++; } getch(); }
//lap #include<conio.h> #include<stdio.h> #include<math.h> int n; int b[50];
int thaythe(int x) { int i = 0; while(x%2==0) { x = x/2; i++; } return i; }
void in() { for(int i =1; i<=n; i++) printf(" %d ", b[i]); printf("
"); }
void thuattoan() { int i; for(i =1; i<=n; i++) b[i]=0; int k = 0; i=1; while(i<=n) { in(); k++; i = thaythe(k) +1; b[i] = 1 - b[i]; }}
int main() { printf("
Nhap n: "); scanf("%d", &n); thuattoan(); getch(); }
Bạn đang đọc truyện trên: AzTruyen.Top