Switch Game :因子数
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;
}

浙公网安备 33010602011771号