首页 \ 问答 \ 组合锁码(Combination lock code)

组合锁码(Combination lock code)

我正试图解决旧的组合锁程序。 问题在下面。 我写了一些代码,但到目前为止,它只打印出一串总是包含第一个数字的可能组合。我真的非常困惑,要把它放在哪里才能到达需要的地方。

“想象一下,你需要打开一个标准的组合拨号锁,但不知道这个组合,并且没有一对螺栓刀。在BlueJ中用一种打印所有可能的组合的方法写一个Java程序,以便你可以在在你尝试的时候检查每一个,假设每个拨号盘上的数字范围从0到36,并且需要依次打开三个数字来打开锁,假设锁不是很好,与每个数字中的正确数字相距不超过两位的数字也将起作用,换句话说,如果组合是17-6-32,则18-5-31,19-4-32,15-8-33和许多其他组合也会打开锁,编写另一种方法,打印出你需要保证打开锁的最小组合列表。“

     /**
     * A method that prints all possible combinations of the lock.
     */
    public void combination(int combo)
    {
        int a;
        int b;
        int c;
        a = 0;
        while (a <= 36)
                {b = 0;
                while (b <= 36)
                    {c = 0;
                    while (c <= 36)
                    {
                        System.out.println(a + " " + b + " " + c);
                        c = c + 1;
                    }
                    b = b + 1;
                }
                a = a + 1;
                }
    }
}

I'm trying to solve the old combination lock program. The question is below. I've wrote some code, but so far it only prints a bunch of possible combinations that always contain the first number having 36. I'm honestly super confused as to where to get it to where it needs to be.

"Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a Java program in BlueJ with a method that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on each dial range from zero to thirty-six and three numbers in sequence are needed to open the lock. Suppose the lock isn't a very good one and any number that's no more than two away from the correct number in each digit will also work. In other words if the combination is 17-6-32, then 18-5-31, 19-4-32, 15-8-33 and many other combinations will also open the lock. Write another method that prints out a minimal list of combinations you would need to try to guarantee opening the lock. "

     /**
     * A method that prints all possible combinations of the lock.
     */
    public void combination(int combo)
    {
        int a;
        int b;
        int c;
        a = 0;
        while (a <= 36)
                {b = 0;
                while (b <= 36)
                    {c = 0;
                    while (c <= 36)
                    {
                        System.out.println(a + " " + b + " " + c);
                        c = c + 1;
                    }
                    b = b + 1;
                }
                a = a + 1;
                }
    }
}

原文:https://stackoverflow.com/questions/49955152
更新时间:2023-12-03 11:12

最满意答案

我从https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/getcwd.3.html找到了解决Xcode中getwcd的方法。 该解决方案类似于@Cyber​​建议的解决方案。 我必须在头文件中进行以下更改;

//#include <direct.h> // for _getcwd WIN
#include <unistd.h> // for getcwd Mac

并使用

getcwd()

代替

_getcwd() 

在我的功能

void GetCurrentPath(char* buffer){getcwd(buffer,PATH_MAX);}

然后在我的函数main()中使用

//Curent path
    char current_path[PATH_MAX]; //CurrentPath[_MAX_PATH]; windows
    GetCurrentPath(current_path);
    cout << current_path << endl;

通过查看头文件解决了我的问题的第二部分。 我曾使用Xcode创建空头文件hbv.h,然后我错误地粘贴了我在较旧的hbv.h文件中的代码,我在VC ++中使用#endif而不是#endif

    #ifndef hbvOptimMAC_hbv_h
    #define hbvOptimMAC_hbv_h
    //<code defining classes was correctly pasted here>
    #endif
    //<code defining classes initially mistakenly pasted here>

I found a way out for dealing with getwcd in Xcode from https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/getcwd.3.html. The solution is similar to that one suggested by @Cyber. I had to do the following changes in my header file;

//#include <direct.h> // for _getcwd WIN
#include <unistd.h> // for getcwd Mac

and use

getcwd()

instead of

_getcwd() 

in my function

void GetCurrentPath(char* buffer){getcwd(buffer,PATH_MAX);}

and then in my function main() I used

//Curent path
    char current_path[PATH_MAX]; //CurrentPath[_MAX_PATH]; windows
    GetCurrentPath(current_path);
    cout << current_path << endl;

The second part of my question was solved by looking at the header files. I had used Xcode to create the empty header file hbv.h and then I had mistakenly pasted the code from my older hbv.h file which I used in VC++ below the #endif instead of above #endif

    #ifndef hbvOptimMAC_hbv_h
    #define hbvOptimMAC_hbv_h
    //<code defining classes was correctly pasted here>
    #endif
    //<code defining classes initially mistakenly pasted here>

相关问答

更多
  • 去这个文件夹,其中包含黑暗的一面: Xcode 4.2或/Developer/Library/PrivateFrameworks/IDEKit.framework/Resources : /Developer/Library/PrivateFrameworks/IDEKit.framework/Resources Xcode 4.3:我不知道,也许是/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources 。 在这个时刻我没 ...
  • 您在序列点之间编写和读取变量a ,因此结果是正式未定义的行为 。 查看特定编译器生成的汇编代码将清楚地说明为什么会得到特定结果,但标准根本不做任何保证。 You're both writing and reading variable a between sequence points, so the result is formally undefined behavior. Looking at the assembly code generated by your particular compile ...
  • 我从https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/getcwd.3.html找到了解决Xcode中getwcd的方法。 该解决方案类似于@Cyber建议的解决方案。 我必须在头文件中进行以下更改; //#include // for _getcwd WIN #include // for getcwd Mac 并使用 getcwd() 代 ...
  • 您正在堆上动态分配Message ,然后取消引用它以通过引用传递它。 push_back的重载之一是push_back(const T& value) ,因此首先在堆上分配值然后取消引用并将其引用传递给函数。 现在这没用了,因为你可以直接用Message(messageID)分配它并将它传递给方法。 此外,由于没有与new关联的delete因此您将生成泄漏,并且由于未将返回的指针绑定到任何位置,因此无法释放它。 另外直接传递Message(messageID)并且可能使用emplace_back而不是pus ...
  • 我不能说出所有重要的事情,但我可以列举一个: 封装 。 结构和类之间唯一的C ++技术差异是默认访问。 在一个结构中,默认情况下一切都是公共的; 在一堂课中,一切都是私密的。 我假设你在这里谈论POD结构,一切都是公开的。 我会做的是这样的: 将struct关键字更改为class并查看调用代码中断的位置。 这会给你一个线索,说明在哪里使用哪种类型的部件。 由此,确定哪些类型的元素应该公开,哪些应该是私人的。 为公共部分编写访问函数,并更改调用代码以使用它们。 将需要访问私有部分的代码移入类本身。 I can ...
  • 我相信有一种方法可以通过添加外部构建目标,file-> new-> target-> External Build System来使用Xcode和makefile。 这是一个较旧的教程,但步骤可能值得一看: http : //web.ist.utl.pt/jose.alberto.coelho/mac/MakefilesXcode.pdf 我从来没有遇到语法突出显示的问题,但我记得重构支持在某些方面受限于C ++。 So, I ended up using this workaround: even tho ...
  • 您可以点击esc键,它会显示方法列表。 此外,当Xcode认为有匹配时,打字通常会提出一些建议。 You could hit the esc key and it'll show you list of methods. Also, typing will generally bring up some suggestions when Xcode believes there are matches.
  • 这实际上意味着完全重写。 如果我是你,我会尝试使用C ++ / CLI而不是C#。 这样,您可以逐步重写代码并将其转换为托管代码。 需要考虑的事项,您迁移到C#或C ++ / CLI: 使用干净的接口(使用n层体系结构,MVC,...)将软件分区到模块中。 如果每个分区都是COM对象,您甚至可以尝试仅迁移部件并在.NET中使用COM对象。 确定您所依赖的库,并检查如何在.NET中替换它们 确定使用指针的位置,并检查如何在C ++中转换此代码 检查使用RAII对象的位置,并找到在.NET中获得相同结果的方法。 ...
  • 最可能的原因是你在一个非C ++文件中包含了Question.h ,例如你的AppDelegate可能是一个.m文件,你试图在那里包含Question.h 。 有两种解决方案。 首先,您可以制作所有Objective-C ++(将所有.m文件重命名为.mm)。 从历史上看,我发现它非常不方便,因为Xcode和gdb在ObjC ++中总是遇到很多麻烦,你可能会遇到很多困惑(可怕的“没有这个指针”和“堆栈框架的未知语言”错误GDB)。 我没有用最新版本的Xcode和gdb做足够的工作来确定这是否仍然是一个问题, ...
  • fltk和SDL都应该有足够的AGL / CGL代码。 我使用fltk-1.3.0在OS X上构建并运行了cmdline GL应用程序。 它使用AGL子系统,但不推荐使用。 SDL为其Quartz视频图层使用CGL和NSOpenGL: ./src/video/quartz video/ ./src/video/quartz 如果需要编译src.m ,请使用obj-c标志作为编译器,并让原生g ++,llvm-g ++,clang ++或其他任何文件处理链接。 您可能需要添加-framework CGL -f ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。