7.18 热身赛 A+B Problem
A+B Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 83 Accepted Submission(s): 62
Problem Description
HZ has a special calculator, in which all the integers are 11-bit signed integers. The range of an 11-bit signed integer is from −210 to 210−1 (from −1024 to 1023). When using this calculator to calculate the sum of two integers A and B, the result may be different from other calculators. Here are some examples:
1 + 1 = 2
1023 + 1 = −1024
1023 + 2 = −1023
−1024 + (−1) = 1023
−1024 + (−2) = 1022
HZ found this special calculator very strange, so he comes to you. You are given two integers A and B, and you need to guess the result of A+B of this calculator.
Input
The first line of input contains an integer T (1≤T≤105
), denoting the number of test cases.
Each test case contains two integers A and B in one line. −1024≤A,B≤1023
.
Output
For each test case, print one integer in one line, denoting your answer.
Sample Input
5
1 1
1023 1
1023 2
-1024 -1
-1024 -2
Sample Output
2
-1024
-1023
1023
1022
大概题意
找个规律就行
思路
直接用if,else,简单粗暴
代码
#include <iostream>
using namespace std;
int a, b, t;
int main() {
cin >> t;
while (t--) {
cin >> a >> b;
int res = a + b;
if (res > 1023) {
res = -1024 + (res - 1024);
} else if (res < -1024) {
res = 1023 + (res + 1025);
}
cout << res << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效