import java.io.*;
public class Solution{
public static void main(String[] args){
/*int[] a=new int[11];
int[] back=new int[11];
int[] m=new int[11];
int[] negm=new int[11];
int[] q=new int[11];*/
int[] a={1,1,1,1,1,1,1,1,1,1,1,1};
int[] back={1,1,1,1,1,1,1,1,1,1,1,1};
int[] m={0,0,0,0,0,0,0,0,1,1,0,1};
int[] negm={1,1,1,1,1,1,1,1,0,0,1,1};
int[] q={1,1,1,1,0,1,1,0,1,1,1,1};
String flag=new String("+");
boolean suc=true;
printArray(a);
System.out.print("\t");
printArray(q);
System.out.print("\t");
System.out.println("Initial");
for(int n=0;n<12;n++){
shift(a);
a[a.length-1]=q[0];
shift(q);
back=copy(a);
printArray(a);
System.out.print("\t");
printArray(q);
System.out.print("\t");
System.out.println("Shift");
if(a[0]==m[0]){
a=add(a,negm);
printArray(a);
System.out.print("\t\t\t");
System.out.println("-");
}
else{
a=add(a,m);
printArray(a);
System.out.print("\t\t\t");
System.out.println("+");
}
if(a[0]==back[0]) suc=true;
else suc=false;
if(suc||zero(a)) set(q,1);
if(!suc&&!zero(a)){
set(q,0);
a=copy(back);
printArray(a);
System.out.print("\t");
printArray(q);
System.out.print("\t");
System.out.println("Backup");
}
}
}
public static int[] add(int[] a,int[] b){
int c=0;
int index;
int[] result=new int[12];
for(index=a.length-1;index>-1;index--){
result[index]=(a[index]+b[index]+c)%2;
c=(a[index]+b[index]+c)/2;
//System.out.println(c);
}
return result;
}
public static void shift(int[] a){
int index;
for(index=0;index<a.length-1;index++){
a[index]=a[index+1];
}
a[index]=0;
}
public static void set(int[] q,int value){
if(q[q.length-1]!=value){
q[q.length-1]=value;
System.out.print("\t\t");
printArray(q);
System.out.print("\t");
System.out.println("Set "+value);
}
}
public static int[] copy(int[] source){
int[] aim=new int[source.length];
for(int index=0;index<source.length;index++)
aim[index]=source[index];
return aim;
}
public static boolean zero(int[] a){
boolean f=true;
for(int i=0;i<a.length;i++){
if(a[i]!=0) f=false;
}
return f;
}
public static void printArray(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]);
if((i+1)%4==0&&(i+1)!=arr.length)
System.out.print(',');
}
}
}
附答案:
1111,1111,1111 1111,0110,1111 Initial
1111,1111,1111 1110,1101,1110 Shift
0000,0000,1100 +
1111,1111,1111 1110,1101,1110 Backup
1111,1111,1111 1101,1011,1100 Shift
0000,0000,1100 +
1111,1111,1111 1101,1011,1100 Backup
1111,1111,1111 1011,0111,1000 Shift
0000,0000,1100 +
1111,1111,1111 1011,0111,1000 Backup
1111,1111,1111 0110,1111,0000 Shift
0000,0000,1100 +
1111,1111,1111 0110,1111,0000 Backup
1111,1111,1110 1101,1110,0000 Shift
0000,0000,1011 +
1111,1111,1110 1101,1110,0000 Backup
1111,1111,1101 1011,1100,0000 Shift
0000,0000,1010 +
1111,1111,1101 1011,1100,0000 Backup
1111,1111,1011 0111,1000,0000 Shift
0000,0000,1000 +
1111,1111,1011 0111,1000,0000 Backup
1111,1111,0110 1111,0000,0000 Shift
0000,0000,0011 +
1111,1111,0110 1111,0000,0000 Backup
1111,1110,1101 1110,0000,0000 Shift
1111,1111,1010 +
1110,0000,0001 Set 1
1111,1111,0101 1100,0000,0010 Shift
0000,0000,0010 +
1111,1111,0101 1100,0000,0010 Backup
1111,1110,1011 1000,0000,0100 Shift
1111,1111,1000 +
1000,0000,0101 Set 1
1111,1111,0001 0000,0000,1010 Shift
1111,1111,1110 +
0000,0000,1011 Set 1