求1+2+……+n(位运算)

求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[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144
 6 ,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,
 7 536870912,1073741824};
 8 int fun(int x)
 9 {
10     int sum;
11     sum = x<<0;
12     sum += x<<1;
13     sum += x<<2;
14     sum += x<<3;
15     sum += x<<4;
16     sum += x<<5;
17     sum += x<<6;
18     sum += x<<7;
19     sum += x<<8;
20     sum += x<<9;
21     sum += x<<10;
22     sum += x<<11;
23     sum += x<<12;
24     sum += x<<13;
25     sum += x<<14;
26     sum += x<<15;
27     sum += x<<16;
28     sum += x<<17;
29     sum += x<<18;
30     sum += x<<19;
31     sum += x<<20;
32     sum += x<<21;
33     sum += x<<22;
34     sum += x<<23;
35     sum += x<<24;
36     sum += x<<25;
37     sum += x<<26;
38     sum += x<<27;
39     sum += x<<28;
40     sum += x<<29;
41     sum += x<<30;
42     return sum;
43 }
44 int main()
45 {
46     int n;
47     LL ans;
48 
49     while(cin>>n)
50     {
51         ans = (n<<0)&fun(((n+1)&p[0])>>0);
52         ans += (n<<1)&fun(((n+1)&p[1])>>1);
53         ans += (n<<2)&fun(((n+1)&p[2])>>2);
54         ans += (n<<3)&fun(((n+1)&p[3])>>3);
55         ans += (n<<4)&fun(((n+1)&p[4])>>4);
56         ans += (n<<5)&fun(((n+1)&p[5])>>5);
57         ans += (n<<6)&fun(((n+1)&p[6])>>6);
58         ans += (n<<7)&fun(((n+1)&p[7])>>7);
59         ans += (n<<8)&fun(((n+1)&p[8])>>8);
60         ans += (n<<9)&fun(((n+1)&p[9])>>9);
61         ans += (n<<10)&fun(((n+1)&p[10])>>10);
62         ans += (n<<11)&fun(((n+1)&p[11])>>11);
63         ans += (n<<12)&fun(((n+1)&p[12])>>12);
64         ans += (n<<13)&fun(((n+1)&p[13])>>13);
65         ans += (n<<14)&fun(((n+1)&p[14])>>14);
66         ans += (n<<15)&fun(((n+1)&p[15])>>15);
67         ans += (n<<16)&fun(((n+1)&p[16])>>16);
68         ans += (n<<17)&fun(((n+1)&p[17])>>17);
69         ans += (n<<18)&fun(((n+1)&p[18])>>18);
70         ans += (n<<19)&fun(((n+1)&p[19])>>19);
71         ans += (n<<20)&fun(((n+1)&p[20])>>20);
72         ans += (n<<21)&fun(((n+1)&p[21])>>21);
73         ans += (n<<22)&fun(((n+1)&p[22])>>22);
74         ans += (n<<23)&fun(((n+1)&p[23])>>23);
75         ans += (n<<24)&fun(((n+1)&p[24])>>24);
76         ans += (n<<25)&fun(((n+1)&p[25])>>25);
77         ans += (n<<26)&fun(((n+1)&p[26])>>26);
78         ans += (n<<27)&fun(((n+1)&p[27])>>27);
79         ans += (n<<28)&fun(((n+1)&p[28])>>28);
80         ans += (n<<29)&fun(((n+1)&p[29])>>29);
81         ans += (n<<30)&fun(((n+1)&p[30])>>30);
82         ans = ans>>1;
83         cout<<ans<<endl;
84    }
85     return 0;
86 }

 

 

转载于:https://www.cnblogs.com/naix-x/p/3641587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一个常见的和问题,可以使用数学公式来解决。根据等差数列和公式,1到100的和为: S = (a1 + an) * n / 2 其中,a1为首项,an为末项,n为项数。将题目中的数列代入公式,得到: S = (1 + 100) * 100 / 2 = 5050 因此,1到100的和为5050。 ### 回答2: 1到100的和,可以使用等差数列的和公式。将和式的结果代入公式中,然后进行计算。 首先,找出等差数列的首项a为1,公差d为1,项数n为100。将这些值代入等差数列和公式: Sn = (n/2) * (2a + (n-1)d) Sn = (100/2) * (2*1 + (100-1)*1) = 50 * (2 + 99) = 50 * 101 = 5050 所以1到100的和为5050。 ### 回答3: 用1到100递增的数列,可以和的方法有多种。 第一种方法是直接将这100个数相加,即1 + 2 + 3 + … + 100。这可以通过简单的数学运算得出结果。 第二种方法是利用数列和公式。根据等差数列和公式,这个数列的和可以表示为n(n+1)/2,其中n为数列中最后一个数的值。所以,这个数列的和可以写成100(100+1)/2 = 5050。 第三种方法是利用循环语句进行和。我们可以使用一个循环来依次将数列中的每个数加到一个变量中,最后得到总和。可以使用for循环或者while循环实现。 例如,可以使用for循环来和: sum = 0 for i in range(1, 101): sum += i 最后,我们得到的sum即为1到100之间所有数的和。 这三种方法都可以得到同样的结果,即1到100之间所有数的和为5050。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值