LearningAlgorithmWrite down some simple questions and do the same.
[Question]: 1-n this N number is randomly reduced by two numbers, placed in a [] (disrupt the order), the time complexity of the O (n) algorithm to find the number of reduced 2
[Idea]:If we reduce the number by 1, we all know that the method -- one loop, calculates the sum of a [] and (1 + 2 +... + n) = sum, and then use sum-sum to get the result. However, if there are two numbers, only the sum is not allowed. What can I do? You can use other operations to record more information! Do the four arithmetic operations have addition, subtraction, multiplication, division? Do not always remember +. Forget all other brothers!
# include "stdafx. H "# include
# include
using namespace STD; // The number of 1 --- N is randomly reduced by two and placed in, // find the algorithms with time complexity O (N). // This has a problem. When n reaches a certain level, sum and Mul must cross-border void findtwolostnum (int A [], int N, Int & num1, Int & num2); int _ tmain (INT argc, _ tchar * argv []) {int N; int num1 = 0; int num2 = 0; int A [100]; cout <"input the value of N (<100)"
N; cout <"input a []"
A [k];} findtwolostnum (A, N, num1, num2); cout <"the two lost numbers are: \ n "
[Note]: A better method is to replace the second key value sum2 with the annotation part, and then, according to the equations: x + y = sum, x * x + y * Y = sum2, you can find x * y accordingly. The subsequent steps are similar.