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. Js Date类型

    一:格式化方法 var box=new Date(); //标准时间,如果没传参数,得到的时间为当前时间 //alert(Date.parse('4/12/2007')); //11763072000 ...

  2. jrtplib源码分析 第一篇 jthread的编译与分析

    第一篇 jthread的编译与分析 jrtplib代码依赖库jthread,因此先从jthread开始jrtplib的学习.首先从以下链接下载jthread的源代码http://research.ed ...

  3. Socket的基本使用步骤

    Socket的基本使用步骤 一.使用Socket,首先需要导入这几个系统头文件 #import <sys/socket.h> #import <netinet/in.h> #i ...

  4. IOS instancetype的使用好处

    instancetype的类型表示上,跟id一样,可以表示任何对象类型 instancetype只能用在返回值类型上,不能像 id 一样用在参数类型上 instancetype 比 id 多一个好处 ...

  5. 2018.9.7 ArrayList

    ArrayList简介 ArrayList核心源码 ArrayList源码分析 System.arraycopy()和Arrays.copyOf()方法 两者联系与区别 ArrayList核心扩容技术 ...

  6. css3之Media Queries 媒体查询

    一.初步了解 Media Queries是CSS3新增加的一个模块功能,其最大的特点就是通过css3来查询媒体,然后调用对应的样式. 了解Media Queries之前需要了解媒体类型以及媒体特性: ...

  7. JAVA HTTP连接(HttpURLConnection)中使用代理(Proxy)及其验证(Authentication)

    public static void main(String[] args) { // TODO Auto-generated method stub try { URL url = new URL( ...

  8. Multigrid for Poisson’s Equation

    泊松方程如何用多重网格求解 来源:佐治亚理工学院 教授:Prof. Richard Vuduc 主页:http://vuduc.org/index.php 教授内容:http://vuduc.org/ ...

  9. Eclipse 修改默认工作空间

    第一次启动Eclipse时会弹出对话框,让你进行Workspace Launcher,也就是设置Eclipse的项目存放路径.但是,当你勾选“Use this as the default and d ...

  10. 7、SpringBoot+Mybatis整合------PageHelper简单分页

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b ...