Function 囚犯墙跳跃程序中的测试用例说明
这将是一般问题陈述: 一名囚犯从监狱越狱,他跳过N堵墙,每堵墙的高度以阵列形式给出。他可以跳x米的高度,但每次跳完后,由于一些无法控制的因素(风、滑墙等),他都会滑倒y米 给出的编程任务是调试包含四个参数的函数-Function 囚犯墙跳跃程序中的测试用例说明,function,debugging,integer-arithmetic,Function,Debugging,Integer Arithmetic,这将是一般问题陈述: 一名囚犯从监狱越狱,他跳过N堵墙,每堵墙的高度以阵列形式给出。他可以跳x米的高度,但每次跳完后,由于一些无法控制的因素(风、滑墙等),他都会滑倒y米 给出的编程任务是调试包含四个参数的函数- NoOfJumps(int x, int y, int N, int Height[]) 他跳的米数 他从墙上滑下的米数 墙的数量 作为阵列的墙的高度 第一个测试用例用于参数-(10,1,1,{10}) 他跳了10米,滑了1米,墙的数目是1,墙的高度是10。现在: 有效跳跃=x-y
NoOfJumps(int x, int y, int N, int Height[])
NoOfJumps(3,1,5,{20,5,12,11,3})
我不明白这个输出值是怎么得到的。墙是怎么布置的
我只能想到一种解决拐角问题的方法,即当人跳过墙时
(当(x)>墙的剩余高度时
)
他不应该滑倒,否则我无法获得所需的解决方案。
例如,在第一堵墙的第二个测试案例中,当人处于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
这是解决问题的正确假设吗?您可以使用此假设
示例代码
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;
}
publicstaticintcalculatejumps(intx,inty,intheight[]){
int tn=0,n;
对于(inti=0;i试试这个
您不需要墙的数量,因为它等于阵列的大小
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);
}
}
请尝试此代码。可能未进行优化
$input1=跳转高度
$input2=Slipage
$input=墙高度数组
function GetJumpCount($input1,$input2,$input3)
{
$jumps = 0;
$wallsCrossed = 0;
while($wallsCrossed != count($input3)){
$jumps++;
$input3[$wallsCrossed] = $input3[$wallsCrossed] - $input1;
if($input3[$wallsCrossed] > 0){
$input3[$wallsCrossed] = $input3[$wallsCrossed] + $input2;
}else{
$wallsCrossed++;
}
}
return $jumps;
}
墙一个接一个地出现。跳过墙一个后,位置应该从零开始,而不是从最后一个跳跃高度开始。对于第一种情况,输出应该是1,因为高度和跳跃是相同的。在第二种测试情况中,24是正确的输出。
我在techgig竞赛上看到了完全相同的问题。对于第一个测试用例,输出应该是1。测试用例已经自己解释过,如果跳跃和高度相同,就不会出现滑动。检查这是否解决了您的问题
def GetJumpCount(jump, slips, walls):
"""
@jump:int, Height of 1 jump
@slips:int, height of slip
@walls:array, height of walls
"""
jumps = []
for wall_height in walls:
wall_jump = 1
wall_height -= jump
while wall_height > 0:
wall_height += slips
wall_height -= jump
wall_jump += 1
jumps.append(wall_jump)
return sum(jumps)
逻辑在这里,请检查这是否解决了您的问题
package puzeels;
public class Jump
{
int jump=6;
int slip=1;
int numberOfWals=4;
int height[] ={21,16,10,5};
static int count=0;
int wallheight=0;
private int findJump()
{
for(int i=0;i<height.length;i++)
{
wallheight=height[i];
while((wallheight>0))
{
count=count+1;
wallheight=wallheight-(jump-slip);
System.out.println(wallheight+" "+count);
}
System.out.println("Out of while loop");
}
return count;
}
public static void main(String arr[])
{
Jump obj = new Jump();
int countOfJumps=obj.findJump();
System.out.println("number of jumps is==> "+countOfJumps);
}
}
package puzeels;
公共类跳转
{
int-jump=6;
int滑移=1;
int numberOfWals=4;
整数高度[]={21,16,10,5};
静态整数计数=0;
内墙高度=0;
私有int findJump()
{
对于(int i=0;i0))
{
计数=计数+1;
墙高=墙高-(跳滑);
系统输出打印项次(墙高+“”+计数);
}
System.out.println(“离开while循环”);
}
返回计数;
}
公共静态void main(字符串arr[]
{
跳转obj=新跳转();
int countOfJumps=obj.findJump();
System.out.println(“跳转次数==>”+countOfJumps);
}
}
C给定案例2上的代码将适用于案例1中更改
参数设置为(10,1,1,10)
#包括
#包括
整数跳跃(整数x,整数y,整数n,整数z[]);
整数跳转(整数x,整数y,整数n,整数z[])
{
int i,j,countjump,总计=0,额外=0;
clrsc();
printf(“\n%d\n”,n);
对于(i=0;i=0){
z[j]=z[j]+y;
z[j]=z[j]-x;
countjump=countjump+1;
if(z[j]<0){
额外=z[j];
}
}
总计=(countjump+总计);
}
返回总数;
}
void main()
{
整数分辨率,人跳=3,滑倒=1,无球=5;
int wallheights[]={20,5,12,11,3};
clrsc();
res=跳跃(人跳、滑倒、N墙、墙高);
printf(“\n\n总跳数:%d”,res);
getch();
}
我认为12是一个错误的答案,因为我尝试了这个代码,得到了11,最后一次跳转没有失误:
public static void main(String [] args) {
int T;
int jcapacity, jslip, nwalls;
//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
jcapacity = sc.nextInt();
jslip = sc.nextInt();
nwalls = sc.nextInt();
int [] wallHeightArr = new int [nwalls];
for (int i = 0; i< nwalls; i++) {
wallHeightArr[i] = sc.nextInt();
}
sc.close();
while(T-->0) {
int distance = log(jcapacity,jslip,wallHeightArr);
System.out.println(distance);
}
}
private static int log(int jcapacity, int jslip, int[] wallHeightArr) {
// TODO Auto-generated method stub
int distance = 0;
for(int i = 0; i< wallHeightArr.length; i++) {
int cHeight = 0;
int count = 0;
while (wallHeightArr[i] - cHeight > jcapacity) {
cHeight += (jcapacity - jslip);
count++;
}
count++;
distance += count;
}
return distance;
}
publicstaticvoidmain(字符串[]args){
int T;
国际jcapacity、jslip、nwalls;
//BufferedReader bf=新的BufferedReader(新的InputStreamReader(System.in));
扫描仪sc=新的扫描仪(System.in);
T=sc.nextInt();
jcapacity=sc.nextInt();
jslip=sc.nextInt();
nwalls=sc.nextInt();
int[]wallHeightArr=新的int[nwalls];
for(int i=0;i0){
int距离=对数(jcapacity、jslip、wallHeightArr);
系统输出打印LN(距离);
}
}
私有静态int日志(int jcapacity、int jslip、int[]wallHeightArr){
//TODO自动生成的方法存根
整数距离=0;
对于(int i=0;ijcapacity){
cHeight+=(jcapacity-jslip);
计数++;
}
计数++;
距离+=计数;
}
返回距离;
}
您是否有
public static void main(String [] args) {
int T;
int jcapacity, jslip, nwalls;
//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
jcapacity = sc.nextInt();
jslip = sc.nextInt();
nwalls = sc.nextInt();
int [] wallHeightArr = new int [nwalls];
for (int i = 0; i< nwalls; i++) {
wallHeightArr[i] = sc.nextInt();
}
sc.close();
while(T-->0) {
int distance = log(jcapacity,jslip,wallHeightArr);
System.out.println(distance);
}
}
private static int log(int jcapacity, int jslip, int[] wallHeightArr) {
// TODO Auto-generated method stub
int distance = 0;
for(int i = 0; i< wallHeightArr.length; i++) {
int cHeight = 0;
int count = 0;
while (wallHeightArr[i] - cHeight > jcapacity) {
cHeight += (jcapacity - jslip);
count++;
}
count++;
distance += count;
}
return distance;
}