Offer prove safety study notes (C #) - A positive number and a consecutive sequence S

Title Description

Xiao Ming is very fond of mathematics, one day when he was doing math homework, required to calculate and 9 to 16, he immediately wrote the correct answer is 100. But he was not satisfied with this, he wondered how many kinds of continuous positive number sequence is 100 (including at least two numbers). Before long, he got another set of consecutive positive number and a sequence of 100: 18,19,20,21,22. Now the question to you, you can also quickly identify all positive and continuous sequence S? Good Luck!

A. Analysis of the topic

        The title has several points need to pay attention to:

        1. This list is continuous positive sequence, and a minimum of two numbers.

        2. Because of a characteristic, and so an array can be calculated using a formula. (Method 1 to 100 of that sum, namely: (1 + 100) * 100/2).

        Looks like the title, only two points to note, the next step is the core of the algorithm, three concepts: 1. The number in the array. 2. array practical and. 3. The array of targets and.

        When the array and actual = target and, in order to continue to look for the array can achieve its objectives and the need to have large numbers and decimals forward one;

        When the array and actual> target and, to move forward a decimal number in this array have less of a minimum number, in comparison.

        When the actual and <target and an array, the large numbers moved forward a number within the array so it adds a large number, then Bibi ah.

II. Code implementation

using System.Collections.Generic;
class Solution
{
    public List<List<int>> FindContinuousSequence(int sum)
    {
        // Write code here Wallpaper
         // define the final output array 
        List <List < int >> RET = new new List <List < int >> ();
         // definition of i = 1, j = 2, reasons: 
        int I = . 1 , J = 2 ;
         // method stop conditions: 1 as the final output array minimum number of 2, so that a small sum can not exceed half of the sum to more than the sum is greater than, and must be less than decimal large numbers. 
        the while (I <(+ SUM . 1 ) / 2 && I < J)
        {
            // Because consecutive numbers, it may be a summation formula (decimal large numbers +) * length / 2; (for example: add 1 to count how 100) 
            int tmpSum = (I + J) * (J - I + 1 ) / 2 ;
             // if the array is less than the target number, the need to make large numbers increased 
            IF (tmpSum < SUM)
            {
                j++;
            }
            // equal slightly outputs 
            the else  IF (tmpSum == SUM)
            {
                // define an array, the array length is the length between ij of 
                int [] = ary new new  int [J - I + . 1 ];
                 // execution cycle, derived from the array ij of 
                for ( int K = I, L = 0 ; K <= J; K ++, L ++ )
                {
                    ary[l] = k;
                }
                // this array is added to the definition of the list 
                ret.Add ( new new List < int > (ary));
                 // If there are other how to do it, so to perform ++ i ++ h and J 
                i ++ ;
                j++;
            }
            // if the array is greater than the target number, the decimal plus 1, so that the array number becomes less of it 
            else
            {
                i++;
            }
        }
        // return to the list 
        return RET;
    }
}

 

Guess you like

Origin www.cnblogs.com/WeiMLing/p/11079799.html