原文:

给定一个由正整数 n 和整数 k 组成的数组 arr[] 。假设你从位置 0 开始,你可以从arr【0】开始通过a【i】位置向左或向右移动。任务是打印移动方向,通过向右或向左移动,您可以完成 n 步,而不超过 [-k, k] 边界。如果不能执行步骤,打印 -1 。如果有多个答案,打印任意一个。 举例:

输入: arr[] = {40,50,60,40},k = 120 输出: 右 右 左 右 说明: 由于 n = 4(数组中的元素个数) 我们需要从 arr[0]开始进行 4 次移动,使得 值不会超出[-120, 120] 移动 1:位置= 0 40 = 40 移动 2:位置= 40 50 = 90 移动 3:位置= 90–60 = 30 移动 4:位置= 30 50 = 80 输入: arr[] = {40,50,60,40},k = 20 输出: -1

方法:可以按照以下步骤解决上述问题:

  • 开始时将位置初始化为 0。
  • 开始遍历所有的数组元素,
    • 如果 a[i] 位置没有超过左右边界,那么移动将是“右”
    • 如果位置-a[i]没有超过左右边界,那么移动将是“左”
  • 如果在任何阶段两个条件都失败,则打印 -1

以下是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// function to print steps such that
// they do not cross the boundary
void printsteps(int a[], int n, int k)
{
    // to store the resultant string
    string res = "";
    // initially at zero-th position
    int position = 0;
    int steps = 1;
    // iterate for every i-th move
    for (int i = 0; i < n; i  ) {
        // check for right move condition
        if (position   a[i] <= k
            && position   a[i] >= (-k)) {
            position  = a[i];
            res  = "right\n";
        }
        // check for left move condition
        else if (position - a[i] >= -k
                 && position - a[i] <= k) {
            position -= a[i];
            res  = "left\n";
        }
        // no move is possible
        else {
            cout << -1;
            return;
        }
    }
    // print the steps
    cout << res;
}
// driver code
int main()
{
    int a[] = { 40, 50, 60, 40 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 120;
    printsteps(a, n, k);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
class gfg
{
// function to print steps such that
// they do not cross the boundary
static void printsteps(int []a, int n, int k)
{
    // to store the resultant string
    string res = "";
    // initially at zero-th position
    int position = 0;
    //int steps = 1;
    // iterate for every i-th move
    for (int i = 0; i < n; i  )
    {
        // check for right move condition
        if (position   a[i] <= k
            && position   a[i] >= (-k))
        {
            position  = a[i];
            res  = "right\n";
        }
        // check for left move condition
        else if (position - a[i] >= -k
                && position - a[i] <= k)
        {
            position -= a[i];
            res  = "left\n";
        }
        // no move is possible
        else
        {
            system.out.println(-1);
            return;
        }
    }
    // print the steps
    system.out.println(res);
}
// driver code
public static void main (string[] args)
{
    int []a = { 40, 50, 60, 40 };
    int n = a.length;
    int k = 120;
    printsteps(a, n, k);
}
}
// this code is contributed by mits

python 3

# python3 implementation of the approach
# function to print steps such that
# they do not cross the boundary
def printsteps(a, n, k):
    # to store the resultant string
    res = ""
    # initially at zero-th position
    position = 0
    steps = 1
    # iterate for every i-th move
    for i in range(n):
        # check for right move condition
        if (position   a[i] <= k and
            position   a[i] >= -k):
            position  = a[i]
            res  = "right\n"
        # check for left move condition
        elif (position-a[i] >= -k and
              position-a[i] <= k):
            position -= a[i]
            res  = "left\n"
        # no move is possible
        else:
            print(-1)
            return
    print(res)
# driver code
a = [40, 50, 60, 40]
n = len(a)
k = 120
printsteps(a, n, k)
# this code is contributed by shrikant13

c

// c# implementation of the approach
using system;
class gfg
{
// function to print steps such that
// they do not cross the boundary
static void printsteps(int []a, int n, int k)
{
    // to store the resultant string
    string res = "";
    // initially at zero-th position
    int position = 0;
    //int steps = 1;
    // iterate for every i-th move
    for (int i = 0; i < n; i  )
    {
        // check for right move condition
        if (position   a[i] <= k
            && position   a[i] >= (-k))
        {
            position  = a[i];
            res  = "right\n";
        }
        // check for left move condition
        else if (position - a[i] >= -k
                && position - a[i] <= k)
        {
            position -= a[i];
            res  = "left\n";
        }
        // no move is possible
        else
        {
            console.writeline(-1);
            return;
        }
    }
    // print the steps
    console.write(res);
}
// driver code
static void main()
{
    int []a = { 40, 50, 60, 40 };
    int n = a.length;
    int k = 120;
    printsteps(a, n, k);
}
}
// this code is contributed by mits

服务器端编程语言(professional hypertext preprocessor 的缩写)

= (-$k))
            {
            $position  = $a[$i];
            $res .= "right\n";
        }
        // check for left move condition
        else if ($position - $a[$i] >= -$k &&
                 $position - $a[$i] <= $k)
        {
            $position -= $a[$i];
            $res .= "left\n";
        }
        // no move is possible
        else
        {
            echo -1;
            return;
        }
    }
    // print the steps
    echo $res;
}
// driver code
$a = array( 40, 50, 60, 40 );
$n = count($a);
$k = 120;
printsteps($a, $n, $k);
// this code is contributed by mits
?>

java 描述语言


output: 

right
right
left
right