变换灯泡颜色

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Given n = 3. 
At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].
So you should return 1, because there is only one bulb is on.

按题意coding

public int bulbSwitch(int n){
int[] arrN = new int[n];
int count = 0;
for (int i = 0; i < arrN.length; i++) {
arrN[i] =0;
}
for (int i = 0; i < arrN.length; i++) {
for (int j = 0; j < arrN.length;j++ ) {
if(i==0){
arrN[j] = 1;
// j++;
}else{
int k = i+1;
if((j+1)%k==0){arrN[j]=(arrN[j]==1?0:1);}
// j=j+i;
}
}
} for (int i = 0; i < arrN.length; i++) {
if (arrN[i]==1) {
count++;
}
}
return count;
}

运行超时:

灯泡颜色的变换只存在于自己的因子处

1.素数,只有1和自己。

2.普通数,因子成对出现。

3.只有完全平方数灯泡奇数次点亮不会熄灭。

此题转化为求完全平方数的个数!!!

比如:3-->1,4-->2,10-->3

 public static int bulbSwitch(int n){
return (int)Math.sqrt(n);
}

最新文章

  1. C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素”
  2. SQLite3
  3. Sublime Text 3 如何修改默认快捷键
  4. .bashrc文件是干什么的(转)
  5. 【解决】 新浪sae固定链接404 问题
  6. RMB转换人民币大小金额
  7. hdu 5310 Souvenir(BestCoder 1st Anniversary ($))
  8. miniui MVC datagrid数据绑定
  9. jQuery live与bind的区别
  10. CSS基础(01)
  11. 《jQuery、jQuery UI及jQuery Mobile技巧与示例》勘误收集
  12. javaTemplates-学习笔记一
  13. 分布式session
  14. 浙大pat 1054 题解
  15. windows系统下升级nodejs
  16. c# 序列化效率比拼
  17. Linux之grep的使用
  18. 回顾ThreadLocal
  19. Excel2013 破解(编辑工作表受保护)密码
  20. mysql 权限管理介绍

热门文章

  1. web应用程序逻辑架构
  2. 【解决】SQL Server作业中Excel Application不能访问文件
  3. background-position值为像素时的使用方法
  4. 72. Generate Parentheses &amp;&amp; Valid Parentheses
  5. Solaris 自动挂载
  6. Type.GetType(string)为空
  7. linux中的进程和线程
  8. nvm
  9. OAuth认证
  10. 脚本工具: 查看当前系统被写入的FD