码迷,mamicode.com
首页 > 其他好文 > 详细

Switch Game :因子数

时间:2019-03-17 18:18:02      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:targe   get   who   lamp   include   color   inf   cpp   namespace   

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 :因子数

标签:targe   get   who   lamp   include   color   inf   cpp   namespace   

原文地址:https://www.cnblogs.com/52dxer/p/10548007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!