2014-03-20 01:14

题目:有100栈灯,一开始都关着。如果你按照n从1~100的顺序,每次都掰一下n的倍数的开关(开->关,关->开),那么到最后有多少灯是亮的?

解法:这个题目要多想想再动手,因为想通了以后就基本不用动手了。对于编号为x的灯,每当ix的约数时,在第i轮时第x号灯就被掰了一次。比如6的约数为{1,2,3,6},6号灯被掰了4次。那么,每个灯被掰的次数就是约数个数次。约数个数的公式你懂的,但是用不着去算。如果一盏灯是开的,那么它就被掰了奇数次,根据约数和公式,只有因数分解之后所有指数都是偶数的时候,约数个数才为奇数。比如36的约数{1,2,3,4,6,9,12,18,36}。只有完全平方数满足条件。还有另一种想法,如果一个数n不是完全平方数,那么n的约数一定可以分为个数相同的两拨儿,一拨儿大于平方根,一拨儿小于平方根,配对正好成绩等于n。只有完全平方数才会多出一个整数的平方根,导致约数个数为奇数。

代码:

 // 6.6 Given n lights, every time you toggle the switches of k-multiples. k goes from 1 to n.
// Assume they're all off at first, how many of them are on at last.
#include <cstdio>
using namespace std; // My solution:
// For a number m between 1~n, the key is total number of divisors of m, defined as num_div(m).
// The light m is toggled for num_div(m) times. If it is odd, light is on. Even and light is off.
// Only perfect square can be factorized, where every prime factor has even exponents.
// That is, m = p[0]^e[0] * p[1]^e[1] * ... * p[whatever]^e[whatever]
// num_div(m) = (e[0] + 1) * (e[1] + 1) * ... * (e[whatever] + 1)
// If num_div(m) is to be odd, every multiplier has to be odd, every e[i] has to be even.
// Thus m has to be a perfect square, if light m is on at last.
int main()
{
int n;
int i; while (scanf("%d", &n) == && n > ) {
for (i = ; i <= n / i; ++i) {
printf("%d ", i * i);
}
printf("\n");
printf("%d light(s) is(are) on.\n", i - );
} return ;
}

《Cracking the Coding Interview》——第6章:智力题——题目6的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第6章:智力题——题目4

    2014-03-20 01:02 题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题.有兴趣的还是看书去吧. 解法:能把题目看懂,你就完成80%了,用反证法吧. 代码: / ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第6章:智力题——题目5

    2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...

随机推荐

  1. input,button制作按钮IE6,IE7点击时1px黑边框的解决方法

    按钮在IE6中点击时1px黑边框的最常见的解决方法 首先设置按钮为none,然后在按钮外面套一层来实现边框的效果,部分代码如下 .btnbox{ border:solid 1px red;} .btn ...

  2. POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图

    题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...

  3. mmap内存映射

    http://blog.csdn.net/kongdefei5000/article/details/70183119 内存映射是个很有用,也很有意思的思想.我们都知道操作系统分为用户态和内核态,用户 ...

  4. react里面Fragments的使用

    关于react Fragments,React 中一个常见模式是为一个组件返回多个元素.Fragments 可以让你聚合一个子元素列表,并且不在DOM中增加额外节点. render() { retur ...

  5. maven项目右键快捷方式,然后点击Run As

    在某一个maven项目右键快捷方式,然后点击Run As就可以发现几个Maven的命令: 1.Maven Build: 这个命令用于编译Maven工程,执行命令后会在target文件夹中的classe ...

  6. 解决Gearman 报sqlite3错误

    删除了系统原带的sqlite3 ,到官网上下一个源码,重新编译安装sqlite3. 如:把sqlite3安装到 /usr/local/sqlite3tar zxf sqlite3.xxxx.tar.g ...

  7. Spring任务执行器(TaskExecutor)

    Spring任务执行器(TaskExecutor)    Spring通州任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线 ...

  8. Python-三元运算符和lambda表达式

    一.三元运算符 #当满足条件1时,res=值1:否则res=值2 res = 值1 if 条件1 else 值2 举例说明: res=10 #简单的if else语句 if abs(res)>0 ...

  9. CUDA数组分配

    原问链接 概述:数组分配可以通过cudaMallocArray()和cudaMalloc3DArray() 1.cudaMallocArray() cudaError_t cudaMallocArra ...

  10. ADO.net中常用的对象有哪些?

    ADO.net中常用的对象有哪些?分别描述一下. 答:Connection 数据库连接对像 Command 数据库命令 DataReader 数据读取器 DataSet 数据集 DataReader与 ...