这将是一般性的问题陈述:
囚犯通过跳过N堵墙而逃出监狱,每一道墙的高度都在一个数组中。他可以跳x米的高度,但每次跳后,他滑了y米,由于一些无法控制的因素(风,滑墙等)。
Similar problem statement mentioned here
给出的编程任务是调试一个包含四个参数的函数-
NoOfJumps(int x, int y, int N, int Height[])第一个测试用例是参数- (10,1,1,{10})
10米为他跳跃,1米他滑倒,墙数为1,墙高为10。现在:
effectiveJump =x= 9。
所以他得跳两次才能跳过墙。因此,这个函数应该返回2 (转义所需的跳转总数)。
参数还有另一个测试用例- (3,1,5,{20,5,12,11,3})
3跳米,1米滑倒,墙数5,墙高20米,5米,12米,11米,3m。现在:
effectiveJump =x= 2。
为提供了上述参数值的输出,为24.。
NoOfJumps(3, 1, 5, {20,5,12,11,3})
我不明白这个产值是怎么得到的。这些墙到底是怎么布置的?
我只能想出一种解决拐角处情况的方法,那就是当人跳过墙壁时,,。
(when (x) > remaining height of the wall),
他不应该滑下去,否则我无法得到所需的解决方案。例如,在第一堵墙的第二个测试用例中,当这个人身高18米时,他跳到3米到21米,当他越过那堵墙时,他不会滑倒。接下来,他从21岁开始跳,而不是20岁。跳的顺序是:
0->2->4->6->8->10->12->14->16->18->21->23->26->28->30->32->34->36->39->41->43->45->47->50->53
假设墙高20,25,37,48,51。
这是解决问题的正确假设吗?
发布于 2017-07-26 20:16:30
C对给定案例2的代码,将适用于案例1将参数更改为(10、1、1、10)。
#include<conio.h>
#include<stdio.h>
int jump(int x,int y,int n,int z[]);
int jump(int x,int y,int n,int z[])
{
int i, j, countjump, total = 0, extra = 0;
clrscr();
printf("\n%d\n", n);
for (i = 0; i < n; i++) {
printf("\n%d", z[i]);
}
printf("\n");
for (j = 0; j < n; j++) {
countjump = 1;
z[j] = z[j] + (extra) - x;
while (z[j] >= 0) {
z[j] = z[j] + y;
z[j] = z[j] - x;
countjump = countjump + 1;
if (z[j] < 0) {
extra = z[j];
}
}
total = (countjump + total);
}
return total;
}
void main()
{
int res, manjump = 3, slip = 1, nwalls = 5;
int wallheights[] = {20, 5, 12, 11, 3};
clrscr();
res = jump(manjump, slip, nwalls, wallheights);
printf("\n\ntotal jumps:%d", res);
getch();
}发布于 2017-04-03 10:49:48
你可以用这个。
示例代码
public static int calculateJumps(int X, int Y, int height[]) {
int tn=0,n;
for(int i=0; i<height.length; i++) {
if(height[i]<=X) {
tn+=1;
continue;
}
n=((height[i]-X)/(X-Y));
n+=height[i]-((X-Y)*n)==X?1:2;
tn+=n;
}
return tn;
}您只需要传递X、Y和Array,才能获得输出。
发布于 2017-04-15 02:10:38
尝尝这个
您不需要墙的数目,因为它等于数组的大小
public class Jump {
public static void main(String[] a) {
int jump = 3;
int slip = 1;
int[] hights = {20,5,12,11,3};
int count = 0;
for (int hight : hights) {
int temp = hight - jump;
if (temp >= 0) {
count = count + temp / (jump - slip)+1;
}
if (temp % (jump - slip) > 0) {
count++;
}
}
System.out.println(count);
}
}https://stackoverflow.com/questions/39388878
复制相似问题