1. 程式人生 > >Switch Game :因子數

Switch Game :因子數

inpu targe int tput ble ons there tom ide

A - Switch Game

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

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.

題目描述:輸入一個n,取i從1到n,在1~n中,是i的倍數,值就變一次,求最後變換完後第n個數的值。

No.1:查找含有多少因子

#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	int n,ans;
	while (cin >> n) {
		ans = 2;
		if (n <= 3)cout<<1<<endl;
		else {
		for (int i = 2; i*i<= n; i++) {		//枚舉含有幾個因子 
			if (n%i == 0) {
				if (i*i == n)ans++;			//
				else ans += 2;				//如果不是i*i,那除了i是因子,n/i也是,所以上邊枚舉到i*i就可以了。 
			}
		}
		if (ans & 1)cout<<1<<endl;
		else cout<<0<<endl;
		}
	}
	return 0;
}

No.2:寫博客時剛想到,可以直接判斷n是不是某個整數的平方,是的話,因子肯定為奇數,所以結果為1.,不是結果為0

#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	double n,ans;
	while (cin >> n) {
		int ans=0;
		for(int i=1;i<400;i++){            //400*400就大於1e5了
			if(i*i==n)ans=1;
		}
		if(ans)cout<<"1\n";
		else cout<<"0\n";
	}
	return 0;
}    




Switch Game :因子數