📌  相关文章
📜  从给定的线段长度中查找中点线段

📅  最后修改于: 2021-04-26 05:09:05             🧑  作者: Mango

给定大小为M的数组arr [] 。该数组表示不同大小的段长度。这些段划分以0开头的行。arr [0]的值表示从0 arr [0]开始的段,arr [1]的值表示从arr [0]到arr [1]的段,依此类推。
任务是找到包含中间点的线段。如果中间线段不存在,则打印“ -1”。
例子:

方法:中间点将始终为N /2。现在,检查该点在哪个段中存在并打印段号。如果它是任何段的开始或结束,则打印“ -1”。
下面是上述方法的实现:

C++
// C/C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns the segment for the
// middle point
 int findSegment(int n, int m, int segment_length[])
    {
 
        // the middle point
        double meet_point = (1.0 * n) / 2.0;
        int sum = 0;
 
        // stores the segment index
        int segment_number = 0;
 
        for (int i = 0; i < m; i++) {
 
            // increment sum by
            // length of the segment
            sum += segment_length[i];
 
            // if the middle is
            // in between two segments
            if ((double)sum == meet_point) {
                segment_number = -1;
                break;
            }
 
            // if sum is greater
            // than middle point
            if (sum > meet_point) {
                segment_number = i + 1;
                break;
            }
        }
 
        return segment_number;
    }
 
    // Driver code
int main() {
        int n = 13;
        int m = 3;
        int segment_length[] = { 3, 2, 8 };
 
        int ans = findSegment(n, m, segment_length);
        cout<<(ans);
 
     
     
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function that returns the segment for the
    // middle point
    static int findSegment(int n, int m, int[] segment_length)
    {
 
        // the middle point
        double meet_point = (1.0 * n) / 2.0;
        int sum = 0;
 
        // stores the segment index
        int segment_number = 0;
 
        for (int i = 0; i < m; i++) {
 
            // increment sum by
            // length of the segment
            sum += segment_length[i];
 
            // if the middle is
            // in between two segments
            if ((double)sum == meet_point) {
                segment_number = -1;
                break;
            }
 
            // if sum is greater
            // than middle point
            if (sum > meet_point) {
                segment_number = i + 1;
                break;
            }
        }
 
        return segment_number;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 13;
        int m = 3;
        int[] segment_length = new int[] { 3, 2, 8 };
 
        int ans = findSegment(n, m, segment_length);
        System.out.println(ans);
    }
}


Python3
# Python 3 implementation of the approach
 
# Function that returns the segment for the
# middle point
def findSegment(n, m, segment_length):
        # the middle point
        meet_point = (1.0 * n) / 2.0
        sum = 0
 
        # stores the segment index
        segment_number = 0
 
        for i in range(0,m,1):
            # increment sum by
            # length of the segment
            sum += segment_length[i]
 
            # if the middle is
            # in between two segments
            if (sum == meet_point):
                segment_number = -1
                break
             
            # if sum is greater
            # than middle point
            if (sum > meet_point):
                segment_number = i + 1
                break
 
        return segment_number
 
# Driver code
if __name__ == '__main__':
        n = 13
        m = 3
        segment_length = [3, 2, 8]
 
        ans = findSegment(n, m, segment_length)
        print(ans)
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
class GFG
{
 
// Function that returns the
// segment for the middle point
static int findSegment(int n, int m,
                       int[] segment_length)
{
 
    // the middle point
    double meet_point = (1.0 * n) / 2.0;
    int sum = 0;
 
    // stores the segment index
    int segment_number = 0;
 
    for (int i = 0; i < m; i++)
    {
 
        // increment sum by
        // length of the segment
        sum += segment_length[i];
 
        // if the middle is
        // in between two segments
        if ((double)sum == meet_point)
        {
            segment_number = -1;
            break;
        }
 
        // if sum is greater
        // than middle point
        if (sum > meet_point)
        {
            segment_number = i + 1;
            break;
        }
    }
 
    return segment_number;
}
 
// Driver code
public static void Main()
{
    int n = 13;
    int m = 3;
    int[] segment_length = new int[] { 3, 2, 8 };
 
    int ans = findSegment(n, m, segment_length);
    Console.WriteLine(ans);
}
}
 
// This code is contributed
// by shs


PHP
 $meet_point)
        {
            $segment_number = $i + 1;
            break;
        }
    }
 
    return $segment_number;
}
 
// Driver code
$n = 13;
$m = 3;
$segment_length = array( 3, 2, 8 );
 
$ans = findSegment($n, $m,
                   $segment_length);
echo ($ans);
     
// This code is contributed by ajit
?>


Javascript


输出:
3