当前位置: 首页 > news >正文

P1601题解

题意

就是A+B,相信大家都能理解,对于Python来说很简单,对C++崽就不友好了

解析

下面提供一种解法(代码中的BitInt500这个类,别问我那个什么int128函数,不顶事):
1.创建数组
2.从字符串构造大型整数
3.大整数加法(直接加肯定不行,用了逐位加法)
4.输出函数(只是懒得在程序中写影响美观)
注:*it为迭代器

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;__uint128_t a,b;
const int ma=001;class BigInt500 {
public:vector<int> digits; // 存储每一位数字,digits[0] 为最低位bool is_negative;   // 新增:标记是否为负数BigInt500() : is_negative(false) {} // 默认构造函数// 从字符串构造大整数BigInt500(const string& num) : is_negative(false) {if (num.empty()) {throw invalid_argument("Input string is empty.");}int start = 0;if (num[0] == '-') {is_negative = true;start = 1;if (start >= num.size()) {throw invalid_argument("Input string is only a minus sign.");}}for (int i = num.size() - 1; i >= start; --i) {if (isdigit(num[i])) {digits.push_back(num[i] - '0');} else {throw invalid_argument("Input string contains non-digit characters.");}}// 去除前导零while (digits.size() > 1 && digits.back() == 0) {digits.pop_back();}// 如果数字是0,重置符号if (digits.empty() || (digits.size() == 1 && digits[0] == 0)) {is_negative = false;}}// 辅助函数:比较两个BigInt500的绝对值大小bool absGreaterOrEqual(const BigInt500& other) const {if (digits.size() != other.digits.size()) {return digits.size() > other.digits.size();}for (int i = digits.size() - 1; i >= 0; --i) {if (digits[i] != other.digits[i]) {return digits[i] > other.digits[i];}}return true; // 相等}// 大整数加法BigInt500 operator+(const BigInt500& other) const {// 如果符号相同,直接相加if (is_negative == other.is_negative) {BigInt500 result;result.is_negative = is_negative;int carry = 0;int maxSize = max(digits.size(), other.digits.size());for (int i = 0; i < maxSize || carry; ++i) {int digit1 = (i < digits.size()) ? digits[i] : 0;int digit2 = (i < other.digits.size()) ? other.digits[i] : 0;int sum = digit1 + digit2 + carry;carry = sum / 10;result.digits.push_back(sum % 10);}return result;} else {// 符号不同,转为减法if (absGreaterOrEqual(other)) {return *this - other;} else {BigInt500 temp = other - *this;temp.is_negative = !temp.is_negative;return temp;}}}// 大整数减法BigInt500 operator-(const BigInt500& other) const {// 如果符号不同,转为加法if (is_negative != other.is_negative) {BigInt500 temp = other;temp.is_negative = !temp.is_negative;return *this + temp;}BigInt500 result;// 如果被减数绝对值小于减数绝对值if (!absGreaterOrEqual(other)) {result = other - *this;result.is_negative = !result.is_negative;return result;}result.is_negative = is_negative;int borrow = 0;for (int i = 0; i < digits.size(); ++i) {int digit1 = digits[i] - borrow;int digit2 = (i < other.digits.size()) ? other.digits[i] : 0;if (digit1 < digit2) {digit1 += 10;borrow = 1;} else {borrow = 0;}result.digits.push_back(digit1 - digit2);}// 去除结果中的前导零while (result.digits.size() > 1 && result.digits.back() == 0) {result.digits.pop_back();}// 如果结果是0,重置符号if (result.digits.size() == 1 && result.digits[0] == 0) {result.is_negative = false;}return result;}// 输出大整数void print() const {if (is_negative && !(digits.size() == 1 && digits[0] == 0)) {cout << '-';}for (auto it = digits.rbegin(); it != digits.rend(); ++it) {cout << *it;}cout << endl;}
};void read_uint128_t(__uint128_t &x) {string s;cin >> s;if (s.empty()) {cerr << "Error: Empty input string" << endl;return;}if (s.length() > 128) {cerr << "Error: Input string too long (exceeds 128 digits)" << endl;return;}for (char c : s) {if (!isdigit(c)) {cerr << "Error: Non-digit character in input" << endl;return;}}x = 0;for (int i = 0; i < s.size(); ++i) {x = x * 10 + (s[i] - '0');}
}void write_uint128_t(__uint128_t x) {if (x == 0) {cout << '0';return;}string s;while (x > 0) {s.push_back((x % 10) + '0');x /= 10;}// 检查结果长度是否超过128位if (s.length() > 128) {cerr << "Error: Number too large to represent as 128-bit unsigned integer" << endl;return;}reverse(s.begin(), s.end());cout << s;
}int main() {try {BigInt500 num1, num2;string str1, str2;cout << "Enter first number: ";cin >> str1;cout << "Enter second number: ";cin >> str2;num1 = BigInt500(str1);num2 = BigInt500(str2);BigInt500 diff = num1 - num2;cout << "Result: ";diff.print();} catch (const invalid_argument& e) {cerr << "Error: " << e.what() << endl;return 1;} catch (const exception& e) {cerr << "Unexpected error: " << e.what() << endl;return 1;}return 0;
}

后记

其实也不难,对吧
注:该代码可能会TLE,不过多试几遍肯定够能AC

http://www.hskmm.com/?act=detail&tid=38382

相关文章:

  • 10-23 好题选讲总结
  • 关于驻马店市 2025 中小学信息学竞赛的记录(入门级)(未完)
  • 关于Markdown的使用
  • 自定义Spring Cloud LoadBalancer实践
  • 游记——驻马店市2025中小学信息学竞赛(未完)
  • 线段树上二分
  • ABP - SqlSugar [SqlSugarModule、ISqlSugarClient、SqlSugarRepository]
  • Odoo18.0 对接 京东快递
  • SAP折旧模拟超过1000条资产dump问题及解决
  • [python] 代码性能分析工具line_profiler使用指北
  • react的diff算法
  • LLM学习记录DAY11
  • ABP - 当前用户 [ICurrentUser、CurrentUser]
  • 轮次检测模型 VoTurn-80M 开源,多模态融合架构;OpenAI 收购桌面助手 Sky:实时识别屏幕自然语言交互丨日报
  • 第4天(中等题 滑动窗口、哈希表)
  • 幂函数
  • ABP - 依赖注入和属性注入
  • SAP维护汇率的关键Tcode
  • ABP vNext 框架功能模块 - 动态API(Dynamic API)
  • ABP vNext 框架功能模块 - 模块化(Modularity)
  • Devolutions Server权限提升漏洞分析与修复指南
  • AI股票预测分析报告 - 2025年10月24日 - 20:08:50
  • 题解:P14299 [JOI2023 预选赛 R2] 填充 / Painting
  • str.endswith() 类似的方法
  • 在 Astro 博客中优雅使用 51.la 统计数据
  • 2025.10.24博客
  • cgroup
  • 设计模式:代码界的 “光之巨人” 养成指南(附 C++ 实战)
  • 深度剖析OpenHarmony AI Engine:开发板端侧大模型推理插件机制全链路拆解 - 实践
  • Linux下的拼音输入法 (3)