1) Maximum approximate problem:
For a given two positive integers, a, B, computes the number of approximately several numbers between a and a. For example: between a=1,b=36,1 and 36, the maximum number is 36, and the number is 9.
Idea: The common method is to cycle tests between A and B each number exactly how many approximate, and then one by one comparison
1 for the first time try:
int number=0;//number of approximate numbers
int flag=0;//Sets the number of comparisons, resets when the next number is greater than the number of previous digits
for (int i=a;i<=b;i++)//outer loop from A to B
{
for (int j=0;j<=i;j++)//inner loop detection from 1 to itself
Modified to:
for (int i=1;j<=i;j++)//programmers always think they won't make such a low-level mistake
Remind yourself of the dirty past in future life to meet the bright future.
{
if (i%j==0)
{
number++;
}
}
if (Number>flag)//Reset flag bit
{
Flag=number;
}
number=0;//just forgot the recovery count value is 0
}
Second attempt: In the time complexity, can shorten the operation time, through observation found that when the inner layer of the loop to run to its own half, until the last itself, only to find that there is an approximate existence, it is itself, for example: The number 100, when more than 50,51,52,53 .... Up to 99 no more than 100 of the approximate, we modify the above program to reduce the complexity of the operation, the number of cycles reduced by half, in the space floating point arithmetic above (in this, remind the reader I have limited level, not in-depth understanding of how the computer division operation, please forgive me)
So the program is modified as follows:
int number=0;//number of approximate numbers
int flag=0;//Sets the number of comparisons, resets when the next number is greater than the number of previous digits
for (int i=a;i<=b;i++)//outer loop from A to B
{
for (int j=1;j<=i/2;j++)//inner loop detection from 1 to itself
{
if (i%j==0)
{
number++;
}
}
number++;//itself is also an approximate, need to add in
if (Number>flag)//Reset flag bit
{
Flag=number;
}
number=0;//just forgot the recovery count value is 0
}
Now based on the thought above: 1, if this number can not be divisible by 2, the range will be shortened by half
2. If this number cannot be 3 correct, then the range will be smaller.
3. The discovery of a rule is to try to divide the number by the number in the primes and the range is even smaller.
Wait, we thought we had reached the limit, but a bigger problem came:
As long as it is an even number it will definitely have a larger approximate number than the odd number. Of course, we need to consider the range of A and B, the number of an even number of adjacent two numbers must not be less than the approximate number of odd number (except 1 and 2), such as: 12 and 13,80 and 81, so this condition is set, the cycle times will fall once again half.
Third attempt: So choose to skip the odd number of tests to directly compare the two even-numbered approximate numbers, the code is as follows:
Algorithm overview (unable to compile) result: Divisor is 0 (Result of debugging)