JAVA_100doors

Question:

100 doors in a row are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it).
The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door
(door #3, #6, #9, ...), etc, until you only visit the 100th door.

Question: What state are the doors in after the last pass? Which are open, which are closed?

[Source http://rosettacode.org]

Answer:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class test
{
    public static void main( String args[] ){
    	int[] array= new int[100];
    	for(int i=1;i<100;i++) {
    		for(int k=1;k<100;k++) {
    			if(k%i==0) {
    				array[k-1]=1-array[k-1];
    			}
    		}
    		
    	}
    	System.out.println(array[99]);
    	
   }
}

Result:0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值