JAVA—— 运用ArrayList实现逻辑推理题(谁养鱼)

经典谁养鱼sql oracle 数据库中经典谁养鱼sql,面试时貌似用的着。 立即下载

运用ArrayList实现逻辑推理题(谁养鱼)


1.穷举法

穷举法的基本思想是根据题目的部分条件确定答案的大致范围,并在此范围内对所有可能的情况逐一验证,直到全部情况验证完毕。若某个情况验证符合题目的全部条件,则为本问题的一个解;若全部情况验证后都不符合题目的全部条件,则本题无解。

2.谁养鱼

用笔穷举的速度非常慢,《谁养鱼》是爱因斯坦在20世纪初出的谜语,他说世界上有百分之98的人答不出来。你能答出来吗?
这道逻辑性很强的题是:
1)在一条街上有5座房子,喷了5种颜色。
2)每个房子里住着不同国籍的人.
3)每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。
问题是:谁养的是鱼?

所列举项应该满足如下15个要求:

  • 英国人住红色房子;
  • 瑞典人养狗;
  • 丹麦人喝茶;
  • 绿色房子在白色房子左边;
  • 绿色房子主人喝咖啡;
  • 抽PalMal香烟的人养鸟;
  • 黄色房子主人抽Dunhill香烟;
  • 住在中间房子的人喝牛奶;
  • 挪威人住第一间房;
  • 抽Blends香烟的人住在养猫的人隔壁;
  • 养马的人住抽Dunhill香烟的人隔壁;
  • 抽BlMt的人喝啤酒;
  • 德国人抽Prince香烟;
  • 挪威人住蓝色房子隔壁;
  • 抽Blends香烟的人有一个喝水的邻居。

3.原理

  • Init()方法中创建ArrayList列表集合,运用多种循环进行判断获得各组数据之间的组合方式,并将符合要求的组合方式放入列表集合中。循环遍历列表集合,将元素放入颜色、人员、饮料等数组中。
  • calculate()方法调用init()方法对数组元素进行初始化。循环判断比较各数组元素的关系获得符合原题要求的元素的索引,再根据 show()方法循环将指定素引的元素打印到控制台。

4.语法

  • ArrayList是List接口的一个可变长数组实现,实现了所有List接口的操作,并允许存储空(null)值。除没有进行同步,ArrayList基本等同于Vector。在Vectors中几乎对所有的方法都进行同步,但ArrayList仅对writeObject()和readObject()进行同步。其他如add()、remove()等都没有同步。
  • ArrayList实现了Java.io.Serializable接口,所以ArrayList对象可以序列化到持久存储介质中。
  • 实例化一个ArrayList对象时,可以指定一个初始容量,默认的容量为10。当元素超过ArrayList对象的初始大小时,容器容量大小増加原来的50%。

5.代码

主程序:

public static void main(String args[]) {
   
   
		//Java程序主入口
		rw33 test=new rw33();//实例化对象
		long l=System.currentTimeMillis();//获得系统时间
		test.calculate();//调用的方法进行计算统计
		System.out.println("计算共用时:"+(System.currentTimeMillis()-l)+"ms");//获得计算所花费的时间
	}

计算一组数据的组合方式:

public void init() {
   
   
		//计算一组数据的组合方式
		ArrayList array=new ArrayList();//创建集合数组
		for(int num1=0;num1<5;num1++) {
   
   
			for(int num2=0;num2<5;num2++) {
   
   
				if(num2==num1)
					continue;
				for(int num3=0;num3<5;num3++) {
   
   
					if(num3 ==num2||num3==num1)
						continue;
					for(int num4 =0;num4<5;num4++) {
   
   
						if(num4==num3||num4==num2||num4==num1)
							continue;
						for (int num5=0;num5<5;num5++) {
   
   
							if(num5==num4||num5==num3||num5==num2||num5==num1)
								continue;
							int oneArray[]= {
   
   num1,num2,num3,num4,num5};
							array.add(oneArray);
						}
					}
				}
			}
		}
		color=new int[array.size()][5];//创建颜色的二维数组
		for(int count=0;count<array.size();count++) {
   
   
			//循环数组时初始化房子颜色数据
			color[count]=(int[])array.get(count);
		}
		person=color;
		drink=color;
		smoke=color;
		pet = color;
	}

判断运算:

