找到所覆盖的距离,以等距收集物品

正在进行一场比赛,在一条路上放置了几块石头。在比赛的起点放置一个桶,距离第一块石头5个单位。其他的石头彼此相隔3个单位,一个接一个地直线排列。也就是说,第一块和第二块石头之间的距离是3个单位,第三块和第四块石头之间的距离也是3个单位,以此类推。参赛者从桶开始,捡起最近的石头,返回并将其放入桶中,然后再次跑去收集下一个最近的石头,返回并将其放入桶中。这样,这个过程一直持续到所有的石头都被放入桶中。 现在,如果地面上有n块石头,那么在完成整个过程中,参赛者将覆盖多少距离。

null

图片[1]-找到所覆盖的距离,以等距收集物品-yiteyi-C++库

例如:

Input : n = 3Output : Distance = 48Explanation= 2*5 + 2(5 + 3) + 2(5 + 3 + 3)= 10 + 16 + 22= 48Input : n = 5Output : Distance = 110Explanation= 2*5 + 2(5 + 3) + 2(5 + 3 + 3) + 2(5 + 3 + 3 + 3) + 2(5 + 3 + 3 + 3 + 3)= 10 + 16 + 22 + 28 + 34= 110

观察模式:

选手选择第一块石头的距离=2*5 选手选择第二块石头的距离=2(5+3) 选手选择第三石的距离=2(5+3+3) = 2(5 + (2 * 3)) 选手选择第四块石头的距离=2(5+3+3+3) = 2(5 + (3 * 3)) 选手选择第五石的距离=2(5+3+3+3+3) = 2(5 + (4 * 3)) . . . 选手选择第n块石头的距离=2(5+3+3+…+(n-1)次) =2(5+(n-1)*3) 因此,参赛者的总跑步距离=上述所有距离的总和 = (2 * 5) + 2(5 + 3) + 2(5 + (2 * 3)) + 2(5 + (3 * 3)) + ………….. + 2(5+(n-1)*3) = 2(5 + (5 + 3) + (5 + (2 * 3)) + (5 + (3 * 3)) + ………………. + (5+(n-1)*3) =2(5+5+5……+n次)+(3+(2*3)+(3*3)+(n-1)*3) =2(5n+3(1+2+3+n-1)) =2(5n+3/2[(n-1)*(n-1+1)]) =2(5n+3/2[(n-1)*n]) =2(5n+3/2(n 2. –n) =10n+3*n 2. -3*n =3*n 2. +7*n = n*((3*n)+7)

以下是该方法的实施情况:

C++

// C++ program to calculate
// the distance for given problem
#include <bits/stdc++.h>
using namespace std;
// function to calculate the
// distance
int find_distance( int n)
{
return n * ((3 * n) + 7);
}
// Driver program
int main()
{
int n = 5;
cout << "Distance = " << find_distance(n);
return 0;
}


JAVA

// Java program to calculate the
// distance for given problem
class demo {
// function to calculate
// the distance
public static int find_distance( int n)
{
return n * ( 3 * n + 7 );
}
// Driver program
public static void main(String args[])
{
int n = 5 ;
System.out.print( "Distance = " );
System.out.println(find_distance(n));
}
}


Python3

# Python3 code to calculate
# the distance for given problem
# function to calculate the
# distance
def find_distance(n):
return n * (( 3 * n) + 7 )
# main function
n = 5
ans = find_distance( n )
print (ans)
# This code is contributed by Saloni Gupta


C#

// C# program to calculate
// the distance for given problem
using System;
class GFG {
// function to calculate the
// distance
public static int find_distance( int n)
{
return n * ((3 * n) + 7);
}
// Driver program
public static void Main()
{
int n = 5;
Console.Write(find_distance(n));
}
}
// This code is contributed by
// Smitha Dinesh Semwal


PHP

<?php
//PHP program to calculate
// the distance for given problem
// function to calculate the
// distance
function find_distance( $n )
{
return $n * ((3 * $n ) + 7);
}
// Driver program
$n = 5;
echo "Distance = " , find_distance( $n );
// This code is contributed by aj_36
?>


Javascript

<script>
// JavaScript program to calculate the
// distance for given problem
// function to calculate
// the distance
function find_distance(n)
{
return n * (3 * n + 7);
}
// Driver code
let n = 5;
document.write( "Distance = " );
document.write(find_distance(n));
</script>


输出:

110

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享