Test instructions
Now there are a lot of weights, their weight is w0,w1,w2,... Each one of them. Ask if these weights can be used to represent something with a weight of M.
Analysis:
The topic was divided into greedy algorithm, but I did not think of greedy solution.
This is the question I think, if a number m is only used w,w^1 ... to say, that this number m into the W system is certainly only the number 0 and 1, but now we require that the number n is required two different m subtraction obtained, Also the same one of these two m cannot appear at the same time 1. Now is the request to give a n can be two such a different m subtract to get.
We can use the W-binary numbers of N to make a step-by-step judgment, using whether or not to be borrow. See the code specifically.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <
Algorithm> using namespace std;
int main () {Long long w,m;
int a[100];
int Num,ans,flag;
while (scanf ("%lld%lld", &w,&m) ==2) {memset (a,0,sizeof (a));
num = 0;
while (m) {a[num++]=m%w;
M/=w;
}//for (int i = num-1; I >= 0; i--)//{//cout<<a[i]<< "";
}//cout<<endl;
cout<<w<<endl;
Ans = 0;
Flag = 0; for (int i = 0; i < num; i++) {if (ans==0)//not borrow {//cout<<a
[i]<<endl; if (a[i]==0| |
A[i]==1) {ans = 0;
} else if (a[i]== (w-1)) {ans = 1;
} else flag = 1; } else//was borrow {if (a[i]==0) {ans
= 0; } else if (a[i]==w-1| |
A[i]==w-2) {ans = 1;
} else flag = 1;
}//cout<<ans<< "" <<flag<<endl;
if (flag==1) break;
} if (flag==0) {printf ("yes\n");
} else printf ("no\n");
} return 0;
}