public void calculate() {
   
   
		//判断运算
		init();//调用方法时初始化数据
		for(int num1=0;num1<color.length;num1++) {
   
   
			if(!con4(num1))
				continue;
			if(!con14
JAVA运用ArrayList实现逻辑推理题(谁养鱼) 使用JAVA语言中的ArrayList解决爱因斯坦在20世纪初出的逻辑推理题——《谁养鱼》,在一条街上有5座房子,喷了5种颜色。每个房子里住着不同国籍的人。每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。问谁养的是鱼? 所列举项应该满足如下要求:英国人住红色房子;瑞典人养狗;丹麦人喝茶;绿色房子在白色房子左边;绿色房子主人喝咖啡;抽PalMal香烟的人养鸟;黄色房子主人抽Dunhill香烟;住在中间房子的人喝牛奶;挪威人住第一间房;抽Blends香烟的人住在养猫的人隔壁;养马的人住抽Dunhill香烟的人隔壁;抽BlMt的人喝啤酒;德国人抽Prince香烟;挪威人住蓝色房子隔壁;抽Blends香烟的人有一个喝水的邻居。 立即下载

相关推荐

Java_Einstein.rar_Einstein_Einstein fish green_PAL_mall_谁养鱼

用JAVA解答智力题: 爱因斯坦留下来的智力题目(谁养鱼?)据说世界上有98%的人答不出来   主题:爱因斯坦留下来的智力题目   1、在一条街上,有5座房子,喷了5种颜色。   2、每个房里住着不同国籍的人   3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物   问题是:谁养鱼?   提示:   1、英国人住红色房子   2、瑞典人养狗   3、丹麦人喝茶   4、绿色房子在白色房子左面   5、绿色房子主人喝咖啡   6、抽Pall Mall 香烟的人养鸟   7、黄色房子主人抽Dunhill 香烟   8、住在中间房子的人喝牛奶   9、 挪威人住第一间房   10、抽Blends香烟的人住在养猫的人隔壁   11、养马的人住抽Dunhill 香烟的人隔壁   12、抽Blue Master的人喝啤酒   13、德国人抽Prince香烟   14、挪威人住蓝色房子隔壁   15、抽Blends香烟的人有一个喝水的邻居   爱因斯坦在20世纪初出的这个谜语。他说世界上有98%的人答不出来。

电阻、电容、电感的实际等效模型

1、电阻的等效模型 2、电容的寄生参数 3、电容的等效参数 4、理想铝电解电容与实际铝电解电容的区别 5、电感的等效模型  

鸟宿江边树的博客 4万+

家庭网络中,路由器和交换机的连接顺序

它的目的是给路由器增加端口,用户访问互联网的速度比情况一要快,常用于办公网络、家庭成员多的场景。光纤入户首先连接的肯定是光猫,而在光猫的后面,是先连接交换机?还是应该接路由器呢?家庭网络中路由器和交换机的先后连接顺序。它目的是让更多的用户访问互联网。常用于一些超大型机房、网吧等。至于路由器和交换机的区别。两者之间很多功能都有兼容。但用户上网速度相对较慢。在技术日益迭代的今天。

m0_74075744的博客 1万+

从硅片到时钟:揭秘SiTime MEMS谐振子的精密制造之旅

本文深入解析了SiTime MEMS谐振子的精密制造工艺,从硅晶圆的提纯到DRIE深硅刻蚀,再到真空封装技术,揭示了其如何克服传统石英晶振的局限。通过对比测试数据,展示了MEMS谐振子在精度、可靠性和生产效率上的显著优势,特别是在5G基站和汽车电子等领域的应用潜力。

weixin_42522953的博客 403

基于STM32的智能交通灯管理系统设计

本项目基于STM32微控制器设计了一个智能交通灯管理系统,通过多个传感器模块实现对交通灯的智能化控制。项目涉及硬件电路设计、传感器数据采集和逻辑控制实现,适用于城市交通路口或小区路口。该基于STM32的智能交通灯系统通过超声波传感器实现了流量监测,结合LED和蜂鸣器实现了自动交通信号控制,适用于城市或社区路口。基于STM32CubeMX生成的框架,编写红绿灯切换、流量监测和蜂鸣器控制的代码。以下为主循环,通过不断读取传感器数据,结合交通灯和蜂鸣器模块实现智能交通灯控制。

2401_88410555的博客 2063

43、基于机器学习的GNSS信号质量监测方法

本文提出了一种基于支持向量机(SVM)的GNSS信号质量监测方法,旨在有效检测邪恶波形(EWF)威胁并抑制多径干扰对导航精度的影响。通过构建包含现场采集与合成数据的数据集,结合11个相关器测量值、链路预算、仰角和方位角等特征,采用RBF核函数的SVM模型进行分类。研究评估了不同平滑时间窗口对漏检率和误报率的影响,结果表明在约35-40秒平滑时间下性能趋于稳定,并可通过多数投票机制进一步优化检测性能。该方案适用于星载GNSS接收器,在计算资源受限条件下实现了高可靠性与低完整性风险的平衡。

yyy34的博客 188

SEGGER Embedded Studio初体验

先楫HPM5E00EVK开发环境搭建

hywing's blog 1109

20、Citrix 云桌面常见VDA注册问题汇总

本文总结了与 VDA 中出现的 VDA 会话断开连接、注册、会话卡住以及挂起/崩溃相关的主要支持文章。

我在Citrix、VMware、Veeam、微软和NVIDIA等领域拥有丰富经验的专业人士,专注于设计和维护虚拟化基础设施、实施备份与灾难恢复策略,致力于优化企业的IT环境。 944

基于Simulink的智能车辆电子稳定控制(ESC)仿真

本文完成了基于 Simulink 的智能车辆电子稳定控制(ESC),实现了:✅ 构建含轮胎非线性的车辆动力学模型✅ 设计滑模横摆稳定性控制器✅ 实现四轮制动力分配逻辑✅ 验证在双移线低附着场景下的卓越稳定性✅ 为主动安全控制系统开发提供完整范式。

xiaoheshang_123的博客 79

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

上一篇: JAVA——点对面通信(Socket基于TCP/IP协议)
下一篇: JAVA——多线程(碰撞的球)
Atishoo_13
博客等级 码龄9年 84粉丝 56原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值