A line of miscellaneous questions to find page pages

Source: Internet
Author: User

1. Problem Description: The number of items per page in the Web shop is limited, and the total number of n (n > 0) Items is given, and only m (M > 0) Items are allowed on each page

Now ask for a line of expressions to calculate how many pages are needed to display all the items? Requirements: can only be expressed in one line,

And can only be used (parentheses, subtraction) operators, and others, such as comparison operators, can not be used.

2. At first I did not read the topic, thought that the study is (A? B:C) operation, so I started with the answer that I gave:

Return (M >= N)? 1: (((N-n/m*m) > 0) n/m + 1:n/m)

It turns out that's not the case ... The requirement is to use subtraction and parentheses to derive an expression to calculate the value.

3. Problem Analysis:

The idea of this problem is obviously this:

1). n/m figure out how many full pages are required (that is, each page shows M items)

2). Judge N%m is 0, if 0, means n divided by M can be done, the result is n/m + 0; if it is greater than 0, another page is displayed, that is, the result is n/m + 1;

  

So the point is to determine whether N m is greater than 0. Obviously the problem requires that the remainder operator (%) cannot be used, then we want to work out a method to calculate the residue.

The results are given directly according to the conditions of the subject: n% m = n-[n/m] * M. where [n/m] is the largest integer less than or equal to [n/m].

Because n,m are integers, they are written directly: n% m = n-n/m*m;

The remainder method has the following problem: If the remainder is 0, the result is n/m + 0, but if it is greater than 0, the result is n/m + 1, the remainder can be any integer less than M,

So how to use when the remainder is greater than 0 o'clock, it becomes + 1. That is, how much when the remainder does not know how many, can get 1, then how to get the remainder is added 0 or plus 1?

Method: observed that when the remainder is 0 o'clock, value = (remainder + m-1 = 0 + m-1 = m-1) < M, then value/m = 0;

When the remainder >=1, value = (remainder + m-1) >= m and value < M, so value/m = 1;

In this way, the remainder is added to the m-1 and then divided by M, if the remainder is 0, then (0 + m-1)/m = 0;

If the remainder is greater than or equal to 1, set to X (1 <= x < m) m <= (x + m-1) < m (x + m-1)/2 = 1;

That just satisfies the demand. So the expression that conforms to test instructions becomes:

    return (N/m + ((n-n/m*m) + (m-1))/m);

Conclusion: After the above analysis, this problem mainly needs to solve two aspects:

1) How to obtain the remainder without taking the rest operator: (n-[n/m] * m)

2) When the value of the remainder does not know, how to get the need to add 0 if you do not have to judge the statement, or add 1. : Value = ((n-n/m*m) + (m-1))/M

  

OK, solve the problem, you can sleep soundly, hehe.

Welcome to correct and pointing.

A line of miscellaneous questions to find page pages

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.