如何获得锁定组合程序的输出?我的计划有什么问题? [英] How do I get the output for lock combination program? What is wrong in my program?

查看:95
本文介绍了如何获得锁定组合程序的输出?我的计划有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个-slot组合锁,其中每个插槽都包含一个编号,其中包含10个连续十进制整数,在0到9的包含范围内。在一个操作中,您可以选择一个插槽并单击旋转拨号,正方向(增加显示的数字)或负方向(减少显示的数字)。请注意,由于表盘的循环特性,9之后的下一个数字为0,0之前的数字为9)。例如,如果表盘上当前显示数字0,您可以在一次操作中将转盘旋转到1(正方向)或9(负方向)。

给定初始配置每个插槽中的数字和一些所需的数字配置,确定将锁的插槽更改为所需配置必须执行的最小操作次数。



  import  java.io. *; 
import java.util。*;
import java.text。*;
import java.math。*;
import java.util.regex。*;

public class lock {

public static void main( String [] args){

Scanner s = new Scanner(System.in);
int n,m,sum = 0;
int n1 [] = new int [ 5 ];
int n2 [] = new int [ 5 ];
for int i = 0; i< 5; i ++)
n1 [I] = s.nextInt();
for int j = 0; j< 5; j ++)
n2 [J] = s.nextInt();
for int k = 0; k <5; k ++)
{
N = Math.abs(N2 [K] -N1 [K]);
m = Math.abs(( 9 -n2 [k])+(n1 [k] +1));
if (n< = m)
sum = sum + n;
else
sum = sum + m;
n = 0; m = 0;
}
System.out.println(sum);
}
}





我的尝试:



这个公式给出了输出13,正确的输出是9.

 n = Math.abs(n2 [K] -N1 [K]); 
m = Math.abs(( 9 -n2 [k])+(n1 [k] +1));

解决方案

尝试使用

  for  int  k = 0; k <5; k ++)
{
n = Math.abs (N 2 [K] -N1 [K]);
m = 10 - n;
if (n< = m)
sum = sum + n;
else
sum = sum + m;
n = 0; m = 0;
}





Quote:

这也有效,但我无法理解我的代码出错的地方。谢谢你的帮助。



你的代码出错是因为m错了。

因为一个轮子是10个位置,而 n 是一种方式的点击次数,另一种方式是 10-n 和你的 m 不是这样做的,因为 n2 [k] -n1 [k] 是正面的还是负面的。



< blockquote class =quote>

引用:

我的n2 [k] -n1 [k]总是正数,因为我使用了Math.abs,如果你检查公式,它就完美了适合。



n2 [k] -n1 [k] 是正数,因为嵌入 abs() ,查看 m ,它嵌入在 abs()中的其他内容,问题是 abs()中的其他内容。

说其他人,

 m =  10  -Math.abs(n2 [k] -n1 [k]); 
//
m = Math.abs(( 9 -n2 [K])+(N1 [K] +1));
//
m = Math.abs( 10 - (n2 [k] -n1 [k]));



是两个不同的东西


Consider a -slot combination lock where each slot contains a dial numbered with the ten sequential decimal integers in the inclusive range from 0 to 9. In one operation, you can choose a slot and rotate the dial by one click, either in the positive direction (to increase the displayed number by ) or the negative direction (to decrease the displayed number by ). Note that, due to the cyclical nature of the dial, the next number after 9 is 0 and the number before 0 is 9). For example, if the number 0 is currently displayed on the dial, you can rotate the dial to either 1(positive direction) or 9 (negative direction) in a single operation.
Given the initial configuration of numbers in each slot and some desired configuration of numbers, determine the minimum number of operations you must perform to change the lock's slots to the desired configuration.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class lock {

    public static void main(String[] args) {
     
        Scanner s=new Scanner(System.in);
        int n,m,sum=0;
        int n1[]=new int[5];
        int n2[]=new int[5];
        for(int i=0;i<5;i++)
            n1[i]=s.nextInt();
        for(int j=0;j<5;j++)
            n2[j]=s.nextInt();
        for(int k=0;k<5;k++)
        {
            n=Math.abs(n2[k]-n1[k]);
            m=Math.abs((9-n2[k])+(n1[k]+1));
            if(n<=m)
                sum=sum+n;
            else
                sum=sum+m;
            n=0;m=0;
        }
        System.out.println(sum);
    }
}



What I have tried:

This formula,which gives me the output 13,the correct output is 9.

n=Math.abs(n2[k]-n1[k]);
m=Math.abs((9-n2[k])+(n1[k]+1));

解决方案

Try with

for(int k=0;k<5;k++)
{
    n=Math.abs(n2[k]-n1[k]);
    m= 10- n;
    if(n<=m)
        sum=sum+n;
    else
        sum=sum+m;
    n=0;m=0;
}



Quote:

This works too,but I cannot understand where is my code going wrong. Thanks for the help.


Your code is going wrong because m is wrong.
Since a wheel is 10 positions, and n is the number of clicks one way, the other way is 10-n and your m is not doing that because n2[k]-n1[k] is positive or negative.

Quote:

My n2[k]-n1[k] is always positive as I have used Math.abs and if you check the formula,it perfectly fits.


n2[k]-n1[k] is positive because embedded in abs(), look at m, it is embedded in abs() with something else, the problem is the something else that is in the abs().
Said otherwize,

m=10-Math.abs(n2[k]-n1[k]);
// and
m=Math.abs((9-n2[k])+(n1[k]+1));
// which is
m=Math.abs(10-(n2[k]-n1[k]));


are 2 different things


这篇关于如何获得锁定组合程序的输出?我的计划有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