hdu_problem_2053_Switch Game

題目大意爲:有一排全部都是關着的燈,然後在這些燈上進行了一系列的操作,第i次操作時將i的倍數的燈改變狀態,即ON變成OFF,OFF變成ON,輸入n,輸出再無數次操作後,第n盞燈的狀態,0表示OFF,1表示ON

思路:從數字本身的性質考慮
1)如果一個數是質數,那麼它只能分解爲1和它本身,也就是隻會執行2次操作,那麼如果是質數,肯定是關閉的狀態。
2)如果一個數是合數,就要分兩個情況

  1. 如果它不是完全平方數,那根據合數的定義,肯定可以分解成n個組合,每個組合裏面都是2個不相等的數字,例如6=16=236 = 1*6 = 2*3,也就是說這個數經歷了偶數次操作,那麼肯定也是關閉狀態。
  2. 如果這個數是完全平方數,那麼他可以分解爲n-1個組合加上他的算術平方根,例如4=14=224 = 1*4=2*2,這種情況下,這個數字只被操作了奇數次,那麼這個燈的狀態就是開着的。

綜上所述,如果這個數是完全平方數(肯定不是質數),就是ON,如果不是,就是OFF

/*
*
*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
*1
*0
*
*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.
*
*
*Author
*LL
*
*
*Source
*校慶杯Warm Up
*
*
*Recommend
*linle
*
*/
#include<iostream>
using namespace std;
int i;
bool is_on(int n) {
 i = sqrt(n);
 return i * i == n;
}
float sqrt(int num) {
 float temp;
 for (temp = 0; temp*temp <= num; temp += 1);
 temp -= 1;
 for (int i = 0; i < 3; i++) {
  temp = (temp + num / temp) / 2;
 }
 return temp;
}
int main() {
 int n;
 while (cin >> n) {
  if (is_on(n))
   cout << "1" << endl;
  else
   cout << "0" << endl;
 }
 system("pause");
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章