Topic One: Problem Description:
Enter an incremented array and a number s, and look for two numbers in the array so that their and exactly is S, and if there are multiple pairs of numbers and s, output any pair. Problem Analysis:
Method 1: A simple and crude approach.
Implementation: Starting from the first number of arrays, and then judging the remaining number and the number of the and is not s, this can solve the problem. Time complexity is O (n*n).
Method 2: The top method solves the problem, but it does not take advantage of the most important condition of the topic-the array is ascending. With this most important condition, the solution of the problem will not be as complicated as it is.
Specific means of implementation:
1.head and tail point to the first and last elements of the array, respectively.
2. Calculates the tail of the element that the head points to.
Head < tail
3. If equal to S, output these two numbers, the program exits.
4. If greater than s,tail move forward.
5. Move backward if less than s,head. Code implementation:
#include <iostream>
using namespace std;
and two numbers for S,
bool Findnumswithsum (int arr[], int len, int sum, int& num1, int& num2)
{
bool found = Fals e;
if (len <= 1 | | | arr = NULL) return
found;
int head = 0;
int tail = len-1;
while (Head < tail)
{
if (Arr[head] + Arr[tail] < sum)
++head;
else if (Arr[head] + arr[tail] > sum)
--tail;
else//found and for sum of two numbers
{
num1 = Arr[head];
num2 = Arr[tail];
Found = true;
break;
}
}
return found;
}
int main ()
{
int arr[] = {1, 2, 4, 7, One,};
int num1 = 0;
int num2 = 0;
int sum = 0;
cin >> sum;
BOOL ret = findnumswithsum (arr, sizeof (arr)/sizeof (arr[0)), Sum, NUM1, num2);
if (ret)
cout<<num1 << "" << num2<<endl;
else
cout<< "not found" <<endl;
System ("pause");
return 0;
}
Topic Two: Question description:
Enter a positive number s to print out all consecutive positive numbers for s (at least two digits), such as input 15, as 1+2+3+4+5 = 4+5+6=7+8, so output 3 consecutive sequences: 1~5,4~6,7~8. Problem Analysis:
If a topic can be made out, the topic of the second is also easier to do (the idea is the same). Assuming that the input S is 15, we define the variables small and big, the initial values are 1, 2, and the cursum for the current sequential sequence. 1+2 < 15, so we make big for 3,1+2+3<15,big++,big = 4,1+2+3+4 = Ten < 15,big++,big = 5,1+2+3+4+5 = 15, output sequence. 1+2+3+4+5+6 > 15,cursum-=small,small++, Adjustment. Until the end of the search and the sequence of S.
It is noteworthy that the small must be less than (1+s)/2, for what?
In fact, if S is an odd,small< (1+s)/2, if S is even, the number of consecutive two numbers is not even, so if s is even, the sequence is at least 3 digits, and the small code implements:
#include <iostream> using namespace std;//and consecutive positive number sequence void printnums (int small, int big) {for s (int i = Small ; I <= big;
++i) {cout<<i<< "";
} cout<<endl; } void findcontinuouspositive (const int& sum) {if (sum <= 2) {cout<< "Cannot find a sequential series of positive numbers" <<endl
;
Return
int small = 1;
int big = 2;
int mid = (1 + sum)/2;
int cursum = small + big;
while (small < mid) {if (cursum = sum) {printnums (small,big);
while (Cursum < sum && Small < mid) {++big;
Cursum + = big;
if (cursum = = sum) {printnums (small,big);
}//cursum > Sum cursum-= small;
++small;
int main () {int sum = 0;
CIN >> sum;
Findcontinuouspositive (sum);
System ("pause");
return 0; }