# 51Nod 1499 binary conversion problem.

Source: Internet
Author: User
Tags printf

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;
}```

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.