100 persons, 100 lamps (details)

Source: Internet
Author: User

Question:The one hundred lamps are arranged in a row and the initial status is bright. The number is 1-100,100, and the number is 1-100. Each person goes through the lamps from the First lamp. Each person only pulls the lamp multiple of the corresponding number and asks how many lights are on at last?

This is a classic pen question and frequently appears. The following is a concise implementation code that has been verified.

 1 package 笔试; 2  3 public class 完全平方数100人和灯 { 4     public static void main(String[] args) { 5         boolean[] light=new boolean[100]; 6         for (int i = 0; i < light.length; i++) { 7             for (int j = 0; j < light.length; j++) { 8                 if ((i+1)%(j+1)==0) { 9                     light[i]=!light[i];10                 }11             }12             System.out.print("第"+(i+1)+"盏"+(light[i]?"亮  ":"灭  "));13         }14     } 16 }

The running result is 1st 4 9 16 25... 81 100. In fact, it is to calculate the number of all full shards in 0-100. Only an odd number of values is a factor. Because the light is on at the beginning, only by pressing the odd number of times can we ensure that the final result is still on.

By the way, the Boolean Type of Java. We all know that Java data types are divided into three categories: Boolean, numeric, and numeric. The value of the Boolean Type in Java can only be true or false, and cannot be represented by digits. Other types cannot be converted to the boolean type.

The default value of Java boolean type is false. Why?

We know that the bottom layer of computing editing can only recognize 0, 1, that is, power-on and power-off status.By default, in order to save resources, do not use power-on as the default situation, that is, 0, or false.

Java Boolean simple code verification:

 1 package Test; 2 //Java布尔类型的默认值是false 3 public class 布尔验证 { 4     static boolean b1; 5     public static void main(String[] args) { 6         boolean b2 = true; 7         String s= b2+" boolean类型转换成字符串";//" boolean类型转换成字符串" 8         boolean b3=!b2; 9         System.out.println(b1);10         System.out.println(b2);11         System.out.println(b3);12         System.out.println(s);13     }14 }15 /*16 运行结果:false17         true18         false19         true boolean类型转换成字符串20 21 */

For the implementation of the full number of workers, only the correct results are output. nested for loops are not applicable. The more efficient code execution is:

 1 // 一百人一百盏灯 求完全平方数题 2  3 public class pf { 4      5     public static void main(String[] args) { 6     int i=1,j=0; 7     while(j<100){ 8           j=i*i; 9           i++;10         System.out.print(j);11     }12    }

 

The output result is consistent with the preceding one.

100 persons, 100 lamps (details)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.