[Bzoj1081] Super Gray code

Manually simulates a sample, it should be appreciated how the operation is substantially
the specific implementation, each of a record current should be +1 or -1, then the lowest operating position and back of +1 and -1 to all negated

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,b,a[105],flag[105];
 4 void write(){
 5     for(int i=1;i<=n;i++)
 6         if (a[i]<10)printf("%d",a[i]);
 7         else printf("%c",a[i]-10+'A');
 8     printf("\n");
 9 }
10 int main(){
11     scanf("%d%d",&n,&b);
12     for(int i=1;i<=n;i++)flag[i]=1;
13     while (1){
14         write();
15         int i=1;
16         for(;i<=n;i++)
17             if ((0<=flag[i]+a[i])&&(flag[i]+a[i]<b)){
18                 a[i]+=flag[i];
19                 for(int j=i-1;j;j--)flag[j]*=-1;
20                 break;
21             }
22         if (i>n)break;
23     }
24 }
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11811301.html