求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

我发现网上的做法都很神,各种理由编译的巧妙办法,就能间接地利用循环来解决,构造函数 递归什么的。想了好久,脑子里只有位运算。。。终于折腾出来了。

代码真傻,我真的没用循环。。。

  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. #define LL long long
  5. int p[] = {,,,,,,,,,,,,,,,,,,
  6. ,,,,,,,,,,,
  7. ,};
  8. int fun(int x)
  9. {
  10. int sum;
  11. sum = x<<;
  12. sum += x<<;
  13. sum += x<<;
  14. sum += x<<;
  15. sum += x<<;
  16. sum += x<<;
  17. sum += x<<;
  18. sum += x<<;
  19. sum += x<<;
  20. sum += x<<;
  21. sum += x<<;
  22. sum += x<<;
  23. sum += x<<;
  24. sum += x<<;
  25. sum += x<<;
  26. sum += x<<;
  27. sum += x<<;
  28. sum += x<<;
  29. sum += x<<;
  30. sum += x<<;
  31. sum += x<<;
  32. sum += x<<;
  33. sum += x<<;
  34. sum += x<<;
  35. sum += x<<;
  36. sum += x<<;
  37. sum += x<<;
  38. sum += x<<;
  39. sum += x<<;
  40. sum += x<<;
  41. sum += x<<;
  42. return sum;
  43. }
  44. int main()
  45. {
  46. int n;
  47. LL ans;
  48.  
  49. while(cin>>n)
  50. {
  51. ans = (n<<)&fun(((n+)&p[])>>);
  52. ans += (n<<)&fun(((n+)&p[])>>);
  53. ans += (n<<)&fun(((n+)&p[])>>);
  54. ans += (n<<)&fun(((n+)&p[])>>);
  55. ans += (n<<)&fun(((n+)&p[])>>);
  56. ans += (n<<)&fun(((n+)&p[])>>);
  57. ans += (n<<)&fun(((n+)&p[])>>);
  58. ans += (n<<)&fun(((n+)&p[])>>);
  59. ans += (n<<)&fun(((n+)&p[])>>);
  60. ans += (n<<)&fun(((n+)&p[])>>);
  61. ans += (n<<)&fun(((n+)&p[])>>);
  62. ans += (n<<)&fun(((n+)&p[])>>);
  63. ans += (n<<)&fun(((n+)&p[])>>);
  64. ans += (n<<)&fun(((n+)&p[])>>);
  65. ans += (n<<)&fun(((n+)&p[])>>);
  66. ans += (n<<)&fun(((n+)&p[])>>);
  67. ans += (n<<)&fun(((n+)&p[])>>);
  68. ans += (n<<)&fun(((n+)&p[])>>);
  69. ans += (n<<)&fun(((n+)&p[])>>);
  70. ans += (n<<)&fun(((n+)&p[])>>);
  71. ans += (n<<)&fun(((n+)&p[])>>);
  72. ans += (n<<)&fun(((n+)&p[])>>);
  73. ans += (n<<)&fun(((n+)&p[])>>);
  74. ans += (n<<)&fun(((n+)&p[])>>);
  75. ans += (n<<)&fun(((n+)&p[])>>);
  76. ans += (n<<)&fun(((n+)&p[])>>);
  77. ans += (n<<)&fun(((n+)&p[])>>);
  78. ans += (n<<)&fun(((n+)&p[])>>);
  79. ans += (n<<)&fun(((n+)&p[])>>);
  80. ans += (n<<)&fun(((n+)&p[])>>);
  81. ans += (n<<)&fun(((n+)&p[])>>);
  82. ans = ans>>;
  83. cout<<ans<<endl;
  84. }
  85. return ;
  86. }

求1+2+……+n(位运算)的更多相关文章

  1. 求幂&&快速幂&&位运算

    1.普通的求幂方法: 时间复杂度为O(n),对于比较大的数在1s限时内可能会TLE int pow(int base,int p){ int ans=1; for(int i=1;i<=p;i+ ...

  2. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  3. C语言学习笔记之位运算求余

    我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注意:该方法只对除数是2的N次方幂时才有效. ...

  4. PHP:第一章——按位运算和求余运算(判断奇偶数)

    <?php //按位运算:与1按位运算等于0,输出偶数.如果等于1,输出奇数 //输出偶数: for($i=0;$i<10;$i++){ if(($i & 1)==0){ echo ...

  5. [note]一类位运算求最值问题

    [note]一类位运算求最值问题 给定一些数,让你从中选出两个数a,b,每次询问下列中的一个 1.a and b的最大值 2.a xor b的最大值 3.a or b的最大值 神仙们都是FWT,小蒟蒻 ...

  6. c语言小技巧:C语言学习笔记之位运算求余

    我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不 允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注 意:该方法只对除数是2的N次方幂时才有效 ...

  7. LeetCode通关:求次数有妙招,位运算三连

    分门别类刷算法,坚持,进步! 刷题路线参考: https://github.com/chefyuan/algorithm-base 大家好,我是刷题困难户老三,这一节我们来刷几道很有意思的求次数问题, ...

  8. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  9. 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3) via Hackbuteer1

    转自:http://blog.csdn.net/hackbuteer1/article/details/7390093 ^: 按位异或:&:按位与: | :按位或 计算机系统中,数值一律用补码 ...

随机推荐

  1. 微信token验证失败的解决方法

    一.问题由来 在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况 1. token校验失败 这样回头检查一下各项配置是否正确.如果确定配置没有问题,请按下面的方法检 ...

  2. 修改了/etc/fstab之后出现登录密码输入之后又返回登录界面的问题

    最后那一个挂载到/home下面的盘是我新增加的,如果注释掉就一切正常,如果取消注释,就会发生标题说的问题. 后来我意思都这样直接挂载,导致/home下面原本的东西不在了,注释掉之后再来看,发现下面确实 ...

  3. 设计工具 -uml

  4. [译]:Orchard入门——安装Orchard

    原文链接:Installing Orchard 文章内容基于Orchard 1.8版本 安装Orchard的方式 主要有以下四种方式安装Orchard: 利用Microsoft Web Platfor ...

  5. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  6. java发展史与java的语言特性

    概述: Java 体系比较庞杂,功能繁多,这也导致很多人在自学 Java 的时候总是感觉无法建立 全面的知识体系, 无法从整体上把握Java 的原因. 在这里我们先简单了解一下Java 的版本. 具体 ...

  7. 长按事件jquery mobile

    chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...

  8. Linux学习笔记(4)Linux常用命令之权限管理命令

    (1)chmod chmod命令用于改变文件或目录权限,英文原意为change the permissions mode of a file,所在路径为/bin/chmod,其语法格式为: chmod ...

  9. Tomcat不输入项目名进入自己项目(根目录指向自己的项目)

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...

  10. poj 2774 Long Long Message 后缀数组基础题

    Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 24756   Accepted: 10130 Case Time Limi ...