Zju2034 false coin-coin Problems

Source: Internet
Author: User

Description:

The old coin problem. There is a pile of coins numbered 1-n (n <100) in sequence, with only one weight slightly different. Next, K (k <100) calls the coin operation to give the results of each weighing, that is, the weight of A1, a2... am is greater than or less than B1, B2.... Bm number. Output coin numbers with different weights.

Analysis:

1. If the two heaps of coins are of the same weight, all coins are normal coins.Coins that never appear in the case of equal weight are suspected.

2. If some coins appear on a smaller or larger side each time, this coin is suspected;Otherwise, it must be normal.

3. Combine the above two points to find all the suspected coins. If the value is 0 or greater than 1, the coin cannot be found; otherwise, the coin is output.

In fact, the idea is very clear. You only need to infer from your life experience.

In the programOptimization. I usedBitwise operationThe question is coming soon in 0.01 s.

For example, I used a 10-element int array to record which coins have appeared in the case of equal weights. Each int stores the status of 10 coins. Initially, all a [I] is~ 0That is to say, each digit is 1, which means all of them are suspected. Then use a [X/10] & = ~ (1 <(X % 10) indicates that the coin number X is no longer suspected.The X % 10 position of a [X/10] is 0..

According to this idea, the other situation can also be solved ~

The code is not pasted ...... Ugly.

Contact Us

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.