求1+2+……+n(位运算)
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
我发现网上的做法都很神,各种理由编译的巧妙办法,就能间接地利用循环来解决,构造函数 递归什么的。想了好久,脑子里只有位运算。。。终于折腾出来了。
代码真傻,我真的没用循环。。。
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- #define LL long long
- int p[] = {,,,,,,,,,,,,,,,,,,
- ,,,,,,,,,,,
- ,};
- int fun(int x)
- {
- int sum;
- sum = x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- sum += x<<;
- return sum;
- }
- int main()
- {
- int n;
- LL ans;
- while(cin>>n)
- {
- ans = (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans += (n<<)&fun(((n+)&p[])>>);
- ans = ans>>;
- cout<<ans<<endl;
- }
- return ;
- }
求1+2+……+n(位运算)的更多相关文章
- 求幂&&快速幂&&位运算
1.普通的求幂方法: 时间复杂度为O(n),对于比较大的数在1s限时内可能会TLE int pow(int base,int p){ int ans=1; for(int i=1;i<=p;i+ ...
- 求集合中选一个数与当前值进行位运算的max
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...
- C语言学习笔记之位运算求余
我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注意:该方法只对除数是2的N次方幂时才有效. ...
- PHP:第一章——按位运算和求余运算(判断奇偶数)
<?php //按位运算:与1按位运算等于0,输出偶数.如果等于1,输出奇数 //输出偶数: for($i=0;$i<10;$i++){ if(($i & 1)==0){ echo ...
- [note]一类位运算求最值问题
[note]一类位运算求最值问题 给定一些数,让你从中选出两个数a,b,每次询问下列中的一个 1.a and b的最大值 2.a xor b的最大值 3.a or b的最大值 神仙们都是FWT,小蒟蒻 ...
- c语言小技巧:C语言学习笔记之位运算求余
我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不 允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注 意:该方法只对除数是2的N次方幂时才有效 ...
- LeetCode通关:求次数有妙招,位运算三连
分门别类刷算法,坚持,进步! 刷题路线参考: https://github.com/chefyuan/algorithm-base 大家好,我是刷题困难户老三,这一节我们来刷几道很有意思的求次数问题, ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3) via Hackbuteer1
转自:http://blog.csdn.net/hackbuteer1/article/details/7390093 ^: 按位异或:&:按位与: | :按位或 计算机系统中,数值一律用补码 ...
随机推荐
- 微信token验证失败的解决方法
一.问题由来 在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况 1. token校验失败 这样回头检查一下各项配置是否正确.如果确定配置没有问题,请按下面的方法检 ...
- 修改了/etc/fstab之后出现登录密码输入之后又返回登录界面的问题
最后那一个挂载到/home下面的盘是我新增加的,如果注释掉就一切正常,如果取消注释,就会发生标题说的问题. 后来我意思都这样直接挂载,导致/home下面原本的东西不在了,注释掉之后再来看,发现下面确实 ...
- 设计工具 -uml
- [译]:Orchard入门——安装Orchard
原文链接:Installing Orchard 文章内容基于Orchard 1.8版本 安装Orchard的方式 主要有以下四种方式安装Orchard: 利用Microsoft Web Platfor ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- java发展史与java的语言特性
概述: Java 体系比较庞杂,功能繁多,这也导致很多人在自学 Java 的时候总是感觉无法建立 全面的知识体系, 无法从整体上把握Java 的原因. 在这里我们先简单了解一下Java 的版本. 具体 ...
- 长按事件jquery mobile
chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...
- Linux学习笔记(4)Linux常用命令之权限管理命令
(1)chmod chmod命令用于改变文件或目录权限,英文原意为change the permissions mode of a file,所在路径为/bin/chmod,其语法格式为: chmod ...
- Tomcat不输入项目名进入自己项目(根目录指向自己的项目)
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...
- poj 2774 Long Long Message 后缀数组基础题
Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 24756 Accepted: 10130 Case Time Limi ...