371. Sum of Two Integers 负数不行 ?

本文介绍了一种特殊算法,该算法能够在不使用加号(+)和减号(-)的情况下计算两个整数的和。通过位操作实现进位计算,巧妙地解决了这一挑战性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1

 

 

class Solution:
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        a=42
        b=-14
        print(bin(b))
        carry=0; 
        
        while b!=0:
          #  if a <0 :
          #      a=~a
          #  if b<0:
          #      b=~b
            carry = a&b;
            a = a ^ b;
            b = carry<<1;
        return a;
def main():
    import sys
    import io
    def readlines():
        for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
            yield line.strip('\n')

    lines = readlines()
    while True:
        try:
            line = next(lines)
            a = int(line);
            line = next(lines)
            b = int(line);
            
            ret = Solution().getSum(a, b)

            out = str(ret);
            print(out)
        except StopIteration:
            break

if __name__ == '__main__':
    main()

 

c++解决 # T590655 【MX-X10-T2】[LSOT-4] NetEase Cloud Music ## 题目背景 Don't underestimate my intelligence network. I know your annual music genre is anime, and your annual keywords are future, world, forever, right? ## 题目描述 In 2077, NetEase Cloud Music introduced a statistical feature. Each song has a "quality value" (any integer), and the "combined value" of any two consecutively listened songs is the sum of their quality values. In 2077, Little H listened to $n$ songs, but he does not know the quality values of each song. You are given the combined values $S_i$ for $1 \le i < n$, where $S_i$ corresponds to the $i$-th and $(i+1)$-th songs. Now, Little H wants to change his listening method: he will listen to songs $m$ times, and in the $i$-th listening session, he will listen to the $a_i$-th song $b_i$ times. Little H asks you to calculate the total sum of the quality values of all songs listened to in the new method. **A song listened to multiple times contributes its value multiple times**. However, if it is impossible to determine the total sum uniquely, output `Impossible`. ## 输入格式 - The first line contains two integers $n$ and $m$, representing the number of songs and listening sessions. - The second line contains $n-1$ integers $S_1, S_2, \ldots, S_{n-1}$. - The next $m$ lines each contain two integers $a_i$ and $b_i$, describing the $i$-th listening session. ## 输出格式 Output one line containing an integerthe total sum of quality values. If the sum cannot be uniquely determined, output the string `Impossible`. ## 输入输出样例 #1 ### 输入 #1 ``` 5 2 8 6 7 2 2 2 3 2 ``` ### 输出 #1 ``` 12 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 5 2 8 6 7 2 2 2 3 10 ``` ### 输出 #2 ``` Impossible ``` ## 输入输出样例 #3 ### 输入 #3 ``` 20 19 425 46 176 409 156 35 128 467 534 411 362 760 32 17 403 210 462 10 94 15 104 12 193 6 249 18 845 1 72 15 269 2 633 10 858 14 282 14 950 5 98 11 162 12 296 14 846 15 793 11 858 19 942 1 886 19 968 ``` ### 输出 #3 ``` 283895 ``` ## 说明/提示 **Sample Explanat
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值