問(wèn)題來(lái)源:Problem - 2053
Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
Sample Input
1
5
Sample Output
10
Hint
hint
Consider the second test case:The initial condition? : 0 0 0 0 0 …After the first operation? : 1 1 1 1 1 …After the second operation : 1 0 1 0 1 …After the third operation? : 1 0 0 0 1 …After the fourth operation : 1 0 0 1 1 …After the fifth operation? : 1 0 0 1 0 …The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
問(wèn)題解讀:
? ? 問(wèn)題主要表達(dá)的意思就是原本有n盞燈璃诀,都是關(guān)閉的,當(dāng)輸入一個(gè)數(shù)字時(shí),從第一次開(kāi)始,直到我們輸入的數(shù)字,以次數(shù)為約數(shù)的編號(hào)的燈的狀態(tài)就會(huì)改變,問(wèn)最后一盞燈的狀態(tài)。
思路分析:
? ? 發(fā)現(xiàn)一個(gè)規(guī)律,第n盞燈的狀態(tài)與n的約數(shù)個(gè)數(shù)有關(guān)丧叽。當(dāng)約數(shù)個(gè)數(shù)為偶數(shù)時(shí),狀態(tài)不變公你;當(dāng)約數(shù)個(gè)數(shù)為奇數(shù)時(shí)踊淳,狀態(tài)改變。
實(shí)現(xiàn)代碼:
? ??import java.util.Scanner;
public class Test_2053B {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while(in.hasNextInt()) {
int n = in.nextInt();
count(n);
}
}
public static void count(int n) {
int c = 0;
for(int i=1;i<=n;i++) {
if(n % i == 0)
c++;
}
if(c % 2 == 1)
System.out.println("1");
else
System.out.println("0");
}
}