给定一个正整数 n ,你的任务使用最少的操作次数把序列 1, 2, 3, …… , n 中的所有数都变成 0 。每次操作可以从序列中选择一个或者多个数,同时减去一个相同的正整数。比如,1, 2, 3 可以把 2 和 3 同时减小 2 ,得到 1, 0, 1 。

模拟几次就能看出做法了。

例如当 n = 6 的时候

0      -----      1  2  3  4  5  6

1      -----      1  2  3  0  1  2

2      -----      1  0  1  0  1  0

3      -----      0  0  0  0  0  0

再比如 n = 9 的时候

0      -----      1  2  3  4  5  6  7  8  9

1      -----      1  2  3  4  0  1  2  3  4

2      -----      1  2  0  1  0  1  2  0  1

3      -----      1  0  0  1  0  1  0  0  1

4      -----      0  0  0  0  0  0  0  0  0

通过这两组例子就能很容易的看出,我每次都把原有数列分成两份,1 ~ len / 2 , 和 (len / 2) + 1 ~ len两部分。然后每次都减去 len / 2 这么多,然后 len = len / 2,如此循环,直到最后把 1 减去,所用的次数就是最小的次数。

现在知道怎么做了,就可以继续分析题目了。

按照上面的方法,我们可以得到下表:

1         ----         1

2         ----         2

3         ----         2

4         ----         3

5         ----         3

6         ----         3

7         ----         3

8         ----         4

打表打到这里,可以很轻松的看到规律。1 个 1 , 2 个 2, 4 个 3 , 8 个 4 。。。以此类推,规律就是这样了。然后写一个递归就能找到答案了。

附AC代码:

   1: #include <stdio.h>

   2: #include <math.h>

   3: #include <iostream>

   4: #include <cstdarg>

   5: #include <algorithm>

   6: #include <string.h>

   7: #include <stdlib.h>

   8: #include <string>

   9: #include <list>

  10: #include <vector>

  11: #include <map>

  12: #define LL long long

  13: #define M(a) memset(a, 0, sizeof(a))

  14: using namespace std;

  15:  

  16: void Clean(int count, ...)

  17: {

  18:     va_list arg_ptr;

  19:     va_start (arg_ptr, count);

  20:     for (int i = 0; i < count; i++)

  21:         M(va_arg(arg_ptr, int*));

  22:     va_end(arg_ptr);

  23: }

  24:  

  25: int res(int n)

  26: {

  27:     return (n == 1) ? 1 : res(n / 2) + 1;

  28: }

  29:  

  30: int main()

  31: {

  32:     int n;

  33:     while (cin >> n)

  34:         cout << res(n) << endl;

  35:     return 0;

  36: }

最新文章

  1. jdbc 设置连接支持多条sql
  2. .NET和JAVA同等加密方法,MD5和DES对称加密记录
  3. mysql中binlog_format模式与配置详解
  4. 谈EXPORT_SYMBOL使用
  5. (转载)LINUX UNBUNTU10.04 下 搭建OC编译环境
  6. careercup-数组和字符串1.6
  7. Android应用程序资源的查找过程分析
  8. 一则 ORA-00471 处理方法
  9. Async和Await进行异步编程
  10. 使用ThinkPHP的扩展功能
  11. python 函数 装饰器的使用方法
  12. 简单的C语言编译器--概述
  13. python的运算符与表达式
  14. js循环语句
  15. SQLServer约束介绍
  16. VS2010 开发 VB6.0 activeX控件 dll
  17. webpack中file-loader和url-loader的关系
  18. TJU Problem 2101 Bullseye
  19. 洛谷P3380 【模板】二逼平衡树(树套树,树状数组,线段树)
  20. redis分布式集群3种架构方案

热门文章

  1. Peer certificate cannot be authenticated with known CA certificates.
  2. C#中out的用法
  3. 华为上机:IP地址转换
  4. C/C++类型转换
  5. 控制台console输出信息原理理解
  6. linuxlcd驱动程序编写 mini2440(w35)
  7. 在Hadoop1.2.1分布式集群环境下安装hive0.12
  8. dojo 二 AMD模块
  9. Android开发环境的安装 Eclipse
  10. linux查看某个端口被占用